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

Advertisement

nezar
Joined:
Posts:
5

how to add logging & use private key option in power shell 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
}

Reply with quote

Advertisement

MARTiN95
Joined:
Posts:
5
Location:
Sweden

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

Reply with quote E-mail

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?

Reply with quote

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.

Reply with quote

Advertisement

nezar
Joined:
Posts:
5

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?

Reply with quote

Advertisement

You can post new topics in this forum