过滤器中的WQL不起作用 [英] WQL in filter doesn't work
问题描述
我正在尝试做类似的事情:
I'm trying to do something like:
Get-WmiObject Win32_NetworkAdapterConfiguration `
-Filter "DefaultIPGateway!=NULL"
但是我有一个错误:
Get-WmiObject:无效的查询 在线:1个字符:14个 + Get-WmiObject<<<< Win32_NetworkAdapterConfiguration-筛选器"DefaultIPGateway!= NULL" + CategoryInfo:InvalidOperation:(:) [Get-WmiObject],ManagementException + FullyQualifiedErrorId:GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Get-WmiObject : Invalid query At line:1 char:14 + Get-WmiObject <<<< Win32_NetworkAdapterConfiguration -Filter "DefaultIPGateway!=NULL" + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], ManagementException + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
这很奇怪,因为当我尝试获取DefaultIPGateway值的类型时.存在值是System.Array:
It's strange, because when I try to get type of DefaultIPGateway values. It's System.Array for the existent values:
PS> $result[0].DefaultIPGateway.Gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String[] System.Array
对于不存在的值,则为NULL:
And a NULL for the nonexistent values:
PS> $result[1].DefaultIPGateway.GetType()
You cannot call a method on a null-valued expression.
At line:1 char:36
+ $result[1].DefaultIPGateway.GetType <<<< ()
+ CategoryInfo : InvalidOperation: (GetType:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
有人可以帮助我理解为什么我的WQL不能正常工作,我应该怎么做才能使它成功?
Could someone help me to understand why my WQL doesn't work and what I should do to make it go?
推荐答案
我不确定如何使过滤查询有效,因为我不知道如何访问数组元素来检查它们,但是有一种解决方法:
I'm not sure how to make the filter query work as I don't know how to access the array elements to check them, but have a workaround:
Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.DefaultIPGateway -ne $null }
这样,powershell将负责过滤从查询返回的对象,而不是WMI在检索过程中执行此操作.
This way, powershell will be responsible for filtering the objects returned from the query, rather than WMI doing it during retrieval.
这篇关于过滤器中的WQL不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!