Flags Enumeration Bitwise Operations Fail With 'Specified cast is not valid' Error
Votes from Connect: 3
Original Date Submitted: 2/19/2015 11:08:09 PM
Handle: Rohn Edwards
Site Name: PowerShell
Feedback ID: 1138469
Frequency: Always Happens
Regression: No, this is new to the most recent version
In the December 2014 and February 2015 WMF 5.0 previews, flags enumerations that have byte, sbyte, short, or ushort underlying types have to be cast or coerced to a numeric type before -band, -bor, or -bxor bitwise operations will work.
int, uint, long, and ulong underlying types still work as expected.
Product Studio item created by Connect Synchronizer due to creation of feedback ID 1138469 (http://connect.microsoft.com/PowerShell/feedback/ViewFeedback.aspx?FeedbackID=1138469).
First, ensure $PSVersionTable.PSVersion is 5.0.10018.0
To see the error, run this command:
PS> [System.Windows.Input.KeyStates]::Down -band [System.Windows.Input.KeyStates]::Down
KeyStates has an underlying type of 'Byte'. The command works if you modify it like this:
PS> [System.Windows.Input.KeyStates]::Down.value__ -band [System.Windows.Input.KeyStates]::Down
No errors are expected, like running this command:
It appears that version 5 returns an instance of the enumeration instead of a numeric value (which it looks like previous versions return).
Internal BugId: 13250