Save credentials in powershell to automate processes that require validation

If we need to execute processes against computers outside our domain, we must use get-credential cmdlet and pass its value to those cmdlets that can interpret it.
What if we want to save the credentials to have the process fully automated?
$domain=read-host "Domain (FQDN)?"
$file="cred.$domain.txt"
read-host "Domain\username?"| out-file $file
read-host "Password to encrypt?" -assecurestring | convertfrom-securestring | out-file $file -append
By this way, we save username and encrypted password into a txt file so we can use them later in any script, for example to retrieve logical disk information of a remote computer:
$credentials = get-content ("cred.$domain.txt")
$username=$credentials[0]
$password=$credentials[1]|convertto-securestring
$cred = New-Object System.Management.Automation.PSCredential ($username, $password)
gwmi –computername $computername win32_logicaldisk -filter "drivetype=3" -credential $cred
The cmdlet you need doesn't come with -credential parameter? Launch it as a job:
start-job -argumentlist $computername -credential $cred -scriptblock {
  param($computername)
  #whatever
  }
http://social.technet.microsoft.com/wiki/contents/articles/4546.working-with-passwords-secure-strings-and-credentials-in-windows-powershell.aspx#Best_Practices

Comments