Post a reply

Options
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)

Options

Topic review

nezar

Re: how to add logging & use private key option in power shell script

Hi Martin,

Basically, I choose to use the $session.SessionLogPath option.
How can I set the roll-over and size of the log file with this option?
nezar

Re: how to add logging & use private key option in power shell script

Hi Martin,

Basically I want to log the WinSCP session. To log the file transfer activity.
nezar

how to add logging & use private key option in power shell script

Thanks for your reply.
In addition, is there an option to control the log file size & roll-over?
Furthermore, can I use a private key instead of clear username & password?
MARTiN95

For logging:
Put this at the first 3 lines of your script.
$filedate = Get-Date -f yyyy-MM-dd
Start-Transcript -Path "C:\Log\Log-$Filedate.txt
Cls
¨
This will create a file at the given path called Log-$filedate
Where $Filedate translates into current date in the yyyy-mm-dd format
nezar

How to add logging & use private key option in PowerShell script

Hello,

I am using the below PowerShell script for Moving local files to different location after successful upload.
I need to modify it so that it would create a log file and use a private key instead of clear username & password.

What parameters should I use?
Do you have any example?
I am using WinSCP 5.13.4
param (
    $localPath = "C:\temp\upload\*",
    $remotePath = "/test/",
    $backupPath = "C:\temp\backup\"
)
 
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "IP_Address"
        UserName = "username"
        Password = "password"
        SshHostKeyFingerprint = "ssh-rsa 2048 dxV8kkFWYK4ofTfQdquK1f+sMrvA90BOQ3f7tEm+3k9o="
    }
 
    $session = New-Object WinSCP.Session
 
    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
}