Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

PowerShell V2 CTP2 : making Custom Enums using add-Type

 

In PowerShell Enums are used a lot but you're not able to create them directly in PowerShell

I made a post about a workaround using reflection on my old blog here : /\/\o\/\/ PowerShelled: PowerShell : making Custum Enums ,as this is using reflection it is a bit hard to grasp, and we can not create enums within a namespace only global.

But in the PowerShell V2 CTP2 there is another simpler way to do this introduced the new Cmdlet :  Add-Type

image

you might have seen in the former posts about WPF, you can use Add-Type to load an assembly or DLL instead of using the .NET framework directly as in PowerShell V1,

V1

[System.Reflection.Assembly]::LoadWithPartialName('system.windows.forms')

CTP2

Add-Type -AssemblyName system.windows.forms

but the add-Type Cmdlet can do much more (see also list of parameters above) !

The Add-Type Cmdlet gives also access to the CodeDomProvider, that enables us to compile .NET code ( for an example how to do this using raw .NET in V1 see :/\/\o\/\/ PowerShelled: getting an Inputbox in MOnad )

This combined with loading assemblies gives us an easy way to use inline C# code in PowerShell we just pass the C# code to the Add-Type cmdlet and it will be compiled and loaded on-the-fly.I made an Add-Enum function using this technique and variables in the embedded C# code, to simply create and use Custom Enums in PowerShell like this  :

image

much simpler and the possibility to provide a namespace for the Enum what was not possible using Reflection.

The Script looks like this :

# Function Add-Enum
# requires -version 2
#
# /\/\o\/\/ 2008
# Http://thePowerShellGuy.com

Function Add-Enum ($name,[string[]]$values,$nameSpace) {

if ($nameSpace) {

$code = @"
  namespace $NameSpace
  {
      public enum $name : int 
      {
          $($values -join ",`n")
      }
  }
"@

} else {

$code = @"
  public enum $name : int 
  {
      $($values -join ",`n")
  }
"@

}

  Add-Type $code 

}

this function makes it easy to create Enums and you can use namespaces and use the -as operator to cast int values into Enum names :

image 

You can see above that the enum by default starts at 0 by default, but that you can overrule values on a per item base, the items following will count up from the last item. So in the Mow.OSType enum giving a start value of 1 is enough to number up from there.

The Get-EnumMembers function I used above to quickly check the Enum values and names looks like this :

Function get-EnumMembers ([type]$enum){
  [enum]::GetValues($enum) | 
    select @{Name='Value';e={[int]($_ -as $enum)}},
      @{Name='Name';e={$_ -as $enum}}
}

Enjoy,

Greetings /\/\o\/\/

Published Monday, June 02, 2008 1:16 PM by MoW
Filed under: , , ,

Comments

# re: PowerShell V2 CTP2 : making Custom Enums using add-Type

Hi

get-EnumMembers Microsoft.PowerShell.ExecutionPolicy return duplicate names:

Value         Name

-----         ----

   0 Unrestricted

   1 RemoteSigned

   2    AllSigned

   3   Restricted

   3   Restricted

Here's my version:

function get-enum([type]$type){

[enum]::getNames($type) | select @{n="Name";e={$_}},@{n="Value";e={$type::$_.value__}}

}

get-enum Microsoft.PowerShell.ExecutionPolicy

Name         Value

----         -----

Unrestricted     0

RemoteSigned     1

AllSigned        2

Restricted       3

Default          3

---

Shay Levy

$cript Fanatic

http://scriptolog.blogspot.com

Tuesday, June 03, 2008 8:35 AM by Shay

# V2: Custom Enums

Once again MOW proves what a clever guy he is. Check out his blog entry PowerShell V2 CTP2: making Custom

Tuesday, June 03, 2008 9:03 AM by Windows PowerShell

# re: PowerShell V2 CTP2 : making Custom Enums using add-Type

@Shay, Nice catch

I did not realize an enum could have same value twice.

So indeed it's better to go the other way arround.

Thanks,

Greetings /\/\o\/\/

Tuesday, June 03, 2008 4:59 PM by MoW

# PowerShell Performance Series Part 1 (Warming Up)

This is the first part of a ( Multi-Blog ) series about PowerShell Performance. In a former post, I mentioned

Wednesday, June 11, 2008 6:13 PM by The PowerShell Guy

# text wrap around image generator

Wednesday, July 23, 2008 11:24 AM by text wrap around image generator

# PowerShell and using .Net enum types

[NOTE: Because this page is the first hit in Google when you search on Powershell + enum, and I landed

Wednesday, September 03, 2008 10:43 AM by Serge van den Oever [Macaw]

# re: PowerShell V2 CTP2 : making Custom Enums using add-Type

How do you make the tool list the available parameters automatically as shown in the first diagram?

Thanks,

xiaodi

Tuesday, October 28, 2008 4:10 AM by xiaodi

# re: PowerShell V2 CTP2 : making Custom Enums using add-Type

@xiaodi,

you can just hit tab to complete paramters in PowerSHell

for the list you need to have PowerTab installed

Greetings /\/\o\/\/

Tuesday, October 28, 2008 7:57 AM by MoW

# re: PowerShell V2 CTP2 : making Custom Enums using add-Type

@xiaodi,

you can just hit tab to complete paramters in PowerSHell

for the list you need to have PowerTab installed

Greetings /\/\o\/\/

Tuesday, October 28, 2008 7:57 AM by MoW

# Unblocking files

When I download files from the Internet to Vista or Windows 7 the file is blocked and I can’t execute

Saturday, September 05, 2009 9:53 AM by Richard Siddaway's Blog
Anonymous comments are disabled