【问题标题】:Unable to run PowerShell uninstall script无法运行 PowerShell 卸载脚本
【发布时间】:2018-03-21 16:41:16
【问题描述】:

我正在尝试启动一个进程并等待退出代码。该进程使用参数列表启动 msiexec。当我运行我的脚本时,它会出现参数帮助向导,但是如果我直接在由以下生成的 CMD 中运行命令:

write-host $command

它按预期工作。这是我的完整脚本:

# Get uninstall strings from registry, looking for the msiexec option
$applist = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall  |
    Get-ItemProperty |
        Where-Object {$_.DisplayName -match "Microsoft Visio Standard 2013" -and $_.UninstallString -match "msiexec"} |
            Select-Object -Property DisplayName, UninstallString

# Check for any aplications requiring uninstall and output their names
if ($applist) {
    foreach ($app in $applist) {
        Write-host "$($app.DisplayName) has been detected for uninstall"
    }

    Write-host "Attempting to uninstall application(s)"

    # Uninstall each application that has been identified
    foreach ($app in $applist) {
        try {
             $uninst = $app.UninstallString
             $pos = $uninst.IndexOf(" ")
             $leftPart = $uninst.Substring(0, $pos)
             $rightPart = $uninst.Substring($pos+1)
             $command = """$rightPart /qn /L*V ""C:\UninstallVisio.txt"""""
             write-host $command
            $uninstall = (Start-Process "msiexec.exe" -ArgumentList $command -Wait -Passthru).ExitCode

            if($uninstall.ExitCode -ne 0) {
                write-host "attempting XML config uninstall"
                #**still to be worked on**
            }
        } catch {
            write-host "Unable to uninstall $_.Name Please view logs" 
            Continue
        }
    }
}
Exit
# Exit script as no apps to uninstall
else {
    write-host "No application(s) detected for uninstall"
    Exit
}

【问题讨论】:

    标签: powershell windows-installer


    【解决方案1】:

    您似乎正在尝试使用 ArgumentList "msiexec.exe /x {ProductCode}" 运行 msiexec.exe。

    Powershell 正在尝试将您的命令作为“msiexec.exe msiexec.exe /x {ProductCode}”运行

    【讨论】:

    • 好点!会尝试修改,看看我现在如何!
    【解决方案2】:

    而不是这个

    $command = "$uninst /qn /L*V ""C:\UninstallVisio.txt"""
    

    试试

    $command = @(
        $uninst
        "/qn"
        "/L*V"
        '"C:\UninstallVisio.txt"'
         )
    

    来源:见最后一个例子 https://kevinmarquette.github.io/2016-10-21-powershell-installing-msi-files/

    【讨论】:

    • 完美,结合下面的解决方案。感谢您的帮助!
    • 几个月前我也经历过同样的痛苦,并用它修复了它!
    【解决方案3】:
    #Get uninstall strings from registry, looking for the msiexec option
    $applist = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall  |
        Get-ItemProperty |
            Where-Object {$_.DisplayName -match "Microsoft Visio Standard 2013" -and $_.UninstallString -match "msiexec"} |
                Select-Object -Property DisplayName, UninstallString
    
    #Check for any aplications requiring uninstall and output their names
    if ($applist){
    foreach ($app in $applist){
    Write-host "$($app.DisplayName) has been detected for uninstall"
    }
    
    
    Write-host "Attempting to uninstall application(s)"
    
    
    #Uninstall each application that has been identified
    foreach ($app in $applist){
    try
    {
            $uninst = $app.UninstallString
            $pos = $uninst.IndexOf(" ")
            $leftPart = $uninst.Substring(0, $pos)
            $rightPart = $uninst.Substring($pos+1)
            $command = @(
            $rightPart
            "/qn"
            "/L*V"
            '"C:\UninstallVisio.txt"'
             )
            write-host $command
            $uninstall = (Start-Process "msiexec.exe" -ArgumentList $command -Wait -Passthru).ExitCode
            If($uninstall.ExitCode -ne 0){
            write-host "attempting XML config uninstall"
            }
           }
    
    catch{
    write-host "Unable to uninstall $_.Name Please view logs" 
    Continue
        }
    Exit
    }
    }
    #Exit script as no apps to uninstall
    else {
    write-host "No application(s) detected for uninstall"
    Exit
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-05
      • 2021-05-13
      • 2016-07-13
      • 2013-01-02
      • 2016-09-14
      • 1970-01-01
      • 1970-01-01
      • 2017-05-26
      • 1970-01-01
      相关资源
      最近更新 更多