Powershell -Filter not accepting two conditions

微笑、不失礼 提交于 2019-12-04 05:35:41

问题


I have this command

$remoteuserlist = Get-WmiObject Win32_UserAccount `
-filter "LocalAccount =True" –computername $PC -verbose

that I am running to get a list of local accounts on a machine. I would also like to exclude the guest account from my list. so I tried something like this

$remoteuserlist = Get-WmiObject Win32_UserAccount `
-filter {LocalAccount =True -and Name -ne "Guest" –computername $PC -verbose}

but I get an invalid query error. Can someone explain my presumably blindingly obvious error?

Thanks


回答1:


$remoteuserlist = Get-WmiObject Win32_UserAccount -filter {LocalAccount = "True" and Name != "Guest"} –computername $PC -verbose
  1. You were mixing WMI syntax and PowerShell syntax
  2. The brackets encompassing the filter were around the other parameters of Get-WmiObject



回答2:


The WQL "not equal" operator is != or <>.

WQL Operators




回答3:


If you have a bunch of old VBScript WMI queries laying around you can use the Get-WMIObject -Query param to reuse them.

$remoteuserlist = Get-WmiObject -query "SELECT * FROM Win32_UserAccount WHERE LocalAccount = 'True' and Name != 'Guest'" –computername $PC -verbose

Not groundbreaking but it can help if you don't want to rewrite queries.



来源:https://stackoverflow.com/questions/28205132/powershell-filter-not-accepting-two-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!