{"id":81235,"date":"2016-12-25T00:01:57","date_gmt":"2016-12-25T08:01:57","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=81235"},"modified":"2019-02-18T09:10:16","modified_gmt":"2019-02-18T16:10:16","slug":"curly-blue-and-the-meaning-of-scripting-part-5","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/curly-blue-and-the-meaning-of-scripting-part-5\/","title":{"rendered":"Curly Blue and the meaning of scripting \u2013 Part 5"},"content":{"rendered":"<p><strong>Summary<\/strong>: Curly Blue learns the true meaning of scripting in PowerShell.<\/p>\n<p>When we last saw our hero, Curly Blue, he had discovered not only the ease of use of PowerShell but also the vast community resources provided by Github.<\/p>\n<p>Because of this one reason alone, his co-workers, Loopy and Codestock, had no need to fret over the upcoming holiday weekend. Their task was solved in a quick and cost-effective manner.<\/p>\n<p>Curly sat down to reflect upon his past 24 hours of \u201cintensive PowerShell training\u201d with his co-workers.<\/p>\n<p>\u201cAt first I came in and was apprehensive about scripting. I personally didn\u2019t find it useful due to my unusual set of skills. Because I could do everything so quickly by using keyboard shortcuts and fast typing for data entry, the need didn\u2019t come up before.\u201d<\/p>\n<p>\u201cHowever,\u201d Linux pointed out, \u201cyour skills, although strong, didn\u2019t easily scale. This is where automation came into play.\u201d<\/p>\n<p>Curly nodded. \u201cAnd, as Azura showed me, there were many tools in Windows that I could use to get good working examples of Windows PowerShell from the GUI itself!\u201d<\/p>\n<p>Hermie nodded emphatically to this last one. \u201cI\u2019m surprised each day just how much of my work I can replicate in PowerShell thanks to that one feature alone! I used those sample cmdlets to build a <em>New User<\/em> script the other day!\u201d<\/p>\n<p>\u201cI thought you said onboarding a new user was a one hour task,\u201d Linux looked quizzically over at Hermie.<\/p>\n<p>\u201cEr \u2026 well yes\u2026. It <u>used<\/u> to be, \u201c he shyly acknowledged. I spent that last hour figuring out how to turn the cmdlets from the Active Directory Administrative Console (ADAC) into a simple script. Now it\u2019s a no brainer!\u201d<\/p>\n<p>\u201cDon\u2019t forget about the Common Information Model (CIM) cmdlets and the massive wealth of inventory information they hold as well!\u201d she chimed in. \u201cThe serial number was just scratching the surface as you can pull free drive space and physical ram configurations from workstations in a very similar manner.\u201d<\/p>\n<p>Curly sat there just imagining how much he could do with PowerShell in this way. Never needing to run to a system, especially remote systems in distant geographic locations.<\/p>\n<p>\u201cAnd also, thanks to that PowerShell script that you found on GitHub, we all get to go home early tonight!\u201d squeeked out Loopy and Codestock in a sound so shrill everyone in the room just winced!<\/p>\n<p>The whole team smiled realizing the mid-afternoon had barely started when Linux noted something critical. \u201cI think the boss already left for the weekend. Shall we invoke the \u2018Tail Light Rule\u2019?<\/p>\n<p>With a thundering of footsteps, you would have thought somebody said that tonight was \u2018Free PowerShell sticker\u2019 night at a user group. They had all disappeared for that holiday weekend with their families.<\/p>\n<h2>Epilogue<\/h2>\n<p>The long holiday weekend has completed. As the newest person to the IT Department, Curly was tasked with that most daunting of tasks. He had to unlock every user who had forgotten his or her own passwords in Active Directory after a long restful weekend.<\/p>\n<p>It appears some people got a bit too restful.<\/p>\n<p>Curly Immediately opened the ADAC, reset the password for one account, and was ready for a long morning of headache. He then remembered that trick he was shown to see how it was done in PowerShell. He noted the line that ran in the PowerShell window of ADAC.<\/p>\n<p style=\"padding-left: 60px\"><code>Set-ADAccountPassword -Identity:\"CN=Ernest DeRoche,OU=Users,DC=CONTOSO,DC=COM\" -NewPassword:\"System.Security.SecureString\" -Reset:$true -Server:\"ContosoDC01.Contoso.Com\"<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>Set-ADUser -ChangePasswordAtLogon:$true -Identity:\" CN=Ernest DeRoche,OU=Users,DC=CONTOSO,DC=COM \" -Server:\" ContosoDC01.Contoso.Com \"<\/code><\/p>\n<p>He quickly pinged Linux to get a little help, having learned a long time ago that when you work as a team you can perform tasks quickly. He set up a quick Skype for Business session to show the sample he got from ADAC.<\/p>\n<p>\u201cSo, you\u2019d like to reset a batch of users with a temporary password for the morning? No problem at all. First, we need to set a default password for the morning.\u201d<\/p>\n<p style=\"padding-left: 60px\"><code>$Password=\u2019T00muchCh33r\u2019<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>$SecurePassword=ConvertTo-SecureString -asplaintext -force $Password<\/code><\/p>\n<p>\u201cNext we\u2019ll need a list of those locked-out accounts.\u201d<\/p>\n<p>Linux showed him a line in PowerShell to get a list of locked-out accounts in Active Directory.<\/p>\n<p style=\"padding-left: 60px\"><code>$Locked=Search-ADAccount-LockedOut<\/code><\/p>\n<p>\u201cWe can take this list and step through it with a <code>Foreach<\/code> statement in PowerShell like this,\u201d Linux said as he typed the example in the chat window.<\/p>\n<p style=\"padding-left: 60px\"><code>Foreach ( $User in $Locked )\n{\n$User\n}<\/code><\/p>\n<p>I can now take your sample code and paste it into the PowerShell loop.<\/p>\n<p style=\"padding-left: 60px\"><code>$Password=\u2019T00muchCh33r\u2019\n$SecurePassword=ConvertTo-SecureString -asplaintext -force $Password\n$Locked=Search-ADAccount-LockedOut\nForeach ( $User in $Locked )\n{\nSet-ADAccountPassword -Identity:\"CN=Ernest DeRoche,OU=Users,DC=CONTOSO,DC=COM\" -NewPassword:\"System.Security.SecureString\" -Reset:$true -Server:\"ContosoDC01.Contoso.Com\"\nSet-ADUser -ChangePasswordAtLogon:$true -Identity:\" CN=Ernest DeRoche,OU=Users,DC=CONTOSO,DC=COM \" -Server:\" ContosoDC01.Contoso.Com \"\n}<\/code><\/p>\n<p>\u201cWe then replace the stuff after <code>-identity<\/code> with a reference to the user\u2019s identity which appears to be the DistinguishedName in Active Directory. We\u2019ll also insert the PowerShell object for <code>$SecurePassword<\/code> after <code>Password<\/code>.<\/p>\n<p style=\"padding-left: 60px\"><code>$Password=\u2019T00muchCh33r\u2019\n$SecurePassword=ConvertTo-SecureString -asplaintext -force $Password\n$Locked=Search-ADAccount-LockedOut\nForeach ( $User in $Locked )\n{\nSet-ADAccountPassword -Identity:$User.DistinguishedName -NewPassword:$SecurePassword -Reset:$true -Server:\"ContosoDC01.Contoso.Com\"\nSet-ADUser -ChangePasswordAtLogon:$true -Identity:$User.DistinguishedName -Server:\" ContosoDC01.Contoso.Com \"\n}<\/code><\/p>\n<p>Curly ran the new script that he had created thanks to the assistance of his buddy, Linux. Within moments, the only thing he had to do was smile and communicate the new temporary password to the locked-out staff.<\/p>\n<p>He had truly discovered the true meaning of scripting!<\/p>\n<p>Happy Holidays to everyone from Hey, Scripting Guy! We hope you and yours enjoy the time with your loved ones.<\/p>\n<p>I invite you to follow the Scripting Guys 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 them at <a target=\"_blank\" 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.<\/p>\n<p>Until then, always remember that with Great PowerShell comes Great Responsibility.<\/p>\n<p><strong>Sean Kearney\n<\/strong>Honorary Scripting Guy\nCloud and Datacenter Management MVP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Curly Blue learns the true meaning of scripting in PowerShell. When we last saw our hero, Curly Blue, he had discovered not only the ease of use of PowerShell but also the vast community resources provided by Github. Because of this one reason alone, his co-workers, Loopy and Codestock, had no need to fret [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[568,685,641],"tags":[56,154,45],"class_list":["post-81235","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-scripting-techniques","category-windows-powershell","tag-guest-blogger","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Curly Blue learns the true meaning of scripting in PowerShell. When we last saw our hero, Curly Blue, he had discovered not only the ease of use of PowerShell but also the vast community resources provided by Github. Because of this one reason alone, his co-workers, Loopy and Codestock, had no need to fret [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/81235","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=81235"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/81235\/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=81235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=81235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=81235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}