How to create enum in PowerShell?

How to create enum in PowerShell?

  • Comments 8

[Updated to comply with the naming guidelines (create=>new)]

Dynamic assembly emitting techniques can be used to create new classes or data types in PowerShell. Trick is that right permission needs to be set so that data types created in dynamic assembly can be used subsequently.

Following is an example for creating a new enum type and use it in powershell.

PS> cat enum.ps1

function New-Enum ([string] $name)

{

    $appdomain = [System.Threading.Thread]::GetDomain()

    $assembly = new-object System.Reflection.AssemblyName

    $assembly.Name = "EmittedEnum"

 

$assemblyBuilder = $appdomain.DefineDynamicAssembly($assembly,

[System.Reflection.Emit.AssemblyBuilderAccess]::Save -bor [System.Reflection.Emit.AssemblyBuilderAccess]::Run);

    $moduleBuilder = $assemblyBuilder.DefineDynamicModule("DynamicModule", "DynamicModule.mod");

    $enumBuilder = $moduleBuilder.DefineEnum($name, [System.Reflection.TypeAttributes]::Public, [System.Int32]);

 

    for($i = 0; $i -lt $args.Length; $i++)

    {

        $null = $enumBuilder.DefineLiteral($args[$i], $i);

    }

 

    $enumBuilder.CreateType() > $null;

}

PS> . 'C:\Documents and Settings\gxie\enum.ps1'

PS > New-Enum my.color blue red yellow

PS > [my.color]::blue

blue

PS > [my.color]::red

red

PS > [my.color]::yellow

yellow

PS > [my.color]::black

PS > 

- George from PowerShell Team

 

Leave a Comment
  • Please add 2 and 8 and type the answer here:
  • Post
  • A slightly easier (shorter) way to get your current app domain is:

    [appdomain]::CurrentDomain

  • Oh, that code makes my head hurt just looking at it... ;-0

    Is it possible in Powershell to create namespace aliases, something like this:

    Set-Namespace-Alias System.Reflection.Emit.AssemblyBuilderAccess aba

    So that the really long line above could be written like this?:

    $assemblyBuilder = $appdomain.DefineDynamicAssembly($assembly, [aba]::Save -bor [aba]::Run);

  • @ Mike Schinkel

    You can use a powershell Type Shortcut(PowerShell team blog entry on that subject can be found on http://blogs.msdn.com/powershell/archive/2006/07/12/663540.aspx)

    Now you can do,

    $aba = [System.Reflection.Emit.AssemblyBuilderAccess]

    ...

    $assemblyBuilder = $appdomain.DefineDynamicAssembly($assembly, $aba::Save -bor $aba::Run);

    And here is the New-Enum function using type shortcuts

    function New-Enum ([string] $name)

    {

       $appdomain = [System.Threading.Thread]::GetDomain()

       $assembly = new-object System.Reflection.AssemblyName

       $assembly.Name = "EmittedEnum"

       # declare type alias

       $aba = [System.Reflection.Emit.AssemblyBuilderAccess]

       $typeAttribs = [System.Reflection.TypeAttributes]

       $short = [System.Int32]

       $assemblyBuilder = $appdomain.DefineDynamicAssembly($assembly, $aba::Save -bor $aba::Run);

       $moduleBuilder = $assemblyBuilder.DefineDynamicModule("DynamicModule", "DynamicModule.mod");

       $enumBuilder = $moduleBuilder.DefineEnum($name, $typeAttribs::Public, $short);

       for($i = 0; $i -lt $args.Length; $i++)

       {

       $null = $enumBuilder.DefineLiteral($args[$i], $i);

       }

       $enumBuilder.CreateType() > $null;

    }

  • Cool! Awesome!  Thanks.

  • yeh, It's cool

  • I have a question with regard to a modification of the enum script.

    If I change the for loop so that the second parameter to DefineLiteral is a double, it always fails to convert the type on the second iteration of the loop:

    for($i = 0; $i -lt $args.Length; $i++)

    {

    $null = $enumBuilder.DefineLiteral($args[$i], ([Math]::Pow(2, $i)));

    }

    I posted to the microsoft.public.windows.powershell newsgroup and was able to work around the issue (URL: http://www.microsoft.com/communities/newsgroups/list/en-us/default.aspx?dg=microsoft.public.windows.powershell&tid=6f4ed326-8217-430d-a0e7-8ed49a1d9fb1&cat=en_us_4200CF39-8075-8AEF-F08A-B4D1E076AE83&lang=en&cr=us&p=1 ).

    I'd like to understand why the type conversion fails when passing a double to DefineLiteral inside a for loop (and always beginning with the second iteration), but works fine when not encapsulated in the loop.

  • I'm having an issue using a dynamic type as a function parameter in a script file. I dynamically define

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

Page 1 of 1 (8 items)