Session.PutFiles Method doesn't upload multiple files or display multiple errors
I am new at PowerShell, and I am using the automated script to upload files to an FTP site.
In order to test for failures, I created five text files and removed the permissions from two of them so that the error message would be:
Can't open file 'H:\Documents\PositivePay\Test02.txt'.
Test Files:
Test01
Test02 (Permissions Removed; Properties > Security > Advanced > Change Permissions > Uncheck "Include..." > Remove > Apply)
Test03
Test04 (Permissions Removed; Properties > Security > Advanced > Change Permissions > Uncheck "Include..." > Remove > Apply)
Test05
When uploading all five files at once, I would expect to see:
Upload of H:\Documents\PositivePay\Test01.txt succeeded, moving to backup
Upload of H:\Documents\PositivePay\Test02.txt failed: Can't open file 'H:\Documents\PositivePay\Test02.txt'.
System Error. Code: 5.
Access is denied
Upload of H:\Documents\PositivePay\Test03.txt succeeded, moving to backup
Upload of H:\Documents\PositivePay\Test04.txt failed: Can't open file 'H:\Documents\PositivePay\Test02.txt'.
System Error. Code: 5.
Access is denied
Upload of H:\Documents\PositivePay\Test03.txt succeeded, moving to backup
However, instead the following happens:
Only the first file (Test01) is uploaded.
Only the first error (Test02) is displayed.
Nothing happens to Test03, Test04 or Test05.
From the Console:
Upload of H:\Documents\PositivePay\Test01.txt succeeded, moving to backup
Upload of H:\Documents\PositivePay\Test02.txt failed: Can't open file 'H:\Documents\PositivePay\Test02.txt'.
System Error. Code: 5.
Access is denied
Here is the script.
param (
$localPath = "H:\Documents\PositivePay\",
$remotePath = "/PosPayPut",
$backupPath = "H:\Documents\BMOSFTP\Backup"
)
try
{
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "INTENTIONALLY BLANK"
UserName = "INTENTIONALLY BLANK"
Password = "INTENTIONALLY BLANK"
SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Upload files, collect results
$transferResult = $session.PutFiles($localPath, $remotePath)
# Iterate over every transfer
foreach ($transfer in $transferResult.Transfers)
{
# Success or error?
if ($transfer.Error -eq $Null)
{
Write-Host ("Upload of {0} succeeded, moving to backup" -f
$transfer.FileName)
# Upload succeeded, move source file to backup
Move-Item $transfer.FileName $backupPath
}
else
{
Write-Host ("Upload of {0} failed: {1}" -f
$transfer.FileName, $transfer.Error.Message)
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
#exit 0
}
catch [Exception]
{
Write-Host ("Error: {0}" -f $_.Exception.Message)
#exit 1
}
In order to test for failures, I created five text files and removed the permissions from two of them so that the error message would be:
Can't open file 'H:\Documents\PositivePay\Test02.txt'.
Test Files:
Test01
Test02 (Permissions Removed; Properties > Security > Advanced > Change Permissions > Uncheck "Include..." > Remove > Apply)
Test03
Test04 (Permissions Removed; Properties > Security > Advanced > Change Permissions > Uncheck "Include..." > Remove > Apply)
Test05
When uploading all five files at once, I would expect to see:
Upload of H:\Documents\PositivePay\Test01.txt succeeded, moving to backup
Upload of H:\Documents\PositivePay\Test02.txt failed: Can't open file 'H:\Documents\PositivePay\Test02.txt'.
System Error. Code: 5.
Access is denied
Upload of H:\Documents\PositivePay\Test03.txt succeeded, moving to backup
Upload of H:\Documents\PositivePay\Test04.txt failed: Can't open file 'H:\Documents\PositivePay\Test02.txt'.
System Error. Code: 5.
Access is denied
Upload of H:\Documents\PositivePay\Test03.txt succeeded, moving to backup
However, instead the following happens:
Only the first file (Test01) is uploaded.
Only the first error (Test02) is displayed.
Nothing happens to Test03, Test04 or Test05.
From the Console:
Upload of H:\Documents\PositivePay\Test01.txt succeeded, moving to backup
Upload of H:\Documents\PositivePay\Test02.txt failed: Can't open file 'H:\Documents\PositivePay\Test02.txt'.
System Error. Code: 5.
Access is denied
Here is the script.
param (
$localPath = "H:\Documents\PositivePay\",
$remotePath = "/PosPayPut",
$backupPath = "H:\Documents\BMOSFTP\Backup"
)
try
{
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "INTENTIONALLY BLANK"
UserName = "INTENTIONALLY BLANK"
Password = "INTENTIONALLY BLANK"
SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Upload files, collect results
$transferResult = $session.PutFiles($localPath, $remotePath)
# Iterate over every transfer
foreach ($transfer in $transferResult.Transfers)
{
# Success or error?
if ($transfer.Error -eq $Null)
{
Write-Host ("Upload of {0} succeeded, moving to backup" -f
$transfer.FileName)
# Upload succeeded, move source file to backup
Move-Item $transfer.FileName $backupPath
}
else
{
Write-Host ("Upload of {0} failed: {1}" -f
$transfer.FileName, $transfer.Error.Message)
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
#exit 0
}
catch [Exception]
{
Write-Host ("Error: {0}" -f $_.Exception.Message)
#exit 1
}