Powershell:从脚本调用时,msiexec /X 抛出错误 1619

Powershell: msiexec /X throws error 1619 when call from script

我确实写了一个脚本来从 windows 7 台机器上卸载 java:

[...]
$p2=start-process "msiexec.exe" -arg "/X $uninstall32 /qn REMOVE=ALL /norestart " -PassThru -wait -verb runAs
$p2.WaitForExit()
[...]

哪里像 $uninstall32 = {26A24AE4-039D-4CA4-87B4-2F03217065FF}

如果我直接以管理员身份调用这些 ps1 文件,一切顺利。 为了更新过程,我必须从 .bat 文件调用我的(工作)ps1 文件。这些以这种方式调用我的 ps1 文件

if exist "%programfiles%\java\jre7" (

    powershell.exe -NoProfile -Command "Set-ExecutionPolicy Bypass"
    powershell.exe -NoProfile -file %~dp0uninstalljava7.ps1
    powershell.exe -NoProfile -Command "Set-ExecutionPolicy "restricted"
)

那么 erythin 是否出错:msiexec 抛出 1619?

我不明白?!


已解决:

对我来说可行的解决方案是:

Set-StrictMode -Version 2

$uninstall32key = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
$uninstall64key = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"

$hklm32 = [Microsoft.Win32.RegistryKey]::OpenBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, [Microsoft.Win32.RegistryView]::Registry32)
$hklm64 = [Microsoft.Win32.RegistryKey]::OpenBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, [Microsoft.Win32.RegistryView]::Registry64)

$key32 = $hklm32.OpenSubKey($uninstall32key)
$key64 = $hklm64.OpenSubKey($uninstall64key)

$subkeys32 = $key32.GetSubKeyNames()
$subkeys64 = $key64.GetSubKeyNames()


foreach($subkey in $subkeys32)
{
    $key = $hklm32.OpenSubKey($uninstall32key+"\"+$subkey)
    $displayName = $key.GetValue("DisplayName")

    if ($displayName -match "Java 7")
    {
        $uninstall32 =$key.GetValue("UninstallString") 
        if ($uninstall32) {
        $uninstall32 = $uninstall32 -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
        $params = @{
            "FilePath" = "$Env:SystemRoot\system32\msiexec.exe"
            "ArgumentList" = @(
                "/x"
                $uninstall32
                "/qn"
                "REMOVE=ALL"
                "/norestart"
            )
            "Verb" = "runas"
            "PassThru" = $true
            }
            $app1 = start-process @params
            $app1.WaitForExit()
        }
    }  
}

foreach($subkey in $subkeys64)
{
    $key = $hklm64.OpenSubKey($uninstall64key+"\"+$subkey)
    $displayName = $key.GetValue("DisplayName")

    if ($displayName -match "Java 7")
    {
        $uninstall64 =$key.GetValue("UninstallString") 
        if ($uninstall64) {
        $uninstall64 = $uninstall64 -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
        $params = @{
            "FilePath" = "$Env:SystemRoot\system32\msiexec.exe"
            "ArgumentList" = @(
                "/x"
                $uninstall64
                "/qn"
                "REMOVE=ALL"
                "/norestart"
            )
            "Verb" = "runas"
            "PassThru" = $true
            }
            $app1 = start-process @params
            $app1.WaitForExit()
        }
    }  
} 

此外,启动进程参数 -ArgumentList(别名:您的示例中的 -arg)接受 ARRAY 参数。传递单个字符串无法正常工作。

这样试试:

$Args = @( "/X", "$uninstall32", "/qn", "REMOVE=ALL",  "/norestart" )
$p2=start-process "msiexec.exe" -argumentList $args -PassThru -wait -verb runAs

此外,如果“$Uninstall32”中的美元符号是安装程序的参数,则应将其写在单引号之间:'$uninstall32'

这样试试:

$appGUID = "{26A24AE4-039D-4CA4-87B4-2F03217065FF}"
$params = @{
  "FilePath" = "$Env:SystemRoot\system32\msiexec.exe"
  "ArgumentList" = @(
    "/x"
    $appGUID
    "/qn"
    "REMOVE=ALL"
    "/norestart"
  )
  "Verb" = "runas"
  "PassThru" = $true
}
$app = start-process @params
$app.WaitForExit()