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

s31064

PowerShell Script Password Encryption

I'm posting this because it took me two days to figure out. I needed a script I could run in a room full of backseat drivers. I couldn't hardcode the username and password into the script, and I didn't want to use Read-Host with people looking over my shoulder. This is what I came up with:

$Creds = Get-Credential
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "sftp.url.com"
    UserName = $Creds.UserName
    Password = (ConvertFrom-SecureToPlain -SecurePassword $Creds.Password)
    SshHostKeyFingerprint = "redacted gobbledegook"
}


Works like a charm. I get a username/password popup from Get-Credential where the password shows as *****, and the (ConvertFrom-SecureToPlain -SecurePassword $Creds.Password) converts the password into something the SFTP server understands.

Hope this helps someone.