PowerShell- Session.Open hangs when using incorrect options

Advertisement

Li
Joined:
Posts:
3

PowerShell- Session.Open hangs when using incorrect options

Hi,
I'm opening a secure WebDAV session to my server, using the .NET classes from WinSCP 6.3.7
When using correct options (WinSCP.SessionOptions), everything works smoothly, but when altering some options, like using an incorrect RootPath, the Session.Open just hangs.
In my PS script I tried using the other WinSCPnet.dll files, that come with the package, but to no avail.
I tried adding Timeout to SessionOptions ($TimeOut= New-Timespan -Seconds 20), but it doesn't work as well.
The only way to exit is disconnecting from the network.
How can I use a proper timeout when establishing a connection and, hopefully, get some exception?

Thanks

Reply with quote

Advertisement

Li
Joined:
Posts:
3

I managed to solve this with opening the session in a parallel runspace with a custom Timer of 10 seconds.
$ParameterList=@{
  Session=$Session
  SessionOptions=$SessionOptions
}             
$RunSpace= [RunspaceFactory]::CreateRunspace()             
$PS= [PowerShell]::Create()             
$PS.RunSpace= $RunSpace             
$RunSpace.Open()             
$null= $PS.AddScript({
   Param($Session,$SessionOptions)
   $Session.Open($SessionOptions)
}).AddParameters($ParameterList)
 
$AsyncObject= $PS.BeginInvoke()
$StartTime= Get-Date
Do{
 # Timer for 10 seconds
}While ((([DateTime]::Now)-$StartTime).Seconds -lt 10)
 
If ($AsyncObject.IsCompleted -ne "True"){
  write-host "$($Session.Output)"
  write-host "`n`nAborting"
  $Session.Abort()
  $Session.Dispose()
  $Inspector.Dispose()           
  $PS.Dispose()   
}
Else{
  #...rest of code with a valid $Session
}

Reply with quote

Advertisement

You can post new topics in this forum