无法运行PowerShell卸载脚本

时间:2017-10-10 10:01:52

标签: powershell msiexec

我正在尝试启动进程并等待退出代码。该过程使用参数列表启动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
}

3 个答案:

答案 0 :(得分: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/

答案 1 :(得分:1)

#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
}

答案 2 :(得分:0)

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

Powershell正试图以" msiexec.exe msiexec.exe / x {ProductCode}"

运行您的命令
相关问题