Automating the world one-liner at a time…
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:
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"ApplePS> [fruit]"Banana"Cannot convert value "Banana" to type "Fruit" due to invalid enumeration values. Specifyone of the following enumeration values and try again. The possible enumeration values are "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 ArchitectVisit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShellVisit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
I want completely Reference for 2.0 CTP2...
  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)