/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" TechNet Webcast: An Overview of Windows PowerShell "," "Windows PowerShell: TFM" goes "Gold" "," PowerShell : Advanced renaming of files "," PowerShell : Playing with LeapYears "," Download PowerShell 1.0 RTM "," PowerShell goes RTM "," Windows PowerShell Week on ScriptCenter "," PowerShell : How Can I Split a String Only on Spec... "," PowerShell : How Can I Tell Whether a Number is Ev... "," PowerShell : 99-bottles-of-beer "," ")

Monday, December 04, 2006

 


PowerShell : making Custum Enums



In PowerShell you often see the use of enum's

for some info , examples of Enums and there usage see :

/\/\o\/\/ PowerShelled: Exploring .NET types, classes and Enums ...,

/\/\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)

As you can see here this is handy for custom functions :

...

PoSH>$t = $eb.CreateType()

PoSH>[fruit]

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Fruit System.Enum

PoSH>[enum]::GetNames([fruit])
Apple
strawberry
Banana

PoSH>function new-FruitShake ([fruit]$f) {"Here is your $f Shake"}

PoSH>new-FruitShake Apple
Here is your Apple Shake

PoSH>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

/\/\o\/\/ PowerShelled: PowerShell : Tabcompletion Part 5 or

/\/\o\/\/ PowerShelled: PowerShell Tab Completion Part 4

you can even tabcomplete on it :

[fruit]A[tab]

[fruit]Apple

 

Enjoy,

 

Greetings /\/\o\/\/

Tags : PowerShell




Comments: Post a Comment



<< Home

Archives

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  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?