{"id":18001,"date":"2010-06-20T00:01:00","date_gmt":"2010-06-20T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/06\/20\/hey-scripting-guy-weekend-scripter-more-fruity-fun-with-windows-powershell-enumerations\/"},"modified":"2010-06-20T00:01:00","modified_gmt":"2010-06-20T00:01:00","slug":"hey-scripting-guy-weekend-scripter-more-fruity-fun-with-windows-powershell-enumerations","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-weekend-scripter-more-fruity-fun-with-windows-powershell-enumerations\/","title":{"rendered":"Hey, Scripting Guy! Weekend Scripter: More Fruity Fun with Windows PowerShell Enumerations"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><!-- AddThis Button BEGIN --><\/p>\n<p>\n<!--\nvar addthis_config = {\"data_track_clickback\":true};\n\/\/ -->\n<a href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;username=scriptingguys\" class=\"addthis_button\"><img decoding=\"async\" height=\"16\" width=\"125\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" alt=\"Bookmark and Share\" style=\"border:0\" \/><\/a><\/p>\n<p><!-- AddThis Button END --><\/p>\n<p>&nbsp;<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. The neighbor&rsquo;s dog never seems to sleep. It is up early this morning, singing for its breakfast. I have news for the creature: Its voice is not nearly as great as he thinks it is. Oh, well, for these past several days the hound has been a reliable alarm clock. I only wish his owner could figure out a way to adjust the time for the weekend. As long as I am up, I might as well make a pot of English Breakfast tea and get back to work on my enumeration project. <\/p>\n<p>After I wrote the Weekend Scripter article <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/06\/19\/hey-scripting-guy-weekend-scripter-using-enumerations-in-scripts.aspx\">yesterday<\/a>, I decided to try an experiment to see if I could make the code a bit more efficient. <\/p>\n<p>Instead of using an <strong>If<\/strong> construction and a <strong>contains<\/strong> statement such as I used yesterday, could I use a <strong>type<\/strong> constraint in the <strong>Param<\/strong> section for the function? If I supply a valid <strong>enum<\/strong> value to the parameter with the <strong>type<\/strong> constraint, it should allow it to pass. If not, it should throw an error. I decided to give it a try and see what would happen. The result is the Get-FruitEnumDemo.ps1 script that is shown here. <\/p>\n<p><strong>Get-FruitEnumDemo.ps1<\/strong><\/p>\n<p><span style=\"background-color: #e2e2e2\">Function Get-Fruit<\/span><\/p>\n<p><span style=\"background-color: #e2e2e2\">{<\/span><\/p>\n<p><span style=\"background-color: #e2e2e2\">Param([myspace.fruit]$enumvalue)<\/span><\/p>\n<p><span style=\"background-color: #e2e2e2\">switch ([enum]::parse([type]&#8221;myspace.fruit&#8221;,$enumValue))<\/span><\/p>\n<p><span style=\"background-color: #e2e2e2\">{<\/span><\/p>\n<p><span style=\"background-color: #e2e2e2\">([myspace.fruit]::apple) { &#8220;Apples are good&#8221; }<\/span><\/p>\n<p><span style=\"background-color: #e2e2e2\">([myspace.fruit]::pear) { &#8220;Pears are yummy&#8221; }<\/span><\/p>\n<p><span style=\"background-color: #e2e2e2\">([myspace.fruit]::kiwi) { &#8220;Kiwis are great&#8221; }<\/span><\/p>\n<p><span style=\"background-color: #e2e2e2\">} #end switch<\/span><\/p>\n<p><span style=\"background-color: #e2e2e2\">} #end function<\/span><\/p>\n<p><span style=\"background-color: #e2e2e2\">Get-Fruit &#8220;plum&#8221;<\/span><\/p>\n<p>I have used type constraints on input parameters in many of my scripts. Usually, it is limited to a string or to an integer or perhaps a date. Until recently, I have not needed to check a more complex data type.&nbsp;The solution is as easy as using a type constraint for a string or an integer. Put the enumeration name in square brackets. This is shown here:<\/p>\n<p><span style=\"background-color: #e2e2e2\">Param([myspace.fruit]$enumvalue)<\/span><\/p>\n<p>When you call the function with a fruit that is not part of the <strong>myspace.fruit<\/strong> enumeration, an error is generated. This is shown in the following image.<\/p>\n<p><a href=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/june\/hey0620\/wes-06-20-10-01.jpg\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" title=\"Image of error generated when function is called with fruit not part of enumeration\" border=\"0\" alt=\"Image of error generated when function is called with fruit not part of enumeration\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4863.wes062010011_7BB15715.jpg\" width=\"604\" height=\"429\" \/> <\/a><\/p>\n<p>Therefore, if a string is submitted to the <strong>Get-Fruit<\/strong> function that is not one of the enumeration values, an error is generated. The nice thing about the error is that it lists the allowed enumeration values in the third line of the error message when it says, &ldquo;The possible enumeration values are &#8220;apple, pear, kiwi&#8221;.&rdquo;<\/p>\n<p>In addition to supplying the enumeration by string, I can also supply the value by number. When the <strong>Myspace.Fruit<\/strong> enumeration was created a couple weeks ago in <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/06\/06\/hey-scripting-guy-weekend-scripter-the-fruity-bouquet-of-windows-powershell-enumerations.aspx\">a Weekend Scripter post<\/a>, numeric values were assigned to each property. <\/p>\n<p>By checking against an enumeration type, numeric values or string values can be supplied to the parameter. This is shown in the following image.<\/p>\n<p><a href=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/june\/hey0620\/wes-06-20-10-02.jpg\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" title=\"Image of supplying values to parameter\" border=\"0\" alt=\"Image of supplying values to parameter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4073.wes062010021_4155E134.jpg\" width=\"604\" height=\"462\" \/> <\/a><\/p>\n<p>The remainder of the Get-FruitEnumDemo.ps1 script works exactly like the script from <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/06\/19\/hey-scripting-guy-weekend-scripter-using-enumerations-in-scripts.aspx\">yesterday&rsquo;s Weekend Scripter post<\/a>. It uses the <strong>Switch<\/strong> statement to determine what action will be taken for each enumeration value. There is no need for a switch default statement, or an <strong>If<\/strong>\/<strong>Else<\/strong> statement because an invalid enumeration value will not make it past the <strong>param<\/strong> statement. <\/p>\n<p>&nbsp;<\/p>\n<p>Well, that is about it for working with enumerations. I hope you enjoy the rest of your weekend. I think that as long as I am awake, I will head out to my woodworking shop and make some saw dust. <\/p>\n<p>Join us tomorrow when we will begin talking about&hellip;oh, wait a second. If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/strong><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; Microsoft Scripting Guy Ed Wilson here. The neighbor&rsquo;s dog never seems to sleep. It is up early this morning, singing for its breakfast. I have news for the creature: Its voice is not nearly as great as he thinks it is. Oh, well, for these past several days the hound has been a [&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":[148,51,3,61,45],"class_list":["post-18001","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-enum","tag-getting-started","tag-scripting-guy","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; &nbsp; Microsoft Scripting Guy Ed Wilson here. The neighbor&rsquo;s dog never seems to sleep. It is up early this morning, singing for its breakfast. I have news for the creature: Its voice is not nearly as great as he thinks it is. Oh, well, for these past several days the hound has been a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/18001","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=18001"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/18001\/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=18001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=18001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=18001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}