Post a reply

Before posting, please read how to report bug or request support effectively.

Bug reports without an attached log file are usually useless.

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

cjwinn

That worked. Thanks!
martin

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
cjwinn

Here are some redacted logs with private info removed if anyone else wants to take a look.
cjwinn

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()