Differences
This shows you the differences between the selected revisions of the page.
script_local_move_after_successful_upload 2015-12-17 | script_local_move_after_successful_upload 2023-06-05 (current) | ||
Line 17: | Line 17: | ||
# Setup session options | # Setup session options | ||
- | $sessionOptions = New-Object WinSCP.SessionOptions | + | $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ |
- | ···$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp | + | ········Protocol = [WinSCP.Protocol]::Sftp |
- | $sessionOptions.HostName = "example.com" | + | ·······HostName = "example.com" |
- | $sessionOptions.UserName = "user" | + | ·······UserName = "user" |
- | $sessionOptions.Password = "mypassword" | + | ·······Password = "mypassword" |
- | $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | ·······SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
+ | } | ||
$session = New-Object WinSCP.Session | $session = New-Object WinSCP.Session | ||
Line 40: | Line 41: | ||
if ($transfer.Error -eq $Null) | if ($transfer.Error -eq $Null) | ||
{ | { | ||
- | Write-Host ("Upload of {0} succeeded, moving to backup" -f | + | Write-Host "Upload of $($transfer.FileName) succeeded, moving to backup" |
- | $transfer.FileName) | + | |
# Upload succeeded, move source file to backup | # Upload succeeded, move source file to backup | ||
Move-Item $transfer.FileName $backupPath | Move-Item $transfer.FileName $backupPath | ||
Line 47: | Line 47: | ||
else | else | ||
{ | { | ||
- | Write-Host ("Upload of {0} failed: {1}" -f | + | Write-Host "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)" |
- | ···················$transfer.FileName, $transfer.Error.Message) | + | |
} | } | ||
} | } | ||
Line 60: | Line 59: | ||
exit 0 | exit 0 | ||
} | } | ||
- | catch [Exception] | + | catch |
{ | { | ||
- | Write-Host $_.Exception.Message | + | Write-Host "Error: $($_.Exception.Message)" |
exit 1 | exit 1 | ||
} | } | ||
</code> | </code> | ||
- | ===== Using WinSCP Scripting ===== | + | ===== [[scripting]] Using WinSCP Scripting ===== |
WinSCP scripting does not support move command for local files. Instead you can combine WinSCP script with batch file: | WinSCP scripting does not support move command for local files. Instead you can combine WinSCP script with batch file: | ||
Line 75: | Line 74: | ||
# Upload the files | # Upload the files | ||
put *.* | put *.* | ||
+ | # Exit WinSCP | ||
+ | exit | ||
</code> | </code> | ||
Line 85: | Line 86: | ||
echo Upload succeeded, moving local files | echo Upload succeeded, moving local files | ||
move *.* c:\backup\ | move *.* c:\backup\ | ||
- | exit 0 | + | exit /b 0 |
:error | :error | ||
echo Upload failed, keeping local files | echo Upload failed, keeping local files | ||
- | exit 1 | + | exit /b 1 |
</code> | </code> | ||
+ | |||
+ | Contrary to the PowerShell script above, this solution is not transactionally safe. If new files appear in the local folder between times the upload starts and the archiving/moving starts, the new files will not get uploaded. For this reason, prefer using the PowerShell script. | ||
+ | |||
+ | Another option is to move the files to an intermediate/temporary folder first, upload from there, and then move them to the backup location. | ||
===== Further Reading ===== | ===== Further Reading ===== |