-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGet-Enum.ps1
115 lines (105 loc) · 4.17 KB
/
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
function Get-Enum {
<#
.SYNOPSIS
To get the static values of enum datatypes
.DESCRIPTION
To get the static values of enum datatypes
.PARAMETER TypeName
A typename or array of typenames that you wish to enumerate the enum values.
Can accept input from the pipeline.
.PARAMETER Binary
A switch that will also display the value in binary form.
.PARAMETER Hex
A switch that will also display the value in hexadecimal form.
.PARAMETER IncludeInput
A switch determining if you want the TypeName to appear in the output. Aliased to 'IncludeType'
.EXAMPLE
Get-Enum -TypeName 'System.Windows.Forms.DialogResult'
Name Value
---- -----
None 0
OK 1
Cancel 2
Abort 3
Retry 4
Ignore 5
Yes 6
No 7
.EXAMPLE
Get-Enum -TypeName 'System.Windows.Forms.MessageBoxButtons' -Hex -IncludeInput
TypeName Name Value Hex
-------- ---- ----- ---
System.Windows.Forms.MessageBoxButtons OK 0 0x0
System.Windows.Forms.MessageBoxButtons OKCancel 1 0x1
System.Windows.Forms.MessageBoxButtons AbortRetryIgnore 2 0x2
System.Windows.Forms.MessageBoxButtons YesNoCancel 3 0x3
System.Windows.Forms.MessageBoxButtons YesNo 4 0x4
System.Windows.Forms.MessageBoxButtons RetryCancel 5 0x5
.EXAMPLE
Get-Enum -TypeName 'System.Windows.Forms.MessageBoxButtons' -Binary
Name Value Binary
---- ----- ------
OK 0 0
OKCancel 1 1
AbortRetryIgnore 2 10
YesNoCancel 3 11
YesNo 4 100
RetryCancel 5 101
#>
[CmdletBinding(ConfirmImpact='None')]
[OutputType('psobject')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingInvokeExpression','')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseProcessBlockForPipelineCommand','')]
param (
[Parameter(Mandatory, HelpMessage='Please enter a .Net typename',Position=0,ValueFromPipeline)]
[type[]] $TypeName,
[switch] $Binary,
[switch] $Hex,
[Alias('IncludeType')]
[switch] $IncludeInput
)
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
if ($Binary) {
Write-Verbose -Message 'Binary included in output.'
}
if ($Hex) {
Write-Verbose -Message 'Hex included in output.'
}
if ($IncludeInput) {
Write-Verbose -Message 'TypeName included in output.'
}
}
process {
foreach ($currentType in $TypeName) {
Write-Verbose -Message "currentType [$currentType]"
if ($currentType.BaseType.FullName -ne 'System.Enum')
{
Write-Error -message "Type '$curExpandType' is not an enum"
break
}
if ($IncludeInput) {
$props = @(
@{ Name = 'TypeName' ; Expression = { Invoke-Expression "write-output $($currenttype)" }}
@{ Name = 'Name' ; Expression = { [string] $_ } }
@{ Name = 'Value' ; Expression = { [int32](Invoke-Expression -Command "[$($currentType.FullName)]'$_'") }}
)
} else {
$props = @(
@{ Name = 'Name' ; Expression = { [string] $_ } }
@{ Name = 'Value' ; Expression = { [int32](Invoke-Expression -Command "[$($currentType.FullName)]'$_'") }}
)
}
if ($Binary) {
$props += @{ Name = 'Binary'; Expression={[Convert]::ToString([int32](Invoke-Expression -Command "[$($currentType.FullName)]'$_'"), 2)}}
}
if ($Hex) {
$props += @{ Name = 'Hex'; Expression={'0x{0}' -f ([Convert]::ToString([int32](Invoke-Expression -Command "[$($currentType.FullName)]'$_'"), 16))}}
}
[enum]::GetNames($currentType) | Select-Object -Property $props
}
}
end {
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}