Skip to content

Instantly share code, notes, and snippets.

@drlsdee
Created February 10, 2023 12:49
Show Gist options
  • Select an option

  • Save drlsdee/6d09a2a6ab1d194e2026ce3d1b302907 to your computer and use it in GitHub Desktop.

Select an option

Save drlsdee/6d09a2a6ab1d194e2026ce3d1b302907 to your computer and use it in GitHub Desktop.
A small and simple PowerShell function to convert enum types to a dictionary with enum numeric values as dictionary keys mapped to corresponding enum string names.
<#
.SYNOPSIS
A small and simple PowerShell function to convert enum types to a dictionary with enum numeric values as dictionary keys mapped to corresponding enum string names.
.DESCRIPTION
A small and simple PowerShell function to convert enum types to a dictionary with enum numeric values as dictionary keys mapped to corresponding enum string names.
.PARAMETER Type
Specifies the type as a [System.Type] object, or as a string containing the typename. Note that the function may throw an exception, if it fails to resolve a type by its short type name.
.EXAMPLE
PS C:\> Get-EnumValuesTable -Type "System.Security.AccessControl.AccessControlType"
The function returns a sorted dictionary:
@{
0 = "Allow" ;
1 = "Deny"
}
.EXAMPLE
PS C:\> Get-EnumValuesTable -Type ((Get-Acl -Path .).Access[0].InheritanceFlags.GetType())
The function returns a sorted dictionary with keys and values of the enum type [System.Security.AccessControl.InheritanceFlags]:
@{
0 = "None" ;
1 = "ContainerInherit" ;
2 = "ObjectInherit"
}
.EXAMPLE
PS C:\> Get-EnumValuesTable -Type ((Get-Acl -Path .).Access[0].InheritanceFlags.GetType().FullName)
Note that in this case the function takes a string typename as an argument.
The function returns a sorted dictionary with keys and values of the enum type [System.Security.AccessControl.InheritanceFlags]:
@{
0 = "None" ;
1 = "ContainerInherit" ;
2 = "ObjectInherit"
}
.EXAMPLE
PS C:\> [System.DirectoryServices.ActiveDirectory.ActiveDirectorySchema]::GetCurrentSchema().FindProperty('Description').Syntax.GetType() | Get-EnumValuesTable
The function returns a sorted dictionary with keys and values of the enum type [System.DirectoryServices.ActiveDirectory.ActiveDirectorySyntax]:
@{
0 = "CaseExactString" ;
1 = "CaseIgnoreString" ;
2 = "NumericString" ;
3 = "DirectoryString" ;
4 = "OctetString" ;
5 = "SecurityDescriptor" ;
6 = "Int" ;
7 = "Int64" ;
8 = "Bool" ;
9 = "Oid" ;
10 = "GeneralizedTime" ;
11 = "UtcTime" ;
12 = "DN" ;
13 = "DNWithBinary" ;
14 = "DNWithString" ;
15 = "Enumeration" ;
16 = "IA5String" ;
17 = "PrintableString" ;
18 = "Sid" ;
19 = "AccessPointDN" ;
20 = "ORName" ;
21 = "PresentationAddress" ;
22 = "ReplicaLink"
}
.INPUTS
[System.Type]
.OUTPUTS
[System.Collections.Generic.SortedDictionary[uint64,string]]
.NOTES
A small and simple PowerShell function to convert enum types to a dictionary with enum numeric values as dictionary keys mapped to corresponding enum string names.
If the specified type is not an enum type, the function returns nothing.
You may specify the type as a [System.Type] object or as a string containing the type name.
But the function may throw an exception, if it fails to resolve a type by its short type name.
This help seems to be much longer than the function code :P
.LINK
.
#>
function Get-EnumValuesTable {
[CmdletBinding()]
[OutputType([System.Collections.Generic.SortedDictionary[uint64,string]])]
param (
# Specifies a type
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true
)]
[type]
$Type
)
begin {}
process {
if (-not $Type.IsEnum) {
return
}
[string[]]$enumNames = [System.Enum]::GetNames($Type)
[System.Collections.Generic.SortedDictionary[uint64,string]]$outputObject = [System.Collections.Generic.SortedDictionary[uint64,string]]::new()
foreach ($enumName in $enumNames) {
[uint64]$enumValue = $Type::$enumName.value__
$outputObject.Add($enumValue,$enumName)
}
return $outputObject
}
end {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment