Timeout waiting for WinSCP to respond
Below is my powershell script that is failing due to 'Timeout waiting for WinSCP to respond',
however using the GUI I can transfer the file without an issue. Log is attached.
however using the GUI I can transfer the file without an issue. Log is attached.
function Email-Alert($subject, $msg){ $smtpServer = "xxxx" $smtpFrom = $smtpTo = $message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto $message.Subject = $subject $message.IsBodyHtml = $false $message.Body = $ErrorMessage $message.Priority = [System.Net.Mail.MailPriority]::High $smtp = New-Object Net.Mail.SmtpClient($smtpServer) $smtp.Send($message) } function UploadTo-KiteWorksFTP ( [Parameter(Mandatory=$true)][string] $outgoing_dir, # Directory To Upload [Parameter(Mandatory=$true)][string] $remote_dir, [Parameter(Mandatory=$false)][string] $HostName = [Parameter(Mandatory=$false)][string] $UserName = , [Parameter(Mandatory=$false)][string] $Password = , [Parameter(Mandatory=$false)][string] $SshHostKeyFingerprint =xxxx # From WinSCP client ) { write-host '--------------------------------------------------------------------' write-host ("Uploading files to the following directory: - " + $(Get-Date)) Write-host "$remote_dir" write-host '--------------------------------------------------------------------' write-host " `n" try { # Load WinSCP .NET assembly $path = "C:\Program Files (x86)\WinSCP\WinSCPnet.dll" Add-Type -Path $path # Setup session options $sessionOptions = New-Object WinSCP.SessionOptions $sessionOptions.PortNumber=22 $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp $sessionOptions.HostName = $HostName $sessionOptions.UserName = $UserName $sessionOptions.Password = $Password $sessionOptions.SshHostKeyFingerprint = $SshHostKeyFingerprint $session = New-Object WinSCP.Session $Session.ExecutablePath = "C:\Program Files (x86)\WinSCP\winscp.exe" $session = New-Object WinSCP.Session $session.SessionLogPath = "C:\Program Files (x86)\WinSCP\Logs\winscp.log" try { # Connect $session.Open($sessionOptions) # Upload files $transferOptions = New-Object WinSCP.TransferOptions $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary $transferOptions.PreserveTimestamp =$false $transferOptions.ResumeSupport.State = [WinSCP.TransferResumeSupportState]::Off $transferResult = $session.PutFiles("$outgoing_dir", "$remote_dir", $False, $transferOptions) # Throw on any error $transferResult.Check() # Print results foreach ($transfer in $transferResult.Transfers) { $n_files_uploaded += 1 Write-Host ("Upload of {0} succeeded" -f $transfer.FileName) } return $n_files_uploaded } finally { # Disconnect, clean up $session.Dispose() } } catch [Exception] { Write-Host $_.Exception.Message $ErrorMessage = $_.Exception.message Write-Output $ErrorMessage #Email-Alert -subject "Kiteworks File Transfer Failed!!!" -msg $ErrorMessage Break } } # rename file $LastestFile = (gci \\Dcbvddbajump02\irm\| sort LastWriteTime | select-object -ExpandProperty name -last 1) # Gets the latest file in the directory $FullPath = '\\Dcbvddbajump02\irm\' + $LastestFile # adding the date to the file $date = Get-Date -format "yyyyMMdd" $FileNewName = 'Vault_Original_Documents_' + $date + '.xlsx' # $FileNewName # this will rename the file Rename-Item -Path $FullPath -NewName $FileNewName Write-Host 'Renaming File Name' $NewFileToUpload = Get-ChildItem \\Dcbvddbajump02\irm\ | select-object -Last 1 | % { $_.FullName } #Define Connection in Function or send as args $FileToUpload = $NewFileToUpload # "FileSystem::\\servername\file\dataset.txt" $RemoteDirectory = "/Disaster Recovery/Vault Original Documents Report/" UploadTo-KiteWorksFTP -outgoing_dir $FileToUpload -remote_dir $RemoteDirectory <# #Or All files in directory with Archive $FileSourcePath = "C:\temp\" $ArchiveFolder = "C:\temp\Archive\" + (Get-Date).ToString("yyyy") + "\" + (Get-Date).ToString("MM") + "\" + (Get-Date).ToString("dd") + "\" #Create Archive Folder if(!(Test-Path -Path $ArchiveFolder)){New-Item -Path $ArchiveFolder -ItemType directory} $FilesToProcess = Get-ChildItem -Path $FileSourcePath -Filter "*.xlsx" #Iterate through files, upload and move to archive foreach($file in $FilesToProcess){ #Upload UploadTo-KiteWorksFTP -outgoing_dir $file.fullname -remote_dir "/files/" #Archive Move-Item -Path $file.fullname -Destination $ArchiveFolder -Force } #>