PowerCLI

 View Only
  • 1.  store encrypted password for PowerShell scripts

    Posted Jan 27, 2011 11:28 PM

    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



  • 2.  RE: store encrypted password for PowerShell scripts
    Best Answer

    Posted Jan 28, 2011 07:48 AM

    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 !



  • 3.  RE: store encrypted password for PowerShell scripts

    Posted Jan 28, 2011 07:39 PM

    Thanks much for that. It worked perfectly. 

    Toad