Hal Rottenberg has put together a very nice set of functions for exporting and importing PSCredential to a file. The export function takes a PSCredential and serializes it to an XML file. The corresponding import function reconstitutes the PSCredential to an object that you can use. Now before you start freaking out about security, the xml file can only be used by the same user account that created it.
What I like about this is that I can now have multiple credentials easily stored for different computers. I’m no longer tied to using one credential. First I’ll export my credentials:
PS C:\> export-pscredential -path godot.cred.xml
PS C:\> export-pscredential -path jdhit.cred.xml
Now I’ll build an associative array of computernames and their corresponding PSCredential:
PS C:\> $sys=@{“godot”=(import-pscredential .\godot.cred.xml);”jdhit-dc01″=(import-pscredential .\jdhit.cred.xml);”dogtoy”=(import-pscredential .\jdhit.cred.xml)}
PS C:\scripts\PoSH> $sys
Name Value
—- —–
jdhit-dc01 System.Management.Automation.PSCredential
godot System.Management.Automation.PSCredential
dogtoy System.Management.Automation.PSCredential
Here’s an example of how I might use this array:
PS C:\> $sys.keys | ForEach {gwmi win32_computersystem -computer “$_” -cred $sys.item($_)} | Select Name,Manufacturer,Model
Name Manufacturer Model
—- ———— —–
JDHIT-DC01 MICRO-STAR INTERNATIONAL CO., LTD KM400-8235
GODOT Dell Computer Corporation Latitude D800
DOGTOY Dell Computer Corporation Dimension 8300
This is a wicked set of functions that are great for unattended automation.