June 3rd, 2008

V2: Custom Enums

PowerShell Team
PowerShell Team

Once again MOW proves what a clever guy he is.  Check out his blog entry PowerShell V2 CTP2: making Custom Enums using Add-Type. He shows how you can make your own enums using a very simple function he wrote (Add-Enum) which leverages our new Add-Type cmdlet.  Wonderful stuff.

Add-Type is one of those huge game-changing features that we’ve added to V2 and it will take all of us a while to understand the full range of scenarios that it enables.  The things that we had in focus for this were:

  • Access to WIN32 APIs.  Check out http://pinvoke.net .  It has C# and VB.Net code snippets to access WIN32 APIs.  Now you can just go there, cut and paste that code into your PS script using Add-Type and your script can now access WIN32 APIs.  With this, I think we now provide complete access to all the major (and many of the minor) API sets you could want to use:  .Net, COM, WMI, ADSI, XML, ADO, Win32.  If we are missing anything, please let us know.
  • Performance.  There will be times when you need absolute performance for an operation (e.g. you need to chug through a couple terabytes of log files) and you want all the nice features of PowerShell but you need the inner core of operations to be maximally efficient.  We didn’t want to make you throw everything away and re-write it in .NET.  Now you can have portions of your processing in C#/VB.NET and fly like the wind.
  • Simple mechanism to load assemblies (read MOWs blog).

We had a couple others but these were the biggies. 

There is an important aspect to Add-Type that you need to get in focus.  Let me illustrate it with the following example using MOW’s cool Add-Enum function:

PS> Add-Enum Fruit “Apple”,”Bannana”,”Strawberry”
PS> [fruit]”Apple”
Apple
PS> [fruit]”Banana”
Cannot convert value “Banana” to type “Fruit” due to invalid enumeration values. Specify
one of the following enumeration values and try again. The possible enumeration values ar
e “Apple, Bannana, Strawberry”.
At line:1 char:8
+ [fruit] <<<< “Banana”
PS> Add-Enum Fruit “Apple”,”Banana”,”Strawberry”
Add-Type : Cannot add type. The type name ‘Fruit’ already exists.
At line:20 char:11
+   Add-Type <<<<  $code

Notice that I misspelled Banana when I first defined the Enum.  Having done that, I can’t now go in and fix the Enum.  That is because this is a now a proper .NET type and it is not dynamic.  If you want to change this, you need to start a new session to do so.

That is an important semantic that you should have in focus when working with Add-Type.  It is an awesome tool but you need to adjust your workflow to accommodate this.  e.g. do yor work in a throw-away worker process until you get it right.  Note also that  you need a worker PROCESS and not a worker RUNSPACE.  When you add the type, you are adding it to the PROCESS not the runspace.

Add-Type is an awesome new tool but you need to understand its semantics to master it.  Try it out – you are going to love it.  And just like MOW did, SHARE SHARE SHARE.  When you share like MOW, you make us all smarter faster. 

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

 

Category
PowerShell
Topics
Add-Type

Author

PowerShell Team
PowerShell Team

PowerShell is a task-based command-line shell and scripting language built on .NET. PowerShell helps system administrators and power-users rapidly automate tasks that manage operating systems (Linux, macOS, and Windows) and processes.

0 comments

Discussion are closed.