Skip to content

Instantly share code, notes, and snippets.

@dlwyatt
Last active September 26, 2016 18:38
Show Gist options
  • Save dlwyatt/ddb33eed55bfe32bb81d to your computer and use it in GitHub Desktop.
Save dlwyatt/ddb33eed55bfe32bb81d to your computer and use it in GitHub Desktop.
Flags Enum parameter demo
Add-Type -TypeDefinition @'
using System;
[Flags]
public enum MyEnum
{
Option1 = 0x1,
Option2 = 0x2,
Option3 = 0x4,
All = Option1 | Option2 | Option3
}
'@
function Test-Flags
{
[CmdletBinding()]
param (
[MyEnum] $Param
)
# Note: The version of .NET in PowerShell 2.0 doesn't have the HasFlag() method, so you'd
# have slightly different code if you want your function to support PS2.0.
if ($Param.HasFlag([MyEnum]::Option1))
{
# Do Option1 stuff
Write-Host 'Option1 set'
}
if ($Param.HasFlag([MyEnum]::Option2))
{
# Do Option2 stuff
Write-Host 'Option2 set'
}
if ($Param.HasFlag([MyEnum]::Option3))
{
# Do Option3 stuff
Write-Host 'Option3 set'
}
}
Write-Host 'Testing Option1,Option2'
Test-Flags -Param Option1,Option2
Write-Host 'Testing All'
Test-Flags -Param All
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment