代码之家  ›  专栏  ›  技术社区  ›  fenster

使用powershell的按位运算符

  •  21
  • fenster  · 技术社区  · 16 年前

    我正在寻找如何解决以下场景的示例:

    假设我的打印机具有“status”的以下属性
    0离线
    2-纸盘空
    4-碳粉耗尽
    8纸卡纸

    当我查询状态时,它返回一个12的值。很明显,这意味着打印机的墨粉用完了,卡纸了,但是我该如何用powershell解决这个问题呢?

    谢谢

    2 回复  |  直到 8 年前
        1
  •  36
  •   driis    8 年前

    powershell中的布尔位and运算符是 -band .

    假设您在哈希表中定义了值和描述,并且打印机中的值为12:

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

    然后,此语句将为您提供文本描述:

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

    能够 在powershell中定义枚举,但上面的方法同样有效, and defining enums in Powershell seems like a lot of work .

    Here is an article ,讨论了如何在powershell中使用位运算符。

        2
  •  8
  •   Keith Hill    16 年前

    你可以让powershell为你做更多的工作。下面是使用system.io.fileoptions的示例:

    PS> [enum]::GetValues([io.fileoptions]) | ?{$_.value__ -band 0x90000000}
    RandomAccess
    WriteThrough