functions/Get-Enum.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function Get-Enum { 
    param (
        [Parameter(Mandatory=$true)]
        [Type]$Type
    )
 
    if ($Type.BaseType.FullName -ne 'System.Enum')
    {
        return
    }
 
    if ($Type.CustomAttributes | Where-Object { $_.AttributeType -eq [System.FlagsAttribute] })
    {
        $isFlagsEnum = $true
    }
 
    $props = @(
        @{ Name = 'Name'; Expression={ [string]$_ } }
        @{ Name = 'Value'; Expression={ [uint32](Invoke-Expression "[$($type.FullName)]'$_'") }}
    )
 
    if ($isFlagsEnum)
    {
        $props += @{ Name = 'Binary'; Expression={[Convert]::ToString([uint32](Invoke-Expression "[$($type.FullName)]'$_'"), 2)}}
    }
 
    [enum]::GetNames($Type) | Select-Object -Property $props   
}