使用 AND 或 OR 获取 WmiObject(它们以相反的方式工作)
Get-WmiObject using AND or OR (they are working in the opposite manner)
使用多个条件(AND 和 OR)的 WMI 过滤似乎无法正常工作。事实上,AND 的行为就像 OR 应该的那样,而 OR 的行为就像 AND 应该的那样。也许只有我一个人,但我想知道是否有其他人看到这种行为。
我正在尝试使用 PowerShell 和 Get-WMIObject
获取 Windows 个共享列表,效果很好:Get-WmiObject -Class Win32_Share
我的大脑爆炸了,因为对我来说,使用 WQL 语句或 WMI 过滤器会导致 AND 和 OR 运算符以它们应有的完全相反的方式运行。
以这些分享为例:Admin$、C$、D$、TestShare
如果我想从我的结果中排除 C$ 和 D$,我可以使用 WQL 语句过滤它
("SELECT * from Win32_Share WHERE Name !='C$' OR Name !='D$'") or a WMI filter (-Filter "Name <>'C$' OR Name <> 'D$'")
不幸的是,当运行任一个选项时,说这个:
Get-WmiObject -Class Win32_Share -Filter "Name <>'C$' OR Name <> 'D$'"
PowerShell returns 所有四个共享的列表,Admin$、C$、D$ 和 TestShare。就好像 PowerShell 将 OR 视为 AND,因此条件不匹配,因此返回所有结果。
疯狂的是,如果我用 AND 替换 OR,我会得到我想要的结果。 运行 这个:
Get-WmiObject -Class Win32_Share -Filter "Name <>'C$' AND Name <> 'D$'"
给我一个共享列表:Admin$ 和 TestShare,不包括 C$ 和 D$。这似乎是 PowerShell 将 AND 视为 OR。我在 PowerShell 2、3 和 5 中对此进行了测试;每个结果相同。
在线查找使用 OR 示例的 WMI 筛选,它们完全符合人们的预期,所以我到底为什么会看到这种倒退的行为?
其他人能否重现我的问题,或者弄清楚为什么我会在我的环境中看到这个问题?谢谢
回到命题逻辑和布尔代数基础知识。特别地,De Morgan's laws用英文可以表示为:
- the negation of a disjunction is the conjunction of the negations; and
- the negation of a conjunction is the disjunction of the negations.
以这些分享为例:print$
、IPC$
、Test
、TestC
:
PS D:\PShell> Get-WmiObject -Class Win32_Share
Name Path Description
---- ---- -----------
IPC$ Remote IPC
print$ C:\Windows\system32\spool\drivers Ovladače tiskárny
test D:\test
testC C:\testC
只摘录print$
和IPC$
,那么你的过滤器应该是"Name='IPC$'
或 Name='print$'"
:
PS D:\PShell> Get-WmiObject -Class Win32_Share -Filter "Name='IPC$' or Name='print$'"
Name Path Description
---- ---- -----------
IPC$ Remote IPC
print$ C:\Windows\system32\spool\drivers Ovladače tiskárny
反之亦然,让我们从结果中 排除 print$
和 IPC$
即取反:
PS D:\PShell> Get-WmiObject -Class Win32_Share -Filter "not (Name='IPC$' or Name='print$')"
Name Path Description
---- ---- -----------
test D:\test
testC C:\testC
后面的结果对于以下过滤器是相同的(依次应用命题逻辑规则):
"not (Name = 'IPC$' or Name = 'print$')"
: 高于原来的
"not (Name = 'IPC$') AND not (Name = 'print$')"
:适用德摩根法
" Name <> 'IPC$' AND Name <> 'print$'"
:比较运算符取反
使用多个条件(AND 和 OR)的 WMI 过滤似乎无法正常工作。事实上,AND 的行为就像 OR 应该的那样,而 OR 的行为就像 AND 应该的那样。也许只有我一个人,但我想知道是否有其他人看到这种行为。
我正在尝试使用 PowerShell 和 Get-WMIObject
获取 Windows 个共享列表,效果很好:Get-WmiObject -Class Win32_Share
我的大脑爆炸了,因为对我来说,使用 WQL 语句或 WMI 过滤器会导致 AND 和 OR 运算符以它们应有的完全相反的方式运行。
以这些分享为例:Admin$、C$、D$、TestShare
如果我想从我的结果中排除 C$ 和 D$,我可以使用 WQL 语句过滤它
("SELECT * from Win32_Share WHERE Name !='C$' OR Name !='D$'") or a WMI filter (-Filter "Name <>'C$' OR Name <> 'D$'")
不幸的是,当运行任一个选项时,说这个:
Get-WmiObject -Class Win32_Share -Filter "Name <>'C$' OR Name <> 'D$'"
PowerShell returns 所有四个共享的列表,Admin$、C$、D$ 和 TestShare。就好像 PowerShell 将 OR 视为 AND,因此条件不匹配,因此返回所有结果。
疯狂的是,如果我用 AND 替换 OR,我会得到我想要的结果。 运行 这个:
Get-WmiObject -Class Win32_Share -Filter "Name <>'C$' AND Name <> 'D$'"
给我一个共享列表:Admin$ 和 TestShare,不包括 C$ 和 D$。这似乎是 PowerShell 将 AND 视为 OR。我在 PowerShell 2、3 和 5 中对此进行了测试;每个结果相同。
在线查找使用 OR 示例的 WMI 筛选,它们完全符合人们的预期,所以我到底为什么会看到这种倒退的行为?
其他人能否重现我的问题,或者弄清楚为什么我会在我的环境中看到这个问题?谢谢
回到命题逻辑和布尔代数基础知识。特别地,De Morgan's laws用英文可以表示为:
- the negation of a disjunction is the conjunction of the negations; and
- the negation of a conjunction is the disjunction of the negations.
以这些分享为例:print$
、IPC$
、Test
、TestC
:
PS D:\PShell> Get-WmiObject -Class Win32_Share
Name Path Description
---- ---- -----------
IPC$ Remote IPC
print$ C:\Windows\system32\spool\drivers Ovladače tiskárny
test D:\test
testC C:\testC
只摘录print$
和IPC$
,那么你的过滤器应该是"Name='IPC$'
或 Name='print$'"
:
PS D:\PShell> Get-WmiObject -Class Win32_Share -Filter "Name='IPC$' or Name='print$'"
Name Path Description
---- ---- -----------
IPC$ Remote IPC
print$ C:\Windows\system32\spool\drivers Ovladače tiskárny
反之亦然,让我们从结果中 排除 print$
和 IPC$
即取反:
PS D:\PShell> Get-WmiObject -Class Win32_Share -Filter "not (Name='IPC$' or Name='print$')"
Name Path Description
---- ---- -----------
test D:\test
testC C:\testC
后面的结果对于以下过滤器是相同的(依次应用命题逻辑规则):
"not (Name = 'IPC$' or Name = 'print$')"
: 高于原来的"not (Name = 'IPC$') AND not (Name = 'print$')"
:适用德摩根法" Name <> 'IPC$' AND Name <> 'print$'"
:比较运算符取反