In PowerShell you often see the use of enum's
for some info , examples of Enums and there usage see :
/\/\o\/\/ PowerShelled: Get SpecialFolder Locations in Monad,
/\/\o\/\/ PowerShelled: Adding a Simple AccesRule to a file ACL in MSH
as you can see in the Special Forder function this is very handy for some parameters :
It is not possible yet to create Enums and classes directly in PowerShell, but there is a way by using Reflection :
# Make a Custom Enum in PowerShell
$cd = [AppDomain]::CurrentDomain
$an = new-object System.Reflection.AssemblyName('Mow')
$ab = $cd.DefineDynamicAssembly($an,'RunAndSave')
$mb = $ab.DefineDynamicModule($an.Name, "$($an.Name).dll")
$eb = $mb.DefineEnum("Fruit",'Public', [int])
& {
$eb.DefineLiteral('Apple',1)
$eb.DefineLiteral('strawberry',2)
$eb.DefineLiteral('Banana',3)
} | out-null
$t = $eb.CreateType()
It might not look that way, but this is "Fasten your Seatbells (c) Jeffrey Snover" - Code (you can even create complete classes this way but that's outside my "Scope" (@ reading developers : but this would be real cool material for a CMDlet *Wink* *Wink* )),
Hence, I won't go into the workings to much, to get an impression after runnin the code try :
$cd,$an,$mb,$eb | gm
but the code as is, is easy to customize, e.g. to add a Kiwi to the list with number 4 add a line like this
$eb.DefineLiteral('Kiwi',4)
...
PoSH>$t = $eb.CreateType()
PoSH>[fruit]
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Fruit System.EnumPoSH>[enum]::GetNames([fruit])
Apple
strawberry
BananaPoSH>function new-FruitShake ([fruit]$f) {"Here is your $f Shake"}
PoSH>new-FruitShake Apple
Here is your Apple ShakePoSH>new-FruitShake chocolate
new-FruitShake : Cannot convert value "chocolate" to type "Fruit" due to invalid enumeration values. Specify one of the
following enumeration values and try again. The possible enumeration values are "Apple, strawberry, Banana".
At line:1 char:15
+ new-FruitShake <<<< chocolate
If you have my custom tabcompletion
you can even tabcomplete on it :
[fruit]A[tab]
[fruit]Apple
Enjoy,
Greetings /\/\o\/\/
Tags : PowerShell
October 2005 November 2005 December 2005 January 2006 February 2006 March 2006 April 2006 May 2006 June 2006 July 2006 August 2006 September 2006 October 2006 November 2006 December 2006