{"id":18071,"date":"2010-06-13T00:01:00","date_gmt":"2010-06-13T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/06\/13\/hey-scripting-guy-weekend-scripter-testing-for-a-loaded-enumeration\/"},"modified":"2010-06-13T00:01:00","modified_gmt":"2010-06-13T00:01:00","slug":"hey-scripting-guy-weekend-scripter-testing-for-a-loaded-enumeration","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-weekend-scripter-testing-for-a-loaded-enumeration\/","title":{"rendered":"Hey, Scripting Guy! Weekend Scripter: Testing for a Loaded Enumeration"},"content":{"rendered":"<p><a href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><\/p>\n<p><img decoding=\"async\" height=\"16\" width=\"125\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" alt=\"Bookmark and Share\" border=\"0\" \/><\/p>\n<p><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. The constant pounding of the waves against the shore has a calming effect upon stressed psyches. The moist salty ocean air carries to the balcony as the sea spray from the waves wafts along the brisk breeze that parallels the beach in the morning. The beach, as seen in the following image, beckons, invites, and exhorts one to allow one&rsquo;s stress to recede with the tide. <\/p>\n<p><a href=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/june\/hey0613\/wes-06-13-10-01.jpg\"><img decoding=\"async\" height=\"484\" width=\"644\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2705.wes061310011_164BF89B.jpg\" alt=\"Photo of the beach\" border=\"0\" title=\"Photo of the beach\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" \/> <\/a><\/p>\n<p>There is no need to dig out my Zune HD because the seagulls and pelicans that comprise the early morning airborne fishing fleet on Hilton Head Island serenade me. With a semidiurnal tide, I get to see low tide early in the morning and early in the evening. The vampiric Scripting Wife joins me for a walk along the beach during early evening low tide&mdash;the morning low tide being too close to sunrise, a time to which she has violent reactions.<\/p>\n<p>During one of those walks along the beach, I was thinking about how to use an enumeration in a Windows PowerShell script. It dawned on me (no pun intended) that before being too carried away with working with an enumeration, it makes sense to detect if the enumeration is available to the current Windows PowerShell session. I scampered back to the hotel room, and set to work on the problem. The result is the Test-ForLoadedEnum.ps1 script that is shown here. <\/p>\n<p><strong>Test-ForLoadedEnum.ps1<\/strong><\/p>\n<p><span style=\"background-color: #f0f0f0\">Param([string]$enumName = &#8220;myspace.fruit&#8221;) <br \/>Function Test-LoadedEnum <br \/>{ <br \/>Param([string]$enumName) <br \/>Try <br \/>{ <br \/>[reflection.assembly]::GetAssembly([type]$enumName) } | Out-Null <br \/>New-Object psobject -Property ` <br \/>@{ &#8220;Name&#8221; = $enumName.tostring() ; &#8220;Loaded&#8221; = [bool]$true } <br \/>} <br \/>Catch [system.exception] <br \/>{ <br \/>New-Object psobject -Property ` <br \/>@{ &#8220;Name&#8221; = $enumName.tostring() ; &#8220;Loaded&#8221; = [bool]$false } <br \/>} <br \/>} #end function Test-LoadedEnum <br \/># *** Entry point to script *** <br \/>$rtn = test-LoadedEnum $enumName <br \/>If ($rtn.Loaded) <br \/>{ &#8220;$($rtn.name) is loaded&#8221; } <br \/>else { &#8220;$($rtn.name) NOT loaded&#8221; }<\/span><\/p>\n<p>To detect if an enumeration is loaded into memory, I can use the <b>GetAssembly<\/b> static method from the <b>reflection.assembly<\/b> .NET Framework class. But, as shown here, an error is generated if the type is not loaded:<\/p>\n<p><span style=\"background-color: #f0f0f0\">PS C:\\&gt; [reflection.assembly]::GetAssembly([myspace.fruit]) <br \/>Unable to find type [myspace.fruit]: make sure that the assembly containing this typ <br \/>e is loaded. <br \/>At line:1 char:51 <br \/>+ [reflection.assembly]::GetAssembly([myspace.fruit] &lt;&lt;&lt;&lt; ) <br \/>+ CategoryInfo : InvalidOperation: (myspace.fruit:String) [], RuntimeE <br \/>xception <br \/>+ FullyQualifiedErrorId : TypeNotFound<\/span><\/p>\n<p>When I anticipate that an error may arise in a method call, I always like to surround the call with a <b>Try<\/b>\/<b>Catch<\/b> block. If the <b>GetAssembly<\/b> method succeeds, I return a custom object that contains just two properties: the name of the enumeration and a Boolean value that indicates the enumeration is loaded. The <b>Try<\/b> portion of the <b>Try<\/b>\/<b>Catch<\/b> block is shown here:<\/p>\n<p><span style=\"background-color: #f0f0f0\">Try <br \/>{ <br \/>[reflection.assembly]::GetAssembly([type]$enumName) <br \/>New-Object psobject -Property ` <br \/>@{ &#8220;Name&#8221; = $enumName.tostring() ; &#8220;Loaded&#8221; = [bool]$true } <br \/>}<\/span><\/p>\n<p>If an error occurs during the call to the <b>GetAssembly<\/b> method, the <b>Catch<\/b> block will catch the error. A custom object is created that contains the exact same information as the success block (the name of the assembly), and a Boolean value indicating the assembly is not loaded. This is shown here:<\/p>\n<p><span style=\"background-color: #f0f0f0\">Catch [system.exception] <br \/>{ <br \/>New-Object psobject -Property ` <br \/>@{ &#8220;Name&#8221; = $enumName.tostring() ; &#8220;Loaded&#8221; = [bool]$false } <br \/>} <\/span><\/p>\n<p>The entry point to the script calls the <b>Test-LoadedEnum<\/b> function and passes a string that represents the name of the enumeration. The object that is returned from either the <b>Try<\/b> or the <b>Catch<\/b> block of the function is stored in the <b>$rtn<\/b> variable. An <b>If<\/b> statement checks the loaded Boolean property. If the <b>Loaded<\/b> property is true, a message that lists the name of the enumeration is displayed stating that the <b>enum<\/b> is loaded. Otherwise, the displayed message states that the <b>enum<\/b> is not loaded: <\/p>\n<p><span style=\"background-color: #f0f0f0\">$rtn = test-LoadedEnum $enumName <br \/>If ($rtn.Loaded) <br \/>{ &#8220;$($rtn.name) is loaded&#8221; } <br \/>else { &#8220;$($rtn.name) NOT loaded&#8221; }<\/span><\/p>\n<p>When the script runs, it displays the message indicating whether the <b>enum<\/b> is loaded or not. This is shown here:<\/p>\n<p><span style=\"background-color: #f0f0f0\">PS C:\\Users\\ed.NWTRADERS&gt; C:\\data\\ScriptingGuys\\2010\\HSG_6_7_10\\Test-ForLoadedEnum.ps1<\/span><\/p>\n<p><span style=\"background-color: #f0f0f0\">myspace.fruit NOT loaded<\/span><\/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><b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/p>\n<p><strong><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Microsoft Scripting Guy Ed Wilson here. The constant pounding of the waves against the shore has a calming effect upon stressed psyches. The moist salty ocean air carries to the balcony as the sea spray from the waves wafts along the brisk breeze that parallels the beach in the morning. The beach, as seen [&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-18071","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 constant pounding of the waves against the shore has a calming effect upon stressed psyches. The moist salty ocean air carries to the balcony as the sea spray from the waves wafts along the brisk breeze that parallels the beach in the morning. The beach, as seen [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/18071","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=18071"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/18071\/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=18071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=18071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=18071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}