<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Passwords in Scripts Archives - the Sysadmin Channel</title>
	<atom:link href="https://thesysadminchannel.com/tag/passwords-in-scripts/feed/" rel="self" type="application/rss+xml" />
	<link>https://thesysadminchannel.com/tag/passwords-in-scripts/</link>
	<description>Documenting My Life as a System Administrator</description>
	<lastBuildDate>Thu, 03 May 2018 20:00:09 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
<site xmlns="com-wordpress:feed-additions:1">144174110</site>	<item>
		<title>Encrypting Passwords in Scripts:  The Ultimate Best Practice Guide for Powershell</title>
		<link>https://thesysadminchannel.com/passwords-in-scripts-the-ultimate-best-practice-guide/</link>
					<comments>https://thesysadminchannel.com/passwords-in-scripts-the-ultimate-best-practice-guide/#comments</comments>
		
		<dc:creator><![CDATA[Paul Contreras]]></dc:creator>
		<pubDate>Wed, 02 May 2018 04:33:57 +0000</pubDate>
				<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[best practices for scripts]]></category>
		<category><![CDATA[encrypting passwords in powershell]]></category>
		<category><![CDATA[how to put passwords in scripts]]></category>
		<category><![CDATA[Passwords in Scripts]]></category>
		<category><![CDATA[powershell best practices]]></category>
		<guid isPermaLink="false">https://thesysadminchannel.com/?p=431</guid>

					<description><![CDATA[<p>Unencrypted passwords in Scripts?  We all know that it&#8217;s a huge security risk and an overall big no no to have your passwords in plain text.  Sometimes we want to run Scheduled tasks without the need to be standing by&#8230; <a href="https://thesysadminchannel.com/passwords-in-scripts-the-ultimate-best-practice-guide/" class="more-link">Continue Reading <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a href="https://thesysadminchannel.com/passwords-in-scripts-the-ultimate-best-practice-guide/">Encrypting Passwords in Scripts:  The Ultimate Best Practice Guide for Powershell</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Unencrypted passwords in Scripts?  We all know that it&#8217;s a huge security risk and an overall big no no to have your passwords in plain text.  Sometimes we want to run Scheduled tasks without the need to be standing by to enter in the appropriate credentials.  Or if you&#8217;re like me you have the need to connect to Azure or Exchange Online 50 million times a day, but don&#8217;t like having the session or Powershell Window open when not needed. So what are the best practices to having your passwords in your scripts to automatically connect to services?  We&#8217;ll go over that next.</p>
<p>&nbsp;</p>
<h1>Passwords in Scripts for Powershell</h1>
<p>Here is my process for when I want to have my passwords in my scripts. The following we&#8217;ll only need to do one time every 3 months or when I change my password.</p>
<p>First we need to get our Credentials.</p>
<pre class="brush: powershell; title: ; notranslate">

$Creds = Get-Credential

</pre>
<p>Then I create an obscure random folder and export the credentials to that folder via the <code>Export-Clixml</code> cmdlet. So for example I&#8217;ll pick <code>C:\Windows\System32\WindowsPowerShell\v1.0\Modules\SomeFakeModule</code> and export the ps1xml file there.</p>
<pre class="brush: powershell; title: ; notranslate">

$Creds | Export-Clixml C:\Windows\System32\WindowsPowerShell\v1.0\Modules\SomeFakeModule\SomeRandomFilename.ps1xml

</pre>
<p>Now there should be a file <code>SomeRandomFilename.ps1xml</code> in the <code>C:\Windows\System32\WindowsPowerShell\v1.0\Modules\SomeFakeModule</code> folder. Next I&#8217;ll convert that path to a secure string and convert it back from a secure string to get a long random hex character string. That would look something like this..</p>
<pre class="brush: powershell; title: ; notranslate">

'C:\Windows\System32\WindowsPowerShell\v1.0\Modules\SomeFakeModule\SomeRandomFilename.ps1xml' | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File C:\_Scripts\SomeOtherDirectory\AnotherRandomFilename.ps1xml

</pre>
<p>The <code>AnotherRandomFilename.ps1xml</code> should contain something like this.</p>
<pre class="brush: powershell; title: ; notranslate">

01000000d08c9ddf0115d1118c7a00c04fc297eb010000004af7b54b56de384793f6556f9f497f240000000002000000000003660000c0000000100000001a2e14d11a0435bb980161d9141109be0000000004800000a0000000100000008686204fd145b3cb74dbf328cf3dc19d60000000836224a5abf022bff65da641f93be649c221418303c626e626ee299eea9af9028308041a1f364a6294d8c31a8060dd19156f48b8df58f060f5b1025070946d1e390005ef0c0ece3f9127a7830711da7d54665d87f

</pre>
<p>The above is the only part you need to do every time you change your password.</p>
<p>&nbsp;</p>
<h1>The Code to have in your Scripts.</h1>
<p>Below is the part you need to have in your script where you run the script. So for example if i wanted to connect to Azure I would create an Connect-Azure.ps1 script with the following:</p>
<pre class="brush: powershell; title: ; notranslate">

$Module = Get-Content C:\Scripts\SomeOtherDirectory\AnotherRandomFilename.ps1xml | ConvertTo-SecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Module)

Connect-MsolService -Credential ([System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) | Import-Clixml)

</pre>
<p>This should allow you to connect with your &#8216;password&#8217; in the script. I know, it&#8217;s confusing as hell but it&#8217;s that way by design. You can probably get away with the <strong>Export-Clixml</strong> and <strong>Import-Clixml</strong> cmdlets since Powershell can only decrypt the credentials from the same user and computer accounts. In other words, if another user on that same computer tried to import the exported credentials, it will fail. Alternatively, if the same user tried to import the credentials from another computer, it will also fail. Sometimes however, a little extra confusion only makes it harder to reverse engineer what someone is trying to do.</p>
<p>Now you can import the clixml in whatever script you wish. If you want to learn more with online video, take a look at our <a href="https://www.youtube.com/channel/UC9VnUjmZrNG3ithDZmG-S-g" target="_blank" rel="noopener">Youtube Channel</a></p>
<p>The post <a href="https://thesysadminchannel.com/passwords-in-scripts-the-ultimate-best-practice-guide/">Encrypting Passwords in Scripts:  The Ultimate Best Practice Guide for Powershell</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://thesysadminchannel.com/passwords-in-scripts-the-ultimate-best-practice-guide/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">431</post-id>	</item>
	</channel>
</rss>
