Try to push some files via SFTP/WinSCP and handle all exceptions in PowerShell
Hi,
I am scripting an automation copy via SFTP/WinSCP.
It's working fine, but I would like to have an issue if the SFTP server doesn't connect for example.
Currently, I add this condition in my script :
If the SFTP server is on, all my files are uploaded.
But I would like to have an exception if the SFTP server can't connect, and my script doesn't do that for the moment.
I was thinking receive an issue with $transfer.Error.
But there is no issue at all, like all files will be copied, but it's not the case.
Here is the result of
As you can see, there is a
But the SFTP server is shutdown, so why I don't have any error here ?
Thanks
I am scripting an automation copy via SFTP/WinSCP.
It's working fine, but I would like to have an issue if the SFTP server doesn't connect for example.
Currently, I add this condition in my script :
# Create a WinSCP session $session = New-Object WinSCP.Session $session.Open($sessionOptions) # Upload files $transferResult = $session.PutFiles("E:\DATA\test\Data\To process\*", $SSH_destination_path) # Throw on any error $transferResult.Check() $result = $transferResult | ConvertTo-Json echo $result # Print results foreach ($transfer in $transferResult.Transfers) { $Date = Get-Date -format G # Success or error? if ($transfer.Error -eq $Null) { $Log += ("{0} - Upload of {1} succeeded on the SFTP server {2}`n" -f $Date, $transfer.FileName, $SSH_hostname) # Upload succeeded, move source file to backup #Move-Item $transfer.FileName $archive_data_path } else { $ExceptionFlag = $true $Log += ("{0} - Upload of {1} failed: {2}`n" -f $Date, $transfer.FileName, $transfer.Error.Message) } }
But I would like to have an exception if the SFTP server can't connect, and my script doesn't do that for the moment.
I was thinking receive an issue with $transfer.Error.
But there is no issue at all, like all files will be copied, but it's not the case.
Here is the result of
$transferResult
in JSON:
{ "Transfers": [ { "Destination": "/lala - Copy.xml", "Touch": "WinSCP.TouchEventArgs", "Chmod": null, "Removal": null, "FileName": "E:\\DATA\\test\\Data\\To process\\lala - Copy.xml", "Error": null }, { "Destination": "/lala.xml", "Touch": "WinSCP.TouchEventArgs", "Chmod": null, "Removal": null, "FileName": "E:\\DATA\\test\\Data\\To process\\lala.xml", "Error": null } ], "Failures": [ ], "IsSuccess": true }
null
for Error
...
But the SFTP server is shutdown, so why I don't have any error here ?
Thanks