Hi there,
I've created a powershell script to upload and download a file from a remote FTPS servier.
The script does the following:
1. Uploads a file from my local dir
2. Moves the uploaded file into a local backup dir
3. Download files from the remote FTPS server to local dir
4.
Remove downloaded file from the remote site.
I'm having issues with point 4. I can manually delete/upload files to that folder I'm trying to download from, but I get the following error when running the script.
Download of /outbound/tes.tst /outbound/* succeeded, deleting from remote
Error: Exception calling "Check" with "0" argument(s): "Can't get attributes of file '/outbound/tes.tst'.
File or folder '/outbound/tes.tst' does not exist."
My Script is
# Load WinSCP .NET assembly
Add-Type -Path "c:\Program Files (x86)\WinSCP\WinSCPnet.dll"
$localAckPath = "C:\Users\user\Documents\test files\ACK\"
$remoteAckPath = "/outbound/test/*"
$backupOrdPath = "C:\Users\user\Documents\test files\localbackup\"
$remoteOrdPath = "/inbound/test/"
$localOrdFilePath = "C:\Users\user\Documents\test files\file to upload\*"
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Timeout = new-timespan -minutes 1
Protocol = [WinSCP.Protocol]::Ftp
HostName = "123.122.250.123"
PortNumber = port
UserName = "user"
Password = "pass"
FtpSecure = [WinSCP.FtpSecure]::Implicit
TlsHostCertificateFingerprint = "fingerprint"
}
while($true)
{
$session = New-Object WinSCP.Session
try
{
Write-Host "Checking..."
# Connect
$session.Open($sessionOptions)
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferOptions.PreserveTimestamp = $False
$transferResult =
$session.PutFiles( $localOrdFilePath, "/inbound/", $False, $transferOptions)
foreach ($transfer in $transferResult.Transfers)
{
# Success or error?
if ($transfer.Error -eq $Null)
{
Write-Host "Upload of $($transfer.FileName) succeeded, moving to backup"
# Upload succeeded, move source file to backup
Move-Item $transfer.FileName $backupOrdPath
}
else
{
Write-Host "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
}
}
$transferResult =
$session.GetFiles($remoteAckPath, $localAckPath, $False, $transferOptions)
foreach ($transfer in $transferResult.Transfers)
{
# Success or error?
if ($transfer.Error -eq $Null)
{
Write-Host "Download of $($transfer.FileName) $($remoteAckPath) succeeded, deleting from remote"
$filename = [WinSCP.RemotePath]::EscapeFileMask($transfer.FileName)
$remoteAckPath = $session.RemoveFiles($filename)
$remoteAckPath.Check()
if ($remoteAckPath.IsSuccess)
{
Write-Host "Removing of file $($transfer.FileName) succeeded"
}
else
{
Write-Host "Removing of file $($transfer.FileName) failed"
}
# Upload succeeded, move source file to backup
#Move-Item $transfer.FileName $backupPath
}
else
{
Write-Host "Download of $($transfer.FileName) failed: $($transfer.Error.Message)"
}
}
# Your code
# Throw on any error
# $transferResult.Check()
# Print results
# foreach ($transfer in $transferResult.Transfers)
#{
# Write-Host "Upload of $($transfer.FileName) succeeded"
#}
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
#exit 1
}
finally
{
$session.Dispose()
}
#exit 0
Write-Host "Sleeping..."
Start-Sleep -Seconds 300
}
[/code]