{"id":13721,"date":"2011-06-05T00:01:00","date_gmt":"2011-06-05T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/06\/05\/use-powershell-to-pass-credentials-to-legacy-systems\/"},"modified":"2011-06-05T00:01:00","modified_gmt":"2011-06-05T00:01:00","slug":"use-powershell-to-pass-credentials-to-legacy-systems","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-pass-credentials-to-legacy-systems\/","title":{"rendered":"Use PowerShell to Pass Credentials to Legacy Systems"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Windows PowerShell MVP, Sean Kearney, teaches how to use passwords in a script and talk to legacy systems.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, here. This week we will have one guest blogger for the entire week. Sean Kearney has written a series of blog posts about Windows PowerShell and the Legacy. I am not going to be redundant by reposting <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/04\/25\/expert-solution-for-2011-scripting-games-beginner-event-6-parse-the-windows-update-log-for-errors-with-powershell.aspx\">his biography<\/a> each and every day. <\/p>\n<p>Take it away, Sean. <\/p>\n<p>First off&mdash;this is NOT a good idea&hellip;<\/p>\n<p>Leaving clear passwords in a script of any kind is about as bad as&nbsp;an idea can be. Except for maybe writing your PIN number on your bank machine card, or writing your network password on a piece of paper and taping it to your monitor&hellip;you get the idea. <\/p>\n<p>This having been said, there are situations where it happens, especially when you need to manage legacy setups. In fact, this is a very common question that I receive when I talk to users. <\/p>\n<p>Many old VBScript scripts and console application .bat files or CDMs will already be doing this. You may have to pass these to a Windows PowerShell script&mdash;or the other way around&mdash;you may be running a Windows PowerShell script and need to pass the credentials down to the legacy application.<\/p>\n<p>Here&rsquo;s example one. You&rsquo;ve got a Windows PowerShell script that gets credentials as per normal:<\/p>\n<p class=\"MsoNormal\" style=\"padding-left: 30px\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$MyCredentials=GET-CREDENTIAL &ndash;credential CONTOSO\\UberSecretUserName<\/span><\/span><\/span><\/p>\n<p>And of course, as normal, you will see Windows PowerShell pop up the old familiar box shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5468.hsg-6-5-11-1_2CA11FC4.jpg\"><img decoding=\"async\" height=\"322\" width=\"404\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5481.hsg-6-5-11-1_thumb_1E62A6D4.jpg\" alt=\"Image of credential request\" border=\"0\" title=\"Image of credential request\" style=\"padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px\" \/><\/a><\/p>\n<p>You would key in the password as normal, which gives you a nice variable to pass into various cmdlets that require secure credentials.<\/p>\n<p>Now here is a thought&hellip;<\/p>\n<p>Let&rsquo;s pretend that you need to pass those same credentials to a legacy VBScript script or CDM (or something else from the console world). Can you get the password out of this? Normally, you would type the variable on the screen and get output similar to the following. <\/p>\n<p class=\"MsoNormal\" align=\"left\" style=\"padding-left: 30px\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">PS C:\\&gt; $MyCredentials | format-list<\/span><\/span><\/span><\/p>\n<p><span><\/p>\n<p class=\"MsoNormal\" align=\"left\" style=\"padding-left: 30px\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">UserName : CONTOSO\\UberSecretUserName <br \/>Password : System.Security.SecureString<\/span><\/span><\/p>\n<p><\/span><\/p>\n<p>But the System.Security.SecureString is absolutely useless as a password to a legacy system.<\/p>\n<p>I was playing about, and I ran <b>Get-Member<\/b> on the value and found that there is a method available called <b>GetNetworkCredential<\/b>. Running this method against <b>$MyCredentials<\/b> will reveal the Username, Password, and Domain as three separate, clear text values:<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"padding-left: 30px\">\n<tbody>\n<tr>\n<td width=\"83\" valign=\"top\">\n<p>Username<\/p>\n<\/td>\n<td width=\"276\" valign=\"top\">\n<p>: UberSecretUserName<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"83\" valign=\"top\">\n<p>Password<\/p>\n<\/td>\n<td width=\"276\" valign=\"top\">\n<p>: MySecretPasswordYouShouldNotSee<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"83\" valign=\"top\">\n<p>SecurePassword<\/p>\n<\/td>\n<td width=\"276\" valign=\"top\">\n<p>: System.Security.SecureString<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"83\" valign=\"top\">\n<p>Domain<\/p>\n<\/td>\n<td width=\"276\" valign=\"top\">\n<p>: CONTOSO<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>With this method, I can pull out the username and password like so:<\/p>\n<blockquote>\n<p><span style=\"line-height: normal;list-style-type: disc\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$MyCredentials.GetNetworkCredential().Username <br \/>$MyCredentials.GetNetworkCredential().Password <br \/>$MyCredentials.GetNetworkCredential().DOMAIN<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>With this in clear text, we could pass it directly back to a legacy application. <\/p>\n<p>But now the flipside. Let&rsquo;s pretend we have a legacy application that is already passing user IDs and passwords. You need to call a cmdlet in Windows PowerShell from a legacy setup. But it requires the output of <b>Get-Credential<\/b>. What to do?<\/p>\n<p>This is not an issue. If you ran <b>Get-Member<\/b> against the <b>$MyCredentials<\/b> variable, you would see the class that it&rsquo;s associated with in .NET as shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0675.hsg-6-5-11-2_5A223F87.jpg\"><img decoding=\"async\" height=\"94\" width=\"404\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5074.hsg-6-5-11-2_thumb_2515B057.jpg\" alt=\"Image of command output\" border=\"0\" title=\"Image of command output\" style=\"padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px\" \/><\/a><\/p>\n<p>If you go to msdn.microsoft.com, you&rsquo;ll see that to make this, you have to re-create the object by specifying the class and the values it needs.<\/p>\n<p class=\"MsoNormal\" style=\"padding-left: 30px\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$MyDomain=&rsquo;CONTOSO&rsquo; <br \/>$MyClearTextUsername=&rsquo;SecretAgentMan&rsquo; <br \/>$MyClearTextPassword=&rsquo;OhNoTheWholeWorldCanSeeMyPassword&rsquo;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"padding-left: 30px\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$MyUsernameDomain=$MyDomain+&rsquo;\\&rsquo;+$MyClearTextUsername<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"padding-left: 30px\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$SecurePassword=Converto-SecureString &ndash;String $MyClearTextPassword &ndash;AsPlainText &ndash;force<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"padding-left: 30px\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$CredentialsMadeFromClearText=New-object System.Management.Automation $MyUsernameDomain $SecurePassword<\/span><\/span><\/span><span><\/span><\/p>\n<p>Now you can build that needed secure credential in Windows PowerShell from the legacy environment.&nbsp;&nbsp; <\/p>\n<p>There are far more secure ways to get this done. But the important point is knowing how to get the two worlds to happily coexist.<\/p>\n<p>The Power of Shell is in you.<\/p>\n<p>Guest blogger week will continue tomorrow when Sean will continue to talk about Windows PowerShell and the Legacy. A special thank you to Sean for writing this week&rsquo;s blog posts. Hope you enjoy them.<\/p>\n<p>I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Windows PowerShell MVP, Sean Kearney, teaches how to use passwords in a script and talk to legacy systems. Microsoft Scripting Guy, Ed Wilson, here. This week we will have one guest blogger for the entire week. Sean Kearney has written a series of blog posts about Windows PowerShell and the Legacy. I am [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,56,2,3,4,154,45],"class_list":["post-13721","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-guest-blogger","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Windows PowerShell MVP, Sean Kearney, teaches how to use passwords in a script and talk to legacy systems. Microsoft Scripting Guy, Ed Wilson, here. This week we will have one guest blogger for the entire week. Sean Kearney has written a series of blog posts about Windows PowerShell and the Legacy. I am [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13721","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=13721"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13721\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=13721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=13721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=13721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}