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

Author Message
moymike
[View user's profile]

Joined: 2016-06-07
Posts: 3
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:
Code:
$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:
Code:
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:
Code:
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?
powershell no winscp.PNG (56.88 KB) [Download]

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

Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
Location: Prague, Czechia
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?
moymike
[View user's profile]

Joined: 2016-06-07
Posts: 3
prikryl wrote:
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?


Here is a link: https://blogs.msdn.microsoft.com/powershell/2015/11/16/announcing-powershell-language-support-for-visual-studio-code-and-more/
(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 Verion 5 is included with Win10, available for Win8.1 and Srv2012)
powershell no winscp.PNG (56.88 KB) [Download]

Description: Powershell within VS2015

moymike
[View user's profile]

Joined: 2016-06-07
Posts: 3
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
Code:
PS> $PROFILE | Format-List * -Force


Test AllUsersAllHosts profile
Code:
PS> test-path $PROFILE.AllUsersAllHosts


If false
Code:
PS> new-item $PROFILE.AllUsersAllHosts -ItemType file -Force


Modify the profile AllUsersAllHosts to add the winSCP type (with the appropriate path)
Code:
#Add this line to the Profile.ps1 file AllUsersAllHosts

add-type -Path 'C:\Program Files (x86)\WinSCP\WinSCPnet.dll'
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
Location: Prague, Czechia
Thanks for sharing your solution.

I'll look into it.
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License