I am trying to connect to SFTP server using PowerShell, and using WinSCP .NET assembly.
Code is not able to open the session ($session.Open($sessionOptions)
).
In the logs I found,
Exception: System.MissingMethodException: Method not found: 'Void System.Threading.EventWaitHandle..ctor(Boolean, System.Threading.EventResetMode, System.String, Boolean ByRef, System.Security.AccessControl.EventWaitHandleSecurity)'. at WinSCP.ExeSessionProcess.TryCreateEvent(String name, EventWaitHandle& ev) at WinSCP.ExeSessionProcess.InitializeConsole() at WinSCP.ExeSessionProcess.Start() at WinSCP.Session.Open(SessionOptions sessionOptions) [2019-03-14 10:59:02.835Z] [0013] Session.Cleanup entering [2019-03-14 10:59:02.835Z] [0013] Terminating process
Basically, it jumps from $session.Open($sessionOptions)
to $session.Dispose()
and skipping other part of code.
I tried to find solution here at WinSCP forum, but it didn't help:
https://winscp.net/forum/viewtopic.php?t=26140
try {
Add-Type -Path "M:\AMA\ztemp_powershell\WinSCPnet.dll"
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "abc.xyz.ca"
UserName = "abc_abc"
Password = "*********"
SshHostKeyFingerprint = "ssh-rsa 2048 **********************"
PortNumber = 22
}
$session = New-Object WinSCP.Session
$session.ExecutablePath = "H:\FromLocal\Powershell\WinSCP.exe"
$filelist = Get-ChildItem M:\AMA\ztemp_powershell\sample_files
try {
# $session.DebugLogPath = "M:\AMA\ztemp_powershell\sftp1.log"
$session.Open($sessionOptions)
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
foreach ($file in $filelist) {
$transferResult = $session.PutFiles("M:\AMA\ztemp_powershell\sample_files\$file" , "/outbox/", $false, $transferOptions)
foreach ($transfer in $transferResult.Transfers) {
Write-Host "Upload of $($transfer.FileName) succeeded"
}
}
}
finally {
$session.Dispose()
}
exit 0
}
catch {
Write-Host "Error: $_"
exit 1
}