Author Posts

March 1, 2018 at 12:53 am

Wow, I never knew about this easy way to deal with bitmasks.

[flags()] Enum ErrorTable
{
  Error1 = 0x01
  Error2 = 0x02
  Error3 = 0x04
  Error4 = 0x08
  Error5 = 0x10
  Error6 = 0x20
}

PS C:\> $errorcodes = [ErrorTable]'Error1,Error2'
PS C:\> $errorcodes
Error1, Error2
PS C:\> $errorcodes = [ErrorTable]3
PS C:\> $errorcodes
Error1, Error2
PS C:\> $errorcodes.HasFlag([ErrorTable]'Error2')                                          
True

March 3, 2018 at 12:00 am

It is indeed a cool feature but I believe the enum keyword only became available in Powershell 5. If you are stuck in the functional abyss of powershell 4 or less then it's still back to inlinining c# code.