PowerShell Script Hanging
I put together a script that works fine, it's setup as a scheduled task but after X amount of runs it will randomly hang. Once the hanging starts, it continues until I reboot the server. I cannot figure out why. I can take the same script and run it on another PC and it works without any issue but will eventually hang the same way. I can't tell if its a bug with WinSCP, PowerShell, or the script. I feel like if the previous sessions aren't being disposed correctly or something? To be honest I have no clue. It just hangs in the task scheduler or if I run it manually.
Here is the part of the log that the script just get's stuck.
. 2020-09-10 08:00:20.356 Getting real path for '/' > 2020-09-10 08:00:20.356 Type: SSH_FXP_REALPATH, Size: 10, Number: 272 < 2020-09-10 08:00:20.403 Type: SSH_FXP_NAME, Size: 49, Number: 272 . 2020-09-10 08:00:20.403 Real path is '/USERNAME' . 2020-09-10 08:00:20.403 File: 'C:\WinSCP Scripts\Temp2\XXXXX.xml' [2020-09-09T20:37:08.500Z] [75239] . 2020-09-10 08:00:20.403 Copying "C:\WinSCP Scripts\Temp2\XXXXX..xml" to remote directory started. . 2020-09-10 08:00:20.403 Binary transfer mode selected. . 2020-09-10 08:00:20.403 Opening remote file. > 2020-09-10 08:00:20.403 Type: SSH_FXP_OPEN, Size: 62, Number: 515 < 2020-09-10 08:00:20.465 Type: SSH_FXP_HANDLE, Size: 33, Number: 515 > 2020-09-10 08:00:20.465 Type: SSH_FXP_WRITE, Size: 32813, Number: 1030 . 2020-09-10 08:00:20.465 2 skipped SSH_FXP_WRITE, SSH_FXP_READ, SSH_FXP_DATA and SSH_FXP_STATUS packets. > 2020-09-10 08:00:20.465 Type: SSH_FXP_CLOSE, Size: 33, Number: 1796 > 2020-09-10 08:00:20.465 Type: SSH_FXP_SETSTAT, Size: 58, Number: 777 . 2020-09-10 08:00:36.870 Waiting for data timed out, asking user what to do. . 2020-09-10 08:00:36.870 Asking user: . 2020-09-10 08:00:36.870 **Host is not communicating for 15 seconds. . 2020-09-10 08:00:36.870 . 2020-09-10 08:00:36.870 Wait for another 15 seconds?** () < 2020-09-10 08:00:36.870 Script: Host is not communicating for more than 15 seconds. < 2020-09-10 08:00:36.870 Still waiting... < 2020-09-10 08:00:36.870 Note: If the problem repeats, try turning off 'Optimize connection buffer size'. < 2020-09-10 08:00:36.870 Warning: Aborting this operation will close connection! . 2020-09-10 08:00:39.949 Data has arrived, closing query to user. . 2020-09-10 08:00:39.949 Network error: Software caused connection abort . 2020-09-10 08:00:39.949 Connection was lost, asking what to do. . 2020-09-10 08:00:39.949 Asking user: . 2020-09-10 08:00:39.949 Network error: Software caused connection abort () < 2020-09-10 08:00:39.949 Script: Network error: Software caused connection abort
The task errors with
Task Scheduler did not launch task "\Script" because instance "{654b6dbf-fd19-4a8c-8ce5-7b42590d11e6}" of the same task is already running.
Here is the script, I removed any identifying info
# Load WinSCP .NET assembly Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll" # Set up session options $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Sftp HostName = "xxxx.com" UserName = "XXXXXXSFTP" SshHostKeyFingerprint = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" SshPrivateKeyPath = "C:\Program Files (x86)\WinSCP\SSH Key\XXXXXXXX\XXXXXXXXXXXKey.ppk" } $remotePath = "/" $localPath = "C:\WinSCP Scripts\XXXXXXXXXXXX\Temp2\*" $uploadSession = New-Object WinSCP.Session $uploadSession.SessionLogPath = "C:\WinSCP Scripts\XXXXXXXXXXXXt\TEST-Upload.log" try { # Connect $uploadSession.Open($sessionOptions) # Upload files $transferOptions = New-Object WinSCP.TransferOptions $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary $transferOptions.OverwriteMode = [WinSCP.OverwriteMode]::Overwrite #Upload Files - localPath, remotePath, remove (true, deletes source local file(s) after a successful transfer) $transferResult = $uploadSession.PutFiles($localPath, $remotePath, $False, $transferOptions) # Print results foreach ($transfer in $transferResult.Transfers){ Write-Host ("Upload of {0} succeeded" -f $transfer.FileName) #Copy-Item $transfer.FileName -Destination "E:\XXXXXXXXXXXXXXXXXXX" #Move-Item $transfer.FileName -Destination "\\XXXXXXXXXXXXX\Archive" $transferResult | Out-File "C:\WinSCP Scripts\XXXXXXXXXXXX\TEST-TransferResult.log" $transferResult.Transfers | Out-File "C:\WinSCP Scripts\XXXXXXXXXXXX\TEST-Transfers.log" # Throw on any error $transferResult.Check() } finally { $uploadSession.Dispose() } exit 0 catch { Write-Host "Error: $($_.Exception.Message)" exit 1 }