document.write('\
<div class="poshcode code posh">\
<style type="text/css">\
');
document.write("/**\
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann\
 * (http://qbnz.com/highlighter/ and http://geshi.org/)\
 */\
.posh .de1, .posh .de2 {font: normal normal 1em/1.2em Consolas, 'Bitstream Vera Sans Mono', 'Courier New', monospace; margin:0; padding:0; background:none; vertical-align:top;}\
.posh  {font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', monospace;}\
.posh .imp {font-weight: bold; color: red;}\
.posh li, .posh .li1 {background: #ffffff;}\
.posh .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}\
.posh .li2 {background: #eeeeee;}\
.posh .kw1 {color: #666699; font-weight: bold;}\
.posh .kw2 {color: #333399; font-weight: bold; font-style: italic;}\
.posh .kw3 {color: #003366; font-weight: bold;}\
.posh .kw4 {color: #660033;}\
.posh .co1 {color: #666666; font-style: italic;}\
.posh .coMULTI {color: #666666; font-style: italic;}\
.posh .es0 {color: #000099; font-weight: bold;}\
.posh .br0 {color: #333;}\
.posh .sy0 {color: #66cc66;}\
.posh .st0 {color: #009900;}\
.posh .nu0 {color: #cc66cc;}\
.posh .me1 {color: #003366;}\
.posh .me2 {color: #003366;}\
.posh .re0 {color: #0066cc; font-style: italic;}\
.posh .re1 {font-style: normal;}\
.posh .re2 {color: #000066;}\
.posh .re3 {color: #660033; font-weight: bold;}\
.posh .re4 {color: #003366; font-weight: bold;}\
.posh span.xtra { display:block; }\
");
document.write(".posh ol {float:left; margin: 0px !important; padding: 0px; min-width:100% !important; list-style-position: inside;}\
.posh li.li2, .posh li.li1 {list-style-type:decimal-leading-zero; white-space:nowrap; padding:0 1em 0 0.5em;}\
.posh li {color: #ccc;}\
.posh li div {color: #000; display: inline;}\
.syntax {background:none repeat scroll 0 0 #eee; border:0.5em solid #ccc;}\
.syntax .posh {overflow: auto;}\
.syntax .poshcredit {\
background-color:#eaeaea; color:#666;\
padding:0.5em 1em; clear:both;\
font-size:85%;\
}");
document.write("\n</style>");
document.write('<div class="syntax"><div class="posh" style="font-family: Consolas, \'Bitstream Vera Sans Mono\', \'Courier New\', monospace;"><ol><li class="li1"><div class="de1"><span class="co1"># Author: &nbsp; &nbsp; &nbsp; Hal Rottenberg &lt;hal@halr9000.com&gt;</span></div></li>\
<li class="li2"><div class="de2"><span class="co1"># Url:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; http://halr9000.com/article/tag/lib-authentication.ps1</span></div></li>\
<li class="li1"><div class="de1"><span class="co1"># Purpose:&nbsp; &nbsp; &nbsp; These functions allow one to easily save network credentials to disk in a relatively</span></div></li>\
<li class="li2"><div class="de2"><span class="co1"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; secure manner. &nbsp;The resulting on-disk credential file can only [1] be decrypted</span></div></li>\
<li class="li1"><div class="de1"><span class="co1"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; by the same user account which performed the encryption. &nbsp;For more details, see</span></div></li>\
<li class="li2"><div class="de2"><span class="co1"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the help files for ConvertFrom-SecureString and ConvertTo-SecureString as well as</span></div></li>\
<li class="li1"><div class="de1"><span class="co1"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MSDN pages about Windows Data Protection API.</span></div></li>\
<li class="li2"><div class="de2"><span class="co1"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1]: So far as I know today. &nbsp;Next week I\'m sure a script kiddie will break it.</span></div></li>\
<li class="li1"><div class="de1"><span class="co1">#</span></div></li>\
<li class="li2"><div class="de2"><span class="co1"># Usage:&nbsp; &nbsp; &nbsp; &nbsp; Export-PSCredential [-Credential &lt;PSCredential object&gt;] [-Path &lt;file to export&gt;]</span></div></li>\
<li class="li1"><div class="de1"><span class="co1"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Export-PSCredential [-Credential &lt;username&gt;] [-Path &lt;file to export&gt;]</span></div></li>\
<li class="li2"><div class="de2"><span class="co1"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If Credential is not specififed, user is prompted by Get-Credential cmdlet.</span></div></li>\
<li class="li1"><div class="de1"><span class="co1"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If a username is specified, then Get-Credential will prompt for password.</span></div></li>\
<li class="li2"><div class="de2"><span class="co1"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If the Path is not specififed, it will default to &quot;./credentials.enc.xml&quot;.</span></div></li>\
<li class="li1"><div class="de1"><span class="co1"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Output: FileInfo object referring to saved credentials</span></div></li>\
<li class="li2"><div class="de2"><span class="co1">#</span></div></li>\
<li class="li1"><div class="de1"><span class="co1"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Import-PSCredential [-Path &lt;file to import&gt;]</span></div></li>\
<li class="li2"><div class="de2"><span class="co1">#</span></div></li>\
<li class="li1"><div class="de1"><span class="co1"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If not specififed, Path is &quot;./credentials.enc.xml&quot;.</span></div></li>\
<li class="li2"><div class="de2"><span class="co1"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Output: PSCredential object</span></div></li>\
<li class="li1"><div class="de1">&nbsp;</div></li>\
<li class="li2"><div class="de2"><span class="kw1">function</span> <span class="re0">Export-<span class="re1">PSCredential</span></span> <span class="br0">&#123;</span></div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">param</span> <span class="br0">&#40;</span> <span class="re3">$Credential</span> <span class="sy0">=</span> <span class="br0">&#40;</span><span class="re0">Get-<span class="re1">Credential</span></span><span class="br0">&#41;</span>, <span class="re3">$Path</span> <span class="sy0">=</span> <span class="st0">&quot;credentials.enc.xml&quot;</span> <span class="br0">&#41;</span></div></li>\
<li class="li2"><div class="de2">&nbsp;</div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1"># Look at the object type of the $Credential parameter to determine how to handle it</span></div></li>\
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">switch</span> <span class="br0">&#40;</span> <span class="re3">$Credential</span>.<span class="me1">GetType</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">Name</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1"># It is a credential, so continue</span></div></li>\
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PSCredential&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span> <span class="kw1">continue</span> <span class="br0">&#125;</span></div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1"># It is a string, so use that as the username and prompt for the password</span></div></li>\
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">String</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span> <span class="re3">$Credential</span> <span class="sy0">=</span> <span class="re0">Get-<span class="re1">Credential</span></span> <span class="re2">-credential</span> <span class="re3">$Credential</span> <span class="br0">&#125;</span></div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1"># In all other caess, throw an error and exit</span></div></li>\
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span> <span class="kw1">Throw</span> <span class="st0">&quot;You must specify a credential object to export to disk.&quot;</span> <span class="br0">&#125;</span></div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li>\
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; </div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1"># Create temporary object to be serialized to disk</span></div></li>\
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re3">$export</span> <span class="sy0">=</span> <span class="st0">&quot;&quot;</span> <span class="sy0">|</span> <span class="re0">Select-<span class="re1">Object</span></span> Username, EncryptedPassword</div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div></li>\
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1"># Give object a type name which can be identified later</span></div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re3">$export</span>.<span class="kw3">PSObject</span>.<span class="me1">TypeNames</span>.<span class="me1">Insert</span><span class="br0">&#40;</span><span class="nu0">0</span>,’ExportedPSCredential’<span class="br0">&#41;</span></div></li>\
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; </div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re3">$export</span>.<span class="me1">Username</span> <span class="sy0">=</span> <span class="re3">$Credential</span>.<span class="me1">Username</span></div></li>\
<li class="li2"><div class="de2">&nbsp;</div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1"># Encrypt SecureString password using Data Protection API</span></div></li>\
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1"># Only the current user account can decrypt this cipher</span></div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re3">$export</span>.<span class="me1">EncryptedPassword</span> <span class="sy0">=</span> <span class="re3">$Credential</span>.<span class="me1">Password</span> <span class="sy0">|</span> <span class="re0">ConvertFrom-<span class="re1">SecureString</span></span></div></li>\
<li class="li2"><div class="de2">&nbsp;</div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1"># Export using the Export-Clixml cmdlet</span></div></li>\
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re3">$export</span> <span class="sy0">|</span> <span class="re0">Export-<span class="re1">Clixml</span></span> <span class="re3">$Path</span></div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">Write-<span class="re1">Host</span></span> <span class="re2">-foregroundcolor</span> Green <span class="st0">&quot;Credentials saved to: &quot;</span> <span class="re2">-noNewLine</span></div></li>\
<li class="li2"><div class="de2">&nbsp;</div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1"># Return FileInfo object referring to saved credentials</span></div></li>\
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">Get-<span class="re1">Item</span></span> <span class="re3">$Path</span></div></li>\
<li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li>\
<li class="li2"><div class="de2">&nbsp;</div></li>\
<li class="li1"><div class="de1"><span class="kw1">function</span> <span class="re0">Import-<span class="re1">PSCredential</span></span> <span class="br0">&#123;</span></div></li>\
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">param</span> <span class="br0">&#40;</span> <span class="re3">$Path</span> <span class="sy0">=</span> <span class="st0">&quot;credentials.enc.xml&quot;</span> <span class="br0">&#41;</span></div></li>\
<li class="li1"><div class="de1">&nbsp;</div></li>\
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1"># Import credential file</span></div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re3">$import</span> <span class="sy0">=</span> <span class="re0">Import-<span class="re1">Clixml</span></span> <span class="re3">$Path</span> </div></li>\
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; </div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1"># Test for valid import</span></div></li>\
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> <span class="sy0">!</span><span class="re3">$import</span>.<span class="me1">UserName</span> <span class="re2">-or</span> <span class="sy0">!</span><span class="re3">$import</span>.<span class="me1">EncryptedPassword</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">Throw</span> <span class="st0">&quot;Input is not a valid ExportedPSCredential object, exiting.&quot;</span></div></li>\
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re3">$Username</span> <span class="sy0">=</span> <span class="re3">$import</span>.<span class="me1">Username</span></div></li>\
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; </div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1"># Decrypt the password and store as a SecureString object for safekeeping</span></div></li>\
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re3">$SecurePass</span> <span class="sy0">=</span> <span class="re3">$import</span>.<span class="me1">EncryptedPassword</span> <span class="sy0">|</span> <span class="re0">ConvertTo-<span class="re1">SecureString</span></span></div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div></li>\
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1"># Build the new credential object</span></div></li>\
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re3">$Credential</span> <span class="sy0">=</span> <span class="re0">New-<span class="re1">Object</span></span> <span class="kw3">System</span>.<span class="me1">Management</span>.<span class="me1">Automation</span>.<span class="me1">PSCredential</span> <span class="re3">$Username</span>, <span class="re3">$SecurePass</span></div></li>\
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">Write-<span class="re1">Output</span></span> <span class="re3">$Credential</span></div></li>\
<li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li>\
</ol></div>');document.write('<div class="poshcredit"><a style="float:right;" href="http://poshcode.org/get/474" title="download file">download</a><a href="http://poshcode.org/?show=474" title="full view">This Script</a> brought to you by <a href="http://PoshCode.org">PoshCode</a>\
</div>\
</div>\
</div>');
