使用PowerShell变量卸载ESET软件失败问题求助
解决PowerShell传递变量卸载ESET的问题
问题根源是PowerShell的引号解析规则:单引号包裹的内容会被当作纯文本,变量不会被展开。你之前的代码里用单引号传递参数,$productID并没有被替换成实际的GUID,msiexec收到的是字符串"$productID",自然找不到对应的安装包。
修正后的代码
把参数的单引号换成双引号,让PowerShell解析变量:
$product = Get-WmiObject -Class Win32_Product | Where-Object Name -EQ "ESET Endpoint Antivirus" $productID = $product.IdentifyingNumber # 使用双引号包裹参数,让变量被正确解析 Start-Process msiexec "/qn /x $productID PASSWORD=`"password`" REBOOT=`"ReallySuppress`""
或者用更清晰的参数拆分写法,通过-ArgumentList逐个传递参数:
Start-Process msiexec -ArgumentList "/qn", "/x", $productID, "PASSWORD=`"password`"", "REBOOT=`"ReallySuppress`""
额外优化建议
Get-WmiObject已被PowerShell弃用,推荐使用兼容性更好的Get-CimInstance替代:
$product = Get-CimInstance -ClassName Win32_Product | Where-Object Name -EQ "ESET Endpoint Antivirus" $productID = $product.IdentifyingNumber Start-Process msiexec -ArgumentList "/qn", "/x", $productID, "PASSWORD=`"password`"", "REBOOT=`"ReallySuppress`""
内容的提问来源于stack exchange,提问作者Kyle Caruthers
相关产品推荐
相关产品推荐

