Bookmark and Share

 

 

Microsoft Scripting Guy Ed Wilson here. The Scripting Wife met me as I was coming down the back stairs with her suitcase packed.

“Good morning, my dear! What’s up?” I exclaimed with a hint of curiosity in my voice.

“We are heading to Hilton Head this weekend,” she said in a matter-of-fact tone.

“We are?”

“Yepper! I made us reservations. We have a room that overlooks the beach.”

“But I have a bunch of scripts I am to working on,” I protested. I also need to get Craig my Weekend Scripter article.”

“You can write in the car. We are going and that is all there is to it. I need to hear the ocean and to smell the salt in the air.”

“Interesting; I studied Maslow at the University, but I do not remember “need to hear ocean” in his hierarchy,” I drolled.

“It comes right after the need to avoid getting your butt kicked,” she smiled. At least I think it was a smile.

“In that case, my dear, I think both you and I need to go to the beach,” I sensibly replied.

Keep in mind, I am not complaining. One of the great things about the Scripting Wife is the way she takes initiative and gives permission to write Windows PowerShell scripts on the trip down. What’s not to like?

A present was waiting for me in the mobile scripting lab—a sunshade! The Scripting Wife had affixed a sunshade to the passenger side window to help block glare from the sun on my laptop screen. Woohoo! I wish someone would design a laptop that can be seen when one is in the sun. I am always complaining about glare. The sunshade will certainly help me on our road trip to the beach. With my Zune HD hooked up to the stereo system in the mobile scripting lab, and Lynyrd Skynyrd cranked up to “one notch below unreasonable,” we are ready to roll.

Well, now that I am settled in, and we are through the city traffic and heading south on the interstate, I can get down to business. I wanted to get back to working with enumerations in Windows PowerShell 2.0. One of the problems with enumerations arises from the difficulty in discovering the values that they supply. If you have the source code, or if you wrote the enumeration yourself, knowing the values is trivial. But what if you have an enumeration you need to use, and you have neither the source code nor the documentation? To that end, I set to work on the Get-EnumAndValues.ps1 script, which is shown here.

Get-EnumAndValues.ps1

Function get-enumValues
{
Param([string]$enum)
$enumValues = @{}
[enum]::getvalues([type]$enum) |
ForEach-Object {
$enumValues.add($_, $_.value__)
}
$enumValues
} #end get-enumValues
get-enumValues -enum "myspace.fruit"

The Get-EnumAndValues.ps1 script begins by creating a function named get-enumValues; this function accepts a single parameter called $enum that must be supplied as a string. Next, an empty hash table is created and stored in the $enumValues variable. The hash table returns the enumeration properties and associated values to the main script. This section of the script is shown here:

Function get-enumValues
{
Param([string]$enum)
$enumValues = @{}

The getvalues static method from the system.enum .NET Framework class is used to return a list of the enumeration values from the supplied enumeration. Because the function is called by passing a string name of the enumeration, the string must be cast (converted) into an instance of the system.type .NET Framework class. The resulting values are then piped to the next section of the function. This portion of the script is shown here:

[enum]::getvalues([type]$enum) |

The enumeration values are piped to the Foreach-Object cmdlet. Inside the Foreach-Object cmdlet, the add method of the hash table is used to add the name of the enumeration value and the associated value to the hash table. This is shown here:

ForEach-Object {
$enumValues.add($_, $_.value__)
}

After the hash table has been populated with all the enumeration values, it is returned to the calling code. The function is then completed with the closing curly bracket. This section of the function is shown here:

$enumValues
} #end get-enumValues

The function is called by passing a string that represents the name of a loaded enumeration, which is shown here.

get-enumValues -enum "myspace.fruit"

The output shown in the following image appears on the screen when the script runs.

Image of script output 


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