简体   繁体   中英

Unable to run PowerShell uninstall script

I am trying to start a process and wait for the exit code. The process starts msiexec with an argument list. When i run my script it comes up with the argument help wizard, however if i run the command directly in CMD generated by:

write-host $command

It works as expected. Here is my full script:

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

Instead of this

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

try

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

Source: see the last example https://kevinmarquette.github.io/2016-10-21-powershell-installing-msi-files/

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

It looks like you are trying to run msiexec.exe with ArgumentList "msiexec.exe /x {ProductCode}".

Powershell is trying to run your command as "msiexec.exe msiexec.exe /x {ProductCode}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM