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
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 }