{"id":18011,"date":"2010-06-19T00:01:00","date_gmt":"2010-06-19T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/06\/19\/hey-scripting-guy-weekend-scripter-using-enumerations-in-scripts\/"},"modified":"2010-06-19T00:01:00","modified_gmt":"2010-06-19T00:01:00","slug":"hey-scripting-guy-weekend-scripter-using-enumerations-in-scripts","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-weekend-scripter-using-enumerations-in-scripts\/","title":{"rendered":"Hey, Scripting Guy! Weekend Scripter: Using Enumerations in Scripts"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><!-- AddThis Button BEGIN --><\/p>\n<p>\n<!--\nvar addthis_config = {\"data_track_clickback\":true};\n\/\/ -->\n<\/p>\n<p><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>Microsoft Scripting Guy Ed Wilson here. The Scripting Wife and I are on the road again. She is driving, and I am playing around with Windows PowerShell enumerations. Teresa made friends with a couple we met in the concierge lounge of our hotel in Lisbon while I was teaching a month&rsquo;s worth of Windows PowerShell workshops. She has remained friends with them ever since, and this weekend we are meeting in Chattanooga, Tennessee, to see the Glen Miller Orchestra. The good thing about having a power adapter in the car is I can write scripts during the entire journey&mdash;from that perspective there is no difference from being at home or on the road. Well, there is one thing that is different: I do not have good Internet access from the car. It is true I can use my Windows Mobile device to search the Internet, and if I tried, I bet I could make it emulate a modem for my laptop, but that is more trouble than it is worth. The cool thing is that Windows PowerShell is largely self-documenting, which means I can use the <strong>Get-Member<\/strong> cmdlet to discover methods and properties of objects. <\/p>\n<p>I have been playing around with enumerations in Windows PowerShell since last weekend, and I decided to see how I would use one in a script. Using the <strong>myspace.fruit<\/strong> enumeration I created last week, I can use an enumeration value to branch out to different parts of a script. I can also check to see if the enumeration is legitimate or not. The Get-FruitEnumDemo_enumContains.ps1 script illustrates this technique. After the <strong>myspace.fruit<\/strong> enumeration has been loaded into memory, the <strong>Get-Fruit<\/strong> function accepts an enumeration value. The <strong>contains<\/strong> operator is used to see if the enumeration exists; a <strong>switch<\/strong> statement performs different actions based upon the enumeration value. If the enumeration does not exist, a message is displayed that states the enumeration is not valid and displays the permissible values. The Get-FruitEnumDemo_enumContains.ps1 script is shown here. <\/p>\n<p><strong>Get-FruitEnumDemo_enumContains.ps1<\/strong><\/p>\n<p><span style=\"background-color: #e3e3e3\">Function Get-Fruit <br \/>{ <br \/>Param($enumvalue) <br \/>if([enum]::Getvalues([myspace.fruit]) -contains $enumValue) <br \/>{ <br \/>switch ([enum]::parse([type]&#8221;myspace.fruit&#8221;,$enumValue)) <br \/>{ <br \/>([myspace.fruit]::apple) { &#8220;Apples are good&#8221; } <br \/>([myspace.fruit]::pear) { &#8220;Pears are yummy&#8221; } <br \/>([myspace.fruit]::kiwi) { &#8220;Kiwis are great&#8221; } <br \/>} #end switch <br \/>} # end if <br \/>else { <br \/>&#8220;Your enumeration is invalid. Please use one of the following values:&#8221; <br \/>[enum]::getValues([myspace.fruit]) <br \/>} <br \/>} #end function <br \/>Get-Fruit &#8220;plum&#8221;<\/span><\/p>\n<p>A string is passed to the <strong>Get-Fruit<\/strong> function via the <strong>$enumvalue<\/strong> parameter. An <strong>if<\/strong> statement first retrieves all permissible values for the enumeration, and then uses the <strong>contains<\/strong> operator to see if the supplied value exists. This is shown here:<\/p>\n<p><span style=\"background-color: #e3e3e3\">Function Get-Fruit <br \/>{ <br \/>Param($enumvalue) <br \/>if([enum]::Getvalues([myspace.fruit]) -contains $enumValue) <br \/>{<\/span><\/p>\n<p>The switch statement evaluates an actual enumeration value, but a string was passed to the function. To correct this, the <strong>parse<\/strong> static method from the <strong>system.enum<\/strong> .NET Framework class is used. It accepts two arguments: the first is an <strong>enum<\/strong> and the second is an <strong>enum<\/strong> value. The string <strong>myspace.fruit<\/strong> is cast into an <strong>enum<\/strong> (or a type) by using the <strong>[type]<\/strong> type constraint:<\/p>\n<p><span style=\"background-color: #e3e3e3\">[enum]::parse([type]&#8221;myspace.fruit&#8221;,$enumValue)<\/span><\/p>\n<p>The <strong>parse<\/strong> method translates the string into an actual instance of the enumeration. This is shown here:<\/p>\n<p><span style=\"background-color: #e3e3e3\">PS C:\\Users\\ed.NWTRADERS&gt; [enum]::parse([type]&#8221;myspace.fruit&#8221;,&#8221;pear&#8221;) <br \/>pear <br \/>PS C:\\Users\\ed.NWTRADERS&gt; [enum]::parse([type]&#8221;myspace.fruit&#8221;,&#8221;pear&#8221;).Gettype() <br \/>IsPublic IsSerial Name BaseType <br \/>&#8212;&#8212;&#8211; &#8212;&#8212;&#8211; &#8212;- &#8212;&#8212;&#8211; <br \/>True True fruit System.Enum<\/span><\/p>\n<p>If you supply a value that is not part of the enumeration, the <strong>parse<\/strong> method throws an error as shown here:<\/p>\n<p><span style=\"background-color: #e3e3e3\">PS C:\\Users\\ed.NWTRADERS&gt; [enum]::parse([type]&#8221;myspace.fruit&#8221;,&#8221;peach&#8221;) <br \/>Exception calling &#8220;Parse&#8221; with &#8220;2&#8221; argument(s): &#8220;Requested value &#8216;peach&#8217; was not found.&#8221; <br \/>At line:1 char:14 <br \/>+ [enum]::parse &lt;&lt;&lt;&lt; ([type]&#8221;myspace.fruit&#8221;,&#8221;peach&#8221;) <br \/>+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException <br \/>+ FullyQualifiedErrorId : DotNetMethodException<\/span><\/p>\n<p>That situation (parse throwing an error) should never occur because the <strong>contains<\/strong> operator used to determine the supplied value is actually part of the enumeration. After the parse method has converted the string into an instance of the enumeration, the switch statement is smart enough to match it. When a match is found, the appropriate script block is called. In this demo, switch displays a string, but this could be a decision matrix that calls other functions or Windows PowerShell cmdlets:<\/p>\n<p><span style=\"background-color: #e3e3e3\">{ <br \/>([myspace.fruit]::apple) { &#8220;Apples are good&#8221; } <br \/>([myspace.fruit]::pear) { &#8220;Pears are yummy&#8221; } <br \/>([myspace.fruit]::kiwi) { &#8220;Kiwis are great&#8221; } <br \/>} #end switch<\/span><\/p>\n<p>If the enumeration value is not found, a message is displayed that lists the permissible enumeration values. This is shown here:<\/p>\n<p><span style=\"background-color: #e3e3e3\">else { <br \/>&#8220;Your enumeration is invalid. Please use one of the following values:&#8221; <br \/>[enum]::getValues([myspace.fruit]) <br \/>}<\/span><\/p>\n<p>When the script runs and an invalid enumeration is submitted, the following output is displayed. <\/p>\n<p><a href=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/june\/hey0619\/wes-06-19-10-01.jpg\"><img decoding=\"async\" height=\"419\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4213.wes061910011_3C947ECE.jpg\" alt=\"Image of output displayed when invalid enumeration is submitted\" border=\"0\" title=\"Image of output displayed when invalid enumeration is submitted\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" \/> <\/a><\/p>\n<p>&nbsp;<\/p>\n<p>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><strong><br \/><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Microsoft Scripting Guy Ed Wilson here. The Scripting Wife and I are on the road again. She is driving, and I am playing around with Windows PowerShell enumerations. Teresa made friends with a couple we met in the concierge lounge of our hotel in Lisbon while I was teaching a month&rsquo;s worth of Windows [&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-18011","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; Microsoft Scripting Guy Ed Wilson here. The Scripting Wife and I are on the road again. She is driving, and I am playing around with Windows PowerShell enumerations. Teresa made friends with a couple we met in the concierge lounge of our hotel in Lisbon while I was teaching a month&rsquo;s worth of Windows [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/18011","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=18011"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/18011\/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=18011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=18011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=18011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}