Thank you Martin!!
- cipo80
Logging\LogMaxSize
configuration option:
$session.AddRawConfiguration("Logging\LogMaxSize", "1048576");
$Session.SessionLogPath
to write a log session and it's working, but the file increase daily.
param (
$localPath = "",
$remotePath = "",
$backupPath = "",
$winSCPPath = "C:\Program Files (x86)\WinSCP\WinSCPnet.dll",
$logPath = ""
)
try
{
# Load WinSCP .NET assembly
Add-Type -Path $winSCPPath
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = ""
UserName = ""
Password = ""
SshHostKeyFingerprint = ""
}
$session = New-Object WinSCP.Session
# Log
$Session.SessionLogPath = $logPath
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 $($transfer.FileName) succeeded, moving to backup"
# Upload succeeded, move source file to backup
Move-Item $transfer.FileName $backupPath
}
else
{
Write-Host "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}