Get successful upload response even when transfer is unsuccessful
Hi
I have a script that uploads to an SFTP server. The server it is uploading to is filename driven, meaning that if it receives a file that is an incorrect filename to what it is expecting it automatically rejects it. For some reason winscp is reporting success even if the transfer was in fact unsuccessful due to me purposely sending a file with the wrong filename, so when I log into the SFTP server as expected the file is not there. Does anyone have any idea what the issue could be?
Please see code:
try
{
write-host "Trying"
# Connect
$session.Open($sessionOptions)
write-host "connected"
# Upload files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult = $session.PutFiles($FullPath, "/inbox/", $False, $transferOptions)
write-host "pushed files"
# Throw on any error
$transferResult.Check()
write-host "error check"
# Print results
foreach ($transfer in $transferResult.Transfers)
{
# Success or error?
if ($transfer.Error -eq $Null)
{
$errortest = $transfer | out-string
$errortest2 = $transferResult | out-string
$errortest3 = $transferResult.Transfers | out-string
Write-Host $errortest
Write-Host $errortest2
Write-Host $errortest3
Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)
Move-Item $FullPath $MovedFolder -force
}
else
{
Write-Host ("Upload of {0} failed: {1}" -f
$transfer.FileName, $transfer.Error.Message)
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
write-host "session disposed"
stop-transcript
}
exit 0
}
catch [Exception]
{
write-host "In the catch"
Write-Host ("Error: {0}" -f $_.Exception.Message)
stop-transcript
exit 1
}
I have a script that uploads to an SFTP server. The server it is uploading to is filename driven, meaning that if it receives a file that is an incorrect filename to what it is expecting it automatically rejects it. For some reason winscp is reporting success even if the transfer was in fact unsuccessful due to me purposely sending a file with the wrong filename, so when I log into the SFTP server as expected the file is not there. Does anyone have any idea what the issue could be?
Please see code:
try
{
write-host "Trying"
# Connect
$session.Open($sessionOptions)
write-host "connected"
# Upload files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult = $session.PutFiles($FullPath, "/inbox/", $False, $transferOptions)
write-host "pushed files"
# Throw on any error
$transferResult.Check()
write-host "error check"
# Print results
foreach ($transfer in $transferResult.Transfers)
{
# Success or error?
if ($transfer.Error -eq $Null)
{
$errortest = $transfer | out-string
$errortest2 = $transferResult | out-string
$errortest3 = $transferResult.Transfers | out-string
Write-Host $errortest
Write-Host $errortest2
Write-Host $errortest3
Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)
Move-Item $FullPath $MovedFolder -force
}
else
{
Write-Host ("Upload of {0} failed: {1}" -f
$transfer.FileName, $transfer.Error.Message)
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
write-host "session disposed"
stop-transcript
}
exit 0
}
catch [Exception]
{
write-host "In the catch"
Write-Host ("Error: {0}" -f $_.Exception.Message)
stop-transcript
exit 1
}