Skip to main content
E14 Exchange Uber-Geek

Mark E. Smith's Brain Dump

Go Search
Home
Capax Global Blogs
Public Document Library
  

Mark E. Smith's Brain Dump > Posts > Storing and Using Password Credentials
Storing and Using Password Credentials

In Powershell you can use the Get-Credential cmdlet to get alternate logon credentials when you need to perform a task from the shell.  But the Get-Credential cmdlet won't accept a hardcoded password in a script.  So, how do you write a script that needs to run without user intervention and needs to use credentials other than those of the account used to run it?

Well, here is the answer.

First, we need to get our password, then pump it into a file.  Doing this encodes the password and stores it in our output file so no-one can read it.

PS C:\> read-host -assecurestring | convertfrom-securestring | out-file C:\cred.txt

Enter your password on the blank line and hit <CR> this will save the password to c:\cred.txt. Once we have our password safely stored away, we can draw it back into our scripts..

PS C:\> $password = get-content C:\cred.txt | convertto-securestring

Then finally, we can create our credential object, which we pump into other cmdlets.

PS C:\> $credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "myusername",$password

Thanks to Rob Costello for this

Comments

There are no comments yet for this post.