ERROR: Description = Invalid query

2019-05-05 19:06发布

Because of a messed up group policy object, multiple computers have TightVNC installed when they shouldn't. The GPO is gone, so just removing the software from there isn't an option that I'm aware of. Therefore, I'm scripting things in order to remove PowerShell from a list of computers.

This is my script:

if ($args.length -ne 1) {
    Write-Warning "Must pass computer name, ending script.";
    break
}

$pc = $args[0]

Write-Output "Scanning $pc for TightVNC...."
$prod = wmic /node:$pc product get name | where {$_ -match "TightVNC"}
if ($prod) {
    Write-Output "Found TightVNC, attempting uninstall...."
    wmic /node:$pc product where name="TightVNC" call uninstall
} else {
    Write-Warning "Could not find TightVNC on $pc."
}
Write-Output "Done."

Now, my output is as follows:

Scanning [computer] for TightVNC....
Found TightVNC, attempting uninstall....
ERROR:
Description = Invalid query
Done.

However, if I copy and paste the second wmic line into an elevated command prompt and replace $pc with [computer], it works just fine. My PowerShell window is elevated.

Does anyone know why my script would be having a fit about this? I know that it does take quite a long time for the first wmic command to complete (>=5 minutes), but it does as well in the second command window where it actually works. I'd appreciate any insight into this.

NOTE: I am using wmic because the computers here aren't properly configured for remote PowerShell access. It's on my list of things to do.

2条回答
祖国的老花朵
2楼-- · 2019-05-05 19:25

You're running afoul of PowerShell's string parsing. Try this instead:

wmic /node:$pc product where name=`"TightVNC`" call uninstall

Note, for those on PowerShell V3, you can use:

wmic /node:$pc --% product where name="TightVNC" call uninstall
查看更多
Emotional °昔
3楼-- · 2019-05-05 19:26

Here's an answer from http://www.tinyint.com/index.php/2011/04/20/escaping-quotes-in-powershell-exe-command-via-command-prompt/ that worked for me:

wmic /node:$pc product where 'name=\"TightVNC\"' call uninstall
查看更多
登录 后发表回答