1

我遇到了让 msiexec 使用 Powershell 删除 java 的问题。我已将结果命令输出到屏幕并将其粘贴到批处理文件中,它运行良好。但是当它通过 Powershell 执行时,它会说“找不到包”。谁能发现我可能做错了什么?我在谷歌上下查找并尝试了几种不同的方式来执行命令而没有成功并获得相同的结果。

cls
$java = Get-WmiObject -Class win32_product | where { $_.Name -like "*Java*"}
$msiexec = "c:\windows\system32\msiexec.exe";
#$msiexecargs = '/x:"$app.LocalPackage" /qr'
$msiexecargs = '/uninstall "$app.IdentifyingNumber" /qr /norestart'

if ($java -ne $null)
{
    foreach ($app in $java)
    {
        write-host $app.LocalPackage
        write-host $app.IdentifyingNumber
        #&cmd /c "msiexec /uninstall $app.IdentifyingNumber /passive"
        #Start-Process -FilePath $msiexec -Arg $msiexecargs -Wait -Passthru
        [Diagnostics.Process]::Start($msiexec, $msiexecargs);
    }
}
else { Write-Host "nothing to see here..." }
Write-Host "check end"

目标是使用 Windows 7 登录脚本删除最终用户系统上的所有 Java 版本,然后安装最新版本。我更喜欢全部使用 Powershell,但如果我不能让它工作,我将只使用一个用卸载 GUID 硬编码的批处理文件

write-host 语句都是出于调试目的,我只是对以这种格式的某些变体执行 msiexec 感兴趣: msiexec /x {GUID} /passive /norestart

我得到的错误是:“无法打开此安装包。验证该包是否存在并且您可以访问它,或者联系应用程序供应商以验证这是一个有效的 Windows Installer 包。”

我知道它可以自己工作,只是不在这个脚本中......所以我认为这是一个语法问题。

如果您有任何问题,请告诉我。

4

1 回答 1

0

首先你必须知道这之间的区别:

"$app.IdentifyingNumber"

还有这个

"$($app.IdentifyingNumber)"

所以我认为你想使用后者(由于注释行,代码有点混乱):

&cmd /c "msiexec /uninstall $($app.IdentifyingNumber) /passive"
于 2012-06-12T19:03:43.960 回答