Post a reply

Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)


Topic review


Thank you Martin!!

PowerShell session log rotation

Hi all,

just implemented the PS script provided by "the great" Martin/WinSCP portal to move files on local folder after uploaded and working very good.
I've insert only a $Session.SessionLogPath to write a log session and it's working, but the file increase daily.
I want to set a maximum log file size and a rotation to keep the last 7 files, is it possible?

Thank you

param (
    $localPath = "",
    $remotePath = "",
    $backupPath = "",
    $winSCPPath = "C:\Program Files (x86)\WinSCP\WinSCPnet.dll",
    $logPath = ""
    # 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
        # Connect
        # 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
                Write-Host "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
        # Disconnect, clean up
    exit 0
    Write-Host "Error: $($_.Exception.Message)"
    exit 1