WinSCP appears to ignore $session.Timeout

Advertisement

cjwinn
Joined:
Posts:
9
Location:
USA

WinSCP appears to ignore $session.Timeout

Hello,

I have a PowerShell script that is connecting to an SFTP server with a directory containing upwards of 200,000 files we need to download. Unfortunately, after 60 seconds, we are receiving a timeout error. I added the $session.Timeout parameter and set it to 120, but it appears that WinSCP is now exiting with an error immediately. My script is below, and I will also attach the session log and debug log.

Basic Info
WinSCP version: 5.17.6 (Build 10516). Unfortunately, upgrading to a more recent version is not an option for us at this point due to our update cycle.
Windows: Windows Server 2016 Standard version 1607
Transfer Protocol: SFTP with private key authentication
Scripting with PowerShell

Error Message:
D:\Temp\Script\Miscellaneous\script.ps1 : Exception calling "Open" with "1" argument(s): "Timeout waiting for WinSCP to respond - WinSCP has not responded in time. There was no output. Response log file C:\Users\user\AppData\Local\Temp\11\wscp8BA8.007A1AA3.tmp was not created. This could indicate lack of write permissions to the log folder or problems starting WinSCP itself."
At line:1 char:1
+ D:\Temp\Script\Miscellaneous\script.ps1 `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [script.ps1], MethodInvocationException
    + FullyQualifiedErrorId : TimeoutException,script.ps1
Script:
[CmdletBinding()]
param
(
   [Parameter(Mandatory = $false)]
   [string] $winSCPLocation,
   
   [Parameter(Mandatory = $false)]
   [string] $sourceUser,
   
   [Parameter(Mandatory = $false)]
   [string] $sourceAddress,
   
   [Parameter(Mandatory = $false)]
   [int] $sourcePort,
   
   [Parameter(Mandatory = $false)]
   [string] $sourcePrivateKeyPath,
   
   [Parameter(Mandatory = $false)]
   [string] $logDirectory
)
 
$date = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
$fileDate = Get-Date -Format 'yyyyMMdd_HHmmss'
 
Add-Type -Path $winSCPLocation
 
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
   Protocol = [WinSCP.Protocol]::"sftp"
   HostName = $sourceAddress
   UserName = $sourceUser
   PortNumber = $sourcePort
   GiveUpSecurityAndAcceptAnySshHostKey = 1
}
 
$sessionOptions.SshPrivateKeyPath = "$sourcePrivateKeyPath"
 
#Set Key Exchange Algorithm Selection Policy
$sessionOptions.AddRawSettings("KEX", "ecdh,dh-gex-sha1,dh-group14-sha1,rsa,dh-group1-sha1,WARN")
 
#Set Optimize Connection Buffer Size to off (0) and set SshSimple to 1 to completely disable optimzation of connection buffer size
$sessionOptions.AddRawSettings("SendBuf",0)
$sessionOptions.AddRawSettings("SshSimple",1)
 
$session = New-Object WinSCP.Session
 
$sessionLogPathWinSCP = "$logDirectory\WinSCPSession_$fileDate.log"
$session.SessionLogPath = $sessionLogPathWinSCP
$winSCPSessionLogLocation = "WinSCP Session Log Location: $sessionLogPathWinSCP"
 
$debugLogPathWinSCP = "$logDirectory\WinSCPDebug_$fileDate.log"
$session.DebugLogPath = $debugLogPathWinSCP
$session.DebugLogLevel = 2
         
$winSCPDebugLogLocation = "WinSCP Debug Log Location: $debugLogPathWinSCP"
 
$session.Timeout = 120
Write-Host "Attempting connection for sftp"
$session.Open($sessionOptions)
Write-Host "sftp Connection Established - Still Connected" 
 
$FileList = $session.ListDirectory($fileDirectory)
 
$FileList.Files | FT
 
$session.Dispose()
  • WinSCPDebug_20201027_113152_redacted.log (67.83 KB, Private file)
  • WinSCPDebug_20201027_113152.log (67.92 KB, Private file)

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,518
Location:
Prague, Czechia

Re: WinSCP Appears to Ignore $session.Timeout

The Session.Timeout is of type TimeSpan. When converting an integer to the TimeSpan, the PowerShell treats the integer value as microseconds (aka ticks), not as seconds. You want this instead:
$session.Timeout = New-TimeSpan -Seconds 120

Reply with quote

Advertisement

You can post new topics in this forum