That worked. Thanks!
- cjwinn
Before posting, please read how to report bug or request support effectively.
Bug reports without an attached log file are usually useless.
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
$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.
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
[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()