PowerShell session log rotation

Advertisement

cipo80
Joined:
Posts:
4
Location:
Vicenza, Itay

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 = ""
)
 
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
}

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum