Using Powershell's bitwise operators

后端 未结 2 770
生来不讨喜
生来不讨喜 2021-02-12 10:24

I\'m looking for example of how I would solve the scenario below:

Imagine my printer has the following property for \"Status\"
0 -Offline
2 -Paper Tray Empty

相关标签:
2条回答
  • 2021-02-12 10:35

    You can let PowerShell do more of the work for you. Here's an example using System.IO.FileOptions:

    PS> [enum]::GetValues([io.fileoptions]) | ?{$_.value__ -band 0x90000000}
    RandomAccess
    WriteThrough
    
    0 讨论(0)
  • 2021-02-12 10:38

    The boolean bitwise and operator in Powershell is -band.

    Assume you define your values and descriptions in a hashtable, and have the value of 12 from the printer:

     $status = @{1 = "Offline" ; 2 = "Paper Tray Empty" ; 4 = "Toner Exhausted" ; 8 = "Paper Jam" }
     $value = 12
    

    Then, this statement will give you the textual descriptions:

    $status.Keys | where { $_ -band $value } | foreach { $status.Get_Item($_) }
    

    You could define the enum in Powershell, but the above works just as well, and defining enums in Powershell seems like a lot of work.

    Here is an article, that talks about how to use the bitwise operators in Powershell.

    0 讨论(0)
提交回复
热议问题