Hello,
I have this script I use daily, but I want to encrypt the password:
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "10.10.10.34"
UserName = "winscpcopy"
Password = "password123"
SshHostKeyFingerprint = "ssh-ed25519 255 pE/2lITZx7dvFNbg86PzSepRcfEA38tmLE5nvv2kw="
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Transfer files
$session.PutFiles("Y:\file1.csv", "/usr/share/csv/*").Check()
}
finally
{
$session.Dispose()
}
Normally I use this command in PowerShell that creates a hashed password I reference instead:
"password123" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "C:\Scripts\pwdlocal.txt"
Then add this to PowerShell like this
$user = 'windscpcopy'
$file = 'C:\Scripts\pwd.txt'
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, (Get-Content $file | ConvertTo-SecureString)
I'm not sure if the is a way WinSCP can do this or how to add the above to the PowerShell script replacing what is there.
Any help would be most appreciated.