使用Get-WmiObject卸载 [英] Uninstalling using Get-WmiObject
问题描述
我想在一个批处理脚本运行PowerShell命令。我试着去删除客户端PC和我的生活的老RMM工具的所有痕迹不能得到这条线正常运行。
I'm trying to run a PowerShell command in a batch script. Im trying to remove all traces of an old RMM tool from client PCs and for the life of me can't get this line to run correctly.
我们的想法是,这个搜索具有在供应商名称为N-能并传递GUID到 $ nableguid
变量,然后软件 msiexec.exe的
对上述发现的GUID。
The idea is that this searches for software that has N-Able in the vendor name and passes the GUID to the $nableguid
variable and then msiexec.exe
against the GUIDs found above.
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {$nableguid = (Get -WmiObject Win32_Product -Filter "vendor LIKE '%N-able%'" | Select -ExpandProperty IdentifyingNumber); foreach ($nguid in $nableguid){ & MsiExec.exe /X$nguid /quiet} }";
电流误差是如下
Get-WmiObject : A positional parameter cannot be found that accepts argument ''.
At line:1 char:18
+ & {$nableguid = (Get-WmiObject Win32_Product -Filter vendor LIKE '' | Select -Ex ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetWmiObjectCommand
我知道,这归结为业务的订单和报价当前组织,但与订购了一个小时左右后,搞乱我仍然无法得到它的正常工作。
I know that this boils down to orders of operations and the current organization of quotations, but after messing around with the ordering for an hour I still cant get it to work correctly.
推荐答案
您会收到这个错误,因为你使用的是双引号字符串内转义百分比字符和双引号:
You're getting that error, because you're using unescaped percent characters and double quotes inside a double-quoted string:
"& {... -Filter "vendor LIKE '%N-able%'" ...}"
上面的计算结果为 -Filter厂商LIKE''
在执行PowerShell命令,即只标记厂商
作为参数传递给参数 -Filter
通过。 LIKE
和作为单独传递的位置的参数。
The above evaluates to -Filter vendor LIKE ''
when the PowerShell command is executed, i.e. only the token vendor
is passed as an argument to the parameter -Filter
. LIKE
and ''
are passed as separate positional parameters.
您必须逃离嵌套双引号preserve他们的PowerShell命令:
You must escape the nested double quotes to preserve them for the PowerShell command:
"& {... -Filter \"vendor LIKE '%N-able%'\" ...}"
您还必须加倍%
字符(这就是他们是如何在批处理/ CMD转义),否则%N-能够%
将是的PowerShell执行之前PTED为批处理变量,扩展到一个空字符串间$ p $
命令行。
You must also double the %
characters (that's how they are escaped in batch/CMD), otherwise %N-able%
would be interpreted as a batch variable and expanded to an empty string before the execution of the powershell
commandline.
"& {... -Filter \"vendor LIKE '%%N-able%%'\" ...}"
随着中说,一般来说它会简单得多实施PowerShell的code在PowerShell脚本:
With that said, in general it would be far simpler to implement the PowerShell code in a PowerShell script:
$nableguid = Get -WmiObject Win32_Product -Filter "vendor LIKE '%N-able%'" |
Select -ExpandProperty IdentifyingNumber)
foreach ($nguid in $nableguid) {
& MsiExec.exe /X$nguid /quiet
}
和运行通过 - 文件
参数的脚本(如果你必须从一个批处理文件摆在首位运行它):
and run that script via the -File
parameter (if you must run it from a batch file in the first place):
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\path\to\script.ps1"
这是更好的方法是完全省略批处理脚本,并在PowerShell中执行的一切。
An even better approach would be to drop the batch script entirely and implement everything in PowerShell.
这篇关于使用Get-WmiObject卸载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!