3

I have an existing Powershell script which works without issue in PowerShell 5.1. After installing PowerShell 7, the script no longer works and it fails when trying to establish a WinSCP session.

First, session options are created via New-WinSCPSessionOption, and those options are stored to $sessionOption without issue.

$sessionOption = New-WinSCPSessionOption -HostName $hostName -Credential $credentials -Protocol Ftp

When the session setup runs, an exception is thrown in Powershell 7:

$session = New-WinSCPSession -SessionOption $sessionOption

The following exception is thrown:

Line |
 |      $session = New-WinSCPSession -SessionOption $sessionOption
 |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | Exception calling "Open" with "1" argument(s): "Method not found: 'Void
 | System.Threading.EventWaitHandle..ctor(Boolean, System.Threading.EventResetMode, System.String,
 | Boolean ByRef, System.Security.AccessControl.EventWaitHandleSecurity)'."
Mike
  • 1,010
  • 1
  • 14
  • 33
  • In case you didn't know, since `PS Core` is meant more for cross platform use, they removed most windows exclusive cmdlets. No, **PS Core will not replace powershell 5** that is why they haven't already added it to windows computers. **This is the reason that PS Core removed over 3000 cmdlets and 70 modules from PS 5** (7678 > 2589 cmdlets, 136 > 57 modules). The reason your script does not work in PS Core is because of the `New-WinSCPSessionOption` which is targeted to Windows more and not a cross-platform environment. – Nico Nekoru Jun 09 '20 at 03:21
  • Some parts of the function are windows exclusive so it doesn't work. Check the function again for the exact error which is probably caused by a cmdlet or type getting removed. – Nico Nekoru Jun 09 '20 at 03:33
  • I assume you are using [this](https://www.powershellgallery.com/packages/WinSCP/5.7.4.1/Content/Functions%5CNew-WinSCPSession.ps1) Which `$FtpMode = (New-Object -TypeName WinSCP.FtpMode)` might cause an error since `WinSCP` type was removed in pscore. https://github.com/dotps1/WinSCP/issues/65 – Nico Nekoru Jun 09 '20 at 03:40
  • WinSCP .NET assembly itself works on .NET Core (on Windows). But you have to use the "netstandard2.0" build of the .dll. For some reason the wrapper is loading the "net40" build. – Consider posting this at https://github.com/dotps1/WinSCP/issues. – Martin Prikryl Jun 09 '20 at 05:20
  • Thank you @NekoMusume and Martin Prikryl for your quick responses. I have to eveluate and determine course of action, but I appreciate the help and references. – Mike Jun 09 '20 at 21:27

1 Answers1

0

Assuming you still have PowerShell 5 available, and you really want to use WinSCP (or some other cmdlet that no longer works) with PowerShell 7, it's possible so long as you can, or are prepared to run your code as administrator.

From an administrator elevated shell, you'll need to use WinPSCompatSession then send your commands through that session.

$hostname = 'yourHostName'
$username = 'yourUsername'
$password = 'yourPassword'
$fingerprint = 'yourFingerprint'


$session = New-PSSession -Name WinPSCompatSession 

Invoke-Command -Session $session -ScriptBlock {

  Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"            

  $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol              = [WinSCP.Protocol]::Sftp
    HostName              = $using:hostname
    UserName              = $using:username
    SecurePassword        = $using:password
    SshHostKeyFingerprint = $using:fingerprint
  }

  $session = New-Object WinSCP.Session
  $session.Open($sessionOptions)        
}
# and so on...     

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_windows_powershell_compatibility?view=powershell-7.3

As above any variables that you want to use within the scriptBlock that exist outside will need to be called like $using:yourVariableName because they're in a different scope.

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.3&viewFallbackFrom=powershell-7#scope-modifiers