Powershell -Filter not accepting two conditions

2020-05-03 19:11发布

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

3条回答
【Aperson】
2楼-- · 2020-05-03 19:20

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.

查看更多
看我几分像从前
3楼-- · 2020-05-03 19:32

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

WQL Operators

查看更多
劫难
4楼-- · 2020-05-03 19:40
$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
查看更多
登录 后发表回答