VMware Cloud Community
Hypnotoad
Contributor
Contributor
Jump to solution

store encrypted password for PowerShell scripts

Is there a way to  store the password for the Commect-VIServer cmdlet in a file? I tried using the ConvertTo-SecureString cmdlet but wasn't able to get it to work.

$vpass = ConvertTo-SecureString (Get-Content C:\scripts\vpassword.txt)
Connect-VIServer -Server vc.domain.com -Protocol https -User user -Password $vpass

Thanks,

Toad

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You are missing some steps.

Provided you created the file this way

$MyPswd = "xyz1234" 
ConvertTo-SecureString $MyPswd -AsPlainText -Force | `
ConvertFrom-SecureString | Out-File -FilePath "C:\pass.txt"

You can convert it back like this

$pswdSec = Get-Content "C:\pass.txt" | ConvertTo-SecureString 
$bPswd
= [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pswdSec) $pswd = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bPswd) $pswd

Note that you have to create and use the password with the same account !


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

You are missing some steps.

Provided you created the file this way

$MyPswd = "xyz1234" 
ConvertTo-SecureString $MyPswd -AsPlainText -Force | `
ConvertFrom-SecureString | Out-File -FilePath "C:\pass.txt"

You can convert it back like this

$pswdSec = Get-Content "C:\pass.txt" | ConvertTo-SecureString 
$bPswd
= [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pswdSec) $pswd = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bPswd) $pswd

Note that you have to create and use the password with the same account !


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Hypnotoad
Contributor
Contributor
Jump to solution

Thanks much for that. It worked perfectly. 

Toad

0 Kudos