使用msiexec的Powershell卸载程序

时间:2012-06-12 18:57:01

标签: powershell java

我遇到了一个问题,让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程序包。”

我知道它可以独立工作,而不是在这个脚本中......所以我认为这是一种语法问题。

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

1 个答案:

答案 0 :(得分:0)

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

"$app.IdentifyingNumber"

和这个

"$($app.IdentifyingNumber)"

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

&cmd /c "msiexec /uninstall $($app.IdentifyingNumber) /passive"