How can I encrypt the password if using in PowerShell?

Advertisement

g0nz0uk
Joined:
Posts:
7

How can I encrypt the password if using in PowerShell?

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.

Reply with quote

Advertisement

g0nz0uk
Joined:
Posts:
7

Re: How can I encrypt the password if using in powershell?

Hello Martin,

I’ve not had much luck getting that to work, I’m getting confused with the Microsoft document I think.

Would you have the time to look at my config and give an example on how you would configure it?

I’m going to try again later today and hopefully crack this.

Reply with quote

Advertisement

You can post new topics in this forum