Using Get-Credential to Store Passwords “securely” in a file.
Lee Holmes of PowerShell Cookbook fame has a post here Importing and Exporting Credentials in PowerShell. After fielding a few questions on EE regarding this post.. I wrote these two scripts to make it a little simpler.
Set-myCredential: This will prompt you for credentials and store them in the file specified.
#Set-myCredential.ps1
Param($File)
$Credential = Get-Credential
$credential.Password | ConvertFrom-SecureString | Set-Content $File
#####################
Get-myCredential: This will get you credentials from a file specified. It require you know the user name.
#Get-myCredential.ps1
Param($User,$File)
$password = Get-Content $File | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential($user,$password)
$credential
#####################
With these two script you can do something like this (using VMware Toolkit for example.)
$creds = c:\scripts\Get-myCredential.ps1 MyUserName c:\tools\mp.txt
Get-ViServer MyVirtualCenter -cred $creds
tshell :: Jul.03.2008 :: All :: 4 Comments »
