I want to be able to log into VIServers using PowerShell and have it ask for the credentials the first time the script runs, then save those credentials in the password.txt file and have the VIServer just use that password.txt file stored locally on the user's computer if the user runs the script again. The pain point is that the credential prompt keeps popping up again and again while the user wants to run the script several times.
I am able to use the following code from another answer posted here on Stackoverflow (Link: http://www.adminarsenal.com/admin-arsenal-blog/secure-password-with-powershell-encrypting-credentials-part-1)
and it works:
Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString |
Out-File "G:\dev\Password.txt"
$pass = Get-Content "G:\dev\Password.txt" | ConvertTo-SecureString
$User = "MyUserName"
$File = "G:\dev\Password.txt"
$MyCredential = New-Object -TypeName System.Management.Automation.PSCredential
-ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
I found the following from a vmware blog (Link: http://blogs.vmware.com/PowerCLI/2011/11/have-you-seen-powerclis-credential-store-feature.html)
And here is the code from vmware blog(with some explanation):
To use the credential store, I do the following:
New-VICredentialStoreItem -Host 192.168.10.10 -User "Andrey" -Password "my favorite password"
Now I can type just:
Connect-VIServer 192.168.10.10
When I don’t specify user and/or password, Connect-VIServer checks the credential store, finds my newly stored credential and uses it.
By default the credential store file is stored under the user profile directory. It is encrypted. If I got you interested, check “help *VICredentialStoreItem” for details.
-Andrey Anastasov, PowerCLI Architect
=============AND NOW MY MODIFIED VERSION OF THE VIServer code========== $Hostname = 192.168.10.10
New-VICredentialStoreItem -Host $Hostname -User $User -Password $pass
Am I on the right track?
What should I do to type the credentials only 1 time and then just have the script call that $creds variable instead of having to type in the credentials every time?