Last active
September 26, 2016 18:38
-
-
Save dlwyatt/ddb33eed55bfe32bb81d to your computer and use it in GitHub Desktop.
Flags Enum parameter demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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