{"id":82645,"date":"2017-12-20T00:01:57","date_gmt":"2017-12-20T08:01:57","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=82645"},"modified":"2019-02-18T09:10:02","modified_gmt":"2019-02-18T16:10:02","slug":"script-wars-the-farce-awakens-part-iii","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/script-wars-the-farce-awakens-part-iii\/","title":{"rendered":"Script Wars: The Farce Awakens (part III)"},"content":{"rendered":"<p><strong>Summary:<\/strong> When last we saw our hero, Rey Skyworker, she was aiding her new co-worker, Tin. They managed to implement better error trapping by using the &#8220;Try Catch&#8221; statement in Windows PowerShell. The fact they could do this so easily it has tickled Tin&#8217;s curiosity.<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>&#8220;Now Rey, I&#8217;m looking at this and as I see it, I could avoid the error altogether by trapping for those situations. Maybe adding in a few IF statements like this to the script.&#8221;<\/p>\n<p><strong>If (($First -or $Last)) -eq &#8221;) then<\/strong><\/p>\n<p><strong>{<\/strong><\/p>\n<p><strong>Write-Output &#8216;Names cannot be blank<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p><strong>Else<\/strong><\/p>\n<p><strong>{<\/strong><\/p>\n<p><strong>\u2026.rest of the script<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p>Rey nodded. &#8220;You mean adding in some data validation?\u00a0 We can definitely do that, but a much better way would be to remove the READ-HOST statements, and use parameters. We can add validation directly to the parameters.&#8221;<\/p>\n<p>She changed the following lines from this:<\/p>\n<p><strong>$First=Read-Host &#8216;Enter First Name&#8217;<\/strong><\/p>\n<p><strong>$Last=Read-Host &#8216;Enter Last Name&#8217;<\/strong><\/p>\n<p>To this:<\/p>\n<p><strong>Param(<\/strong><\/p>\n<p><strong>[String]$First,<\/strong><\/p>\n<p><strong>[String]$Last<\/strong><\/p>\n<p><strong>)<\/strong><\/p>\n<p>Tin scratched his head. &#8220;How would admins run the script? Does it prompt for the information?&#8221;<\/p>\n<p>Rey shook her head. &#8220;First you would run the script in this fashion.&#8221;<\/p>\n<p><strong>.\\NewContosoAzureUser.ps1 -First &#8216;John&#8217; -Last &#8216;Smith&#8217;<\/strong><\/p>\n<p>&#8220;In this setup, you now do create users in bulk, by grabbing data from another source and sending it to the script from the command line.&#8221;<\/p>\n<p>Ben looked down. &#8220;That&#8217;s a great improvement. But in the short term, we have a lot of techs trained on the manual approach. How can this be adapted to meet those needs?&#8221;<\/p>\n<p>&#8220;By using validation on the parameters.&#8221; Rey looked up. &#8220;In this setup, we can define that the values are permitted to be null or not, assign minimum\/maximum lengths, and even specify the object type. In the example above, these objects will default as a [String] type.&#8221;<\/p>\n<p>Rey keyed into the PowerShell console the following line, to show how to leverage parameters in an Advanced function.<\/p>\n<p><strong>Get-Help About_Functions_Advanced_Parameters <\/strong><\/p>\n<p>Drilling through the content, she found a good example of what she was looking for. &#8220;Ah! Here we go. The ability to ensure the parameter cannot be empty or null.&#8221;<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/09-script-wars-12122017.png\"><img decoding=\"async\" width=\"1024\" height=\"475\" class=\"alignnone size-large wp-image-82655\" alt=\"Screenshot of PowerShell\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/09-script-wars-12122017-1024x475.png\" \/><\/a><\/p>\n<p>Tin looked over. &#8220;Hang on, that seems too easy. If I just do this change to the code.&#8221;<\/p>\n<p>Rey nodded. &#8220;It actually is. So first, if we just want to ensure that $First is never a blank and is a required value, we would add in this.&#8221;<\/p>\n<p>She altered the beginning block of code from this:<\/p>\n<p><strong>Param(<\/strong><\/p>\n<p><strong>[String]$First,<\/strong><\/p>\n<p><strong>[String]$Last<\/strong><\/p>\n<p><strong>)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>To this:<\/p>\n<p><strong>Param(<\/strong><\/p>\n<p><strong>[Parameter(Mandatory=$true)]<\/strong><\/p>\n<p><strong>[ValidateNotNullOrEmpty()]<\/strong><\/p>\n<p><strong>[String]$First,<\/strong><\/p>\n<p><strong>[String]$Last<\/strong><\/p>\n<p><strong>)<\/strong><\/p>\n<p>\u2026And then re-ran his script without providing any values to see the result. They hit enter when it prompted for &#8220;First:&#8221;<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/10-script-wars-12122017.png\"><img decoding=\"async\" width=\"1024\" height=\"108\" class=\"alignnone size-large wp-image-82657\" alt=\"Screenshot of PowerShell\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/10-script-wars-12122017-1024x108.png\" \/><\/a><\/p>\n<p>Tin was jumping up and down excitedly. &#8220;This is too awesome! So if anybody makes a mistake, I can actually TRAP for it BEFORE it tries to use the bad data? Hey, wait, I think I can do the second parameter!&#8221;<\/p>\n<p>Tin added the additional lines for $Last to force it to be required information.<\/p>\n<p><strong>Param(<\/strong><\/p>\n<p><strong>[Parameter(Mandatory=$true)]<\/strong><\/p>\n<p><strong>[ValidateNotNullOrEmpty()]<\/strong><\/p>\n<p><strong>[String]$First,<\/strong><\/p>\n<p><strong>[Parameter(Mandatory=$true)]<\/strong><\/p>\n<p><strong>[ValidateNotNullOrEmpty()]<\/strong><\/p>\n<p><strong>[String]$Last<\/strong><\/p>\n<p><strong>)<\/strong><\/p>\n<p>&#8220;Now hang on\u2026 I just realized something. We do have the occasional account that doesn&#8217;t need a last name. Ones like &#8216;Marketing&#8217; or &#8216;Finance.&#8217; How can we ensure there <em>is<\/em> a value, and allow that one to be not assigned by the user?&#8221;<\/p>\n<p>To identify the solution, Rey scanned through the help from <strong>About_Functions_Advanced_Parameters<\/strong>. &#8220;Ah! Here it is. Remove the &#8216;NotNullOrEmpty&#8217; and replace it with &#8216;AllowEmptyString&#8217;. In this scenario, it will still prompt, but allow the value to be a blank.&#8221;<\/p>\n<p><strong>Param(<\/strong><\/p>\n<p><strong>[Parameter(Mandatory=$true)]<\/strong><\/p>\n<p><strong>[ValidateNotNullOrEmpty()]<\/strong><\/p>\n<p><strong>[String]$First,<\/strong><\/p>\n<p><strong>[Parameter(Mandatory=$true)]<\/strong><\/p>\n<p><strong>[AllowEmptyString()]<\/strong><\/p>\n<p><strong>[String]$Last<\/strong><\/p>\n<p><strong>)<\/strong><\/p>\n<p>They re-ran the script to see the results, supplying a value for $First when prompted, and nothing for $Last.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/11-script-wars-12122017.png\"><img decoding=\"async\" width=\"980\" height=\"123\" class=\"alignnone size-full wp-image-82666\" alt=\"Screenshot of PowerShell\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/11-script-wars-12122017.png\" \/><\/a><\/p>\n<p>Ben looked over. &#8220;Looks like the pair of you will make a great team in this battle to smooth out our processes!&#8221;<\/p>\n<p>Just as Ben finished talking, TB-7 decided to start chomping on Rey&#8217;s shoelaces.<\/p>\n<p>Return tomorrow for another exciting adventure in &#8220;Script Wars.&#8221; There will be more exciting PowerShell coolness, and we hope Rey will get her shoelaces back.<\/p>\n<p><strong>Sean Kearney, Premier Field Engineer<\/strong><\/p>\n<p><strong>Enterprise Services Delivery, Secure Infrastructure<\/strong><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: When last we saw our hero, Rey Skyworker, she was aiding her new co-worker, Tin. They managed to implement better error trapping by using the &#8220;Try Catch&#8221; statement in Windows PowerShell. The fact they could do this so easily it has tickled Tin&#8217;s curiosity. &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- &#8220;Now Rey, I&#8217;m looking at this and as I [&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],"tags":[3,154,45],"class_list":["post-82645","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","tag-scripting-guy","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: When last we saw our hero, Rey Skyworker, she was aiding her new co-worker, Tin. They managed to implement better error trapping by using the &#8220;Try Catch&#8221; statement in Windows PowerShell. The fact they could do this so easily it has tickled Tin&#8217;s curiosity. &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- &#8220;Now Rey, I&#8217;m looking at this and as I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/82645","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=82645"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/82645\/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=82645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=82645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=82645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}