V2: Custom Enums

V2: Custom Enums

  • Comments 4

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

 

Leave a Comment
  • Please add 2 and 3 and type the answer here:
  • Post
  • I want completely Reference for 2.0 CTP2...

  • &#160; I got nifty Add-Type trick from Tao Ma to open and close the CD door that he also posted on his

  • I tried the following with Powershell 2.0 on Windows Server 2008 R2, but it crashed Powershell. Any idea?

    $myTpe = Add-Type -memberDefinition @"

     [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]

     public static extern int QueryDosDevice(

       string lpDeviceName,

       out StringBuilder lpTargetPath,

       int ucchMax);

    "@ -Using System.Text -ReferencedAssemblies mscorlib.dll -ErrorAction 'SilentlyContinue' -passthru -name MyQueryDosDevice

    $drv = "D:"

    $x = new-object -typename System.Text.StringBuilder -ArgumentList 256

    $myTpe::QueryDosDevice($drv, [ref] $x, $x.Capacity)

  • The original blog link is broken but I found this:

    web.archive.org/.../powershell-v2-ctp2-making-custom-enums-using-add-type.aspx

Page 1 of 1 (4 items)