Problem using WinSCPnet.dll inPowerShell, when in a CLASS [includes screen capture]

Advertisement

moymike
Joined:
Posts:
3

Problem using WinSCPnet.dll inPowerShell, when in a CLASS [includes screen capture]

I am having a problem using WinSCPnet.dll with PowerShell, in both ISE and VS2015.
I am running Windows 10, PowerShell V5 (version 5 build 10586 revision 122)
I have the WinSCPnet.dll file in two places, the program files (x86) and in the script directory.

I am using classes within my PowerShell script, which is where WinSCP is not being recognized.

The references to WinSCP within the class fail.
Unable to find type [WinSCP.Protocol]
My script looks like this:
$ScriptPath = $(Split-Path -Parent $MyInvocation.MyCommand.Definition) 
[Reflection.Assembly]::LoadFrom( $(Join-Path $ScriptPath "WinSCPnet.dll") ) | Out-Null
 
sessionOptions1 = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::ftp
    FtpMode = [WinSCP.FtpMode]::Active
}
 
Class Ftp
{
    Ftp()
    {
        sessionOptions2 = New-Object WinSCP.SessionOptions -Property @{
            Protocol = [WinSCP.Protocol]::Sftp
            FtpMode = [WinSCP.FtpMode]::Active
        }
    }
}
* the references to [WinSCP.Protocol] and [WinSCP.FtpModel] within the class both underlined in RED. (see attachment)

When I run the script within ISE, I get this:
PS D:\> D:\testFTP.ps1
At D:\testFTP.ps1:14 char:29
+                 Protocol = [WinSCP.Protocol]::Sftp
+                             ~~~~~~~~~~~~~~~
Unable to find type [WinSCP.Protocol].
At D:\testFTP.ps1:15 char:28
+                 FtpMode = [WinSCP.FtpMode]::Active
+                            ~~~~~~~~~~~~~~
Unable to find type [WinSCP.FtpMode].
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TypeNotFound
Within ISE I can "fix" this by typing the command within the script pane:
add-type -Path 'C:\Program Files (x86)\WinSCP\WinSCPnet.dll'
* to update the RED underlines I have to edit something within the code

Within VS2015 community edition, I am stuck. I can't get it to acknowledge WinSCP at all.

I checked my DLL's, and they are not blocked. I registered the DLL and that didn't change anything. I want to stress that WinSCP is working for me outside of a CLASS. Within ISE, if I manually Add-Type to it, then WinSCP will work within a CLASS. This work around does not work with VS though.

What can I do to fix this, and how can I get this PowerShell script to work within VS?

Description: Screen capture of ISE with error text in script box.

powershell no winscp.PNG

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,476
Location:
Prague, Czechia

Re: Problem using WinSCPnet.dll inPowerShell, when in a CLASS [includes screen capture]

I've never used PowerShell in Visual Studio. How do you do that? Can your post screenshot?

The screenshot your posted in from ISE, but that's solved, right?

Reply with quote

moymike
Joined:
Posts:
3

Re: Problem using WinSCPnet.dll inPowerShell, when in a CLASS [includes screen capture]

Here is a link: <invalid hyperlink removed by admin>
(screen capture attached)

I installed VS 2015 community edition and selected the 3rd party PowerShell support during the installation. You can add PowerShell support after the install by going to Tools / Extensions and Updates .. search for "PowerShell" and add.

Since I posted, I found that I can make my workaround get me going in VS also.

I am hoping there is a "more better" way to get ISE and VS to see WinSCP when working with PowerShell. Having to manually go into the interactive session to load WinSCP via Add-Type is clunky at best. It means that every time I load a script that uses WinSCP, I can't even run it within the shell until I do this workaround.

Opinion: PowerShell is likely to really takeoff. I am using version 5, and I see it as a best option for automating some of my processes. I am expecting it will become a major player in this kind of development.

(PowerShell Version 5 is included with Win10, available for Win8.1 and Srv2012)

Description: Powershell within VS2015

powershell no winscp.PNG

Reply with quote

moymike
Joined:
Posts:
3

Another Workaround, not sure if it's a good idea though...

I found that by adding WinSCP to the main profile, everything works. I don't know how detrimental this approach is regarding non-WinSCP scripts though.

Within PowerShell:

Check profiles
PS> $PROFILE | Format-List * -Force
Test AllUsersAllHosts profile
PS> test-path $PROFILE.AllUsersAllHosts
If false
PS> new-item $PROFILE.AllUsersAllHosts -ItemType file -Force
Modify the profile AllUsersAllHosts to add the WinSCP type (with the appropriate path)
#Add this line to the Profile.ps1 file AllUsersAllHosts 
 
add-type -Path 'C:\Program Files (x86)\WinSCP\WinSCPnet.dll'

Reply with quote

martin
Site Admin
martin avatar

Re: Problem using WinSCPnet.dll inPowerShell, when in a CLASS [includes screen capture]

Thanks for sharing your solution.

I'll look into it.

Reply with quote

Advertisement

xjuanch0x
Guest

Re: Problem using WinSCPnet.dll inPowerShell, when in a CLASS [includes screen capture]

Hi
I have the same problem, only work loading the library from the profil.ps1 file,
I'm using this command but not work, I get the same error.
Add-Type -Path 'D:\PScripts\WinScp\WinSCPnet.dll'

errorWinscp.PNG

Reply with quote

Advertisement

You can post new topics in this forum