Bookmark and Share

 

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’s stress to recede with the tide.

Photo of the beach

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—the morning low tide being too close to sunrise, a time to which she has violent reactions.

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.

Test-ForLoadedEnum.ps1

Param([string]$enumName = "myspace.fruit")
Function Test-LoadedEnum
{
Param([string]$enumName)
Try
{
[reflection.assembly]::GetAssembly([type]$enumName) } | Out-Null
New-Object psobject -Property `
@{ "Name" = $enumName.tostring() ; "Loaded" = [bool]$true }
}
Catch [system.exception]
{
New-Object psobject -Property `
@{ "Name" = $enumName.tostring() ; "Loaded" = [bool]$false }
}
} #end function Test-LoadedEnum
# *** Entry point to script ***
$rtn = test-LoadedEnum $enumName
If ($rtn.Loaded)
{ "$($rtn.name) is loaded" }
else { "$($rtn.name) NOT loaded" }

To detect if an enumeration is loaded into memory, I can use the GetAssembly static method from the reflection.assembly .NET Framework class. But, as shown here, an error is generated if the type is not loaded:

PS C:\> [reflection.assembly]::GetAssembly([myspace.fruit])
Unable to find type [myspace.fruit]: make sure that the assembly containing this typ
e is loaded.
At line:1 char:51
+ [reflection.assembly]::GetAssembly([myspace.fruit] <<<< )
+ CategoryInfo : InvalidOperation: (myspace.fruit:String) [], RuntimeE
xception
+ FullyQualifiedErrorId : TypeNotFound

When I anticipate that an error may arise in a method call, I always like to surround the call with a Try/Catch block. If the GetAssembly 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 Try portion of the Try/Catch block is shown here:

Try
{
[reflection.assembly]::GetAssembly([type]$enumName)
New-Object psobject -Property `
@{ "Name" = $enumName.tostring() ; "Loaded" = [bool]$true }
}

If an error occurs during the call to the GetAssembly method, the Catch 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:

Catch [system.exception]
{
New-Object psobject -Property `
@{ "Name" = $enumName.tostring() ; "Loaded" = [bool]$false }
}

The entry point to the script calls the Test-LoadedEnum function and passes a string that represents the name of the enumeration. The object that is returned from either the Try or the Catch block of the function is stored in the $rtn variable. An If statement checks the loaded Boolean property. If the Loaded property is true, a message that lists the name of the enumeration is displayed stating that the enum is loaded. Otherwise, the displayed message states that the enum is not loaded:

$rtn = test-LoadedEnum $enumName
If ($rtn.Loaded)
{ "$($rtn.name) is loaded" }
else { "$($rtn.name) NOT loaded" }

When the script runs, it displays the message indicating whether the enum is loaded or not. This is shown here:

PS C:\Users\ed.NWTRADERS> C:\data\ScriptingGuys\2010\HSG_6_7_10\Test-ForLoadedEnum.ps1

myspace.fruit NOT loaded

 

If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson and Craig Liebendorfer, Scripting Guys