中使用PowerShell的位运算符

中使用PowerShell的位运算符

本文介绍了中使用PowerShell的位运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要找的例子我将如何解决以下情况:

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

想象一下,我的打印机具有状态结果以下属性
0 -offline结果
2 - 纸托盘空结果
4上色剂用尽结果
8 - 纸卡纸

Imagine my printer has the following property for "Status"
0 -Offline
2 -Paper Tray Empty
4 -Toner Exhausted
8 -Paper Jam

当我查询状态返回的12值,我可以明显的看出这意味着打印机碳粉用尽,卡纸,但我将如何来解决这一问题使用PowerShell?

When I query status it returns a value of 12. I can obviously see that this means the printer has the Toner Exhausted and a Paper Jam, but how would I work this out with Powershell?

感谢

推荐答案

布尔按位和运营商PowerShell是波段

The boolean bitwise and operator in Powershell is -band.

假设你定义在哈希表中的值和描述,并有12个从打印机的值:

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($_) }

您的可能的定义PowerShell中的枚举,但上面的作品一样好,的的。

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.

,谈到有关如何在PowerShell中使用的位运算符。

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

这篇关于中使用PowerShell的位运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 00:41