3

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
}

3 Answers 3

2

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/

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect, combined with the solution below. Thanks for the help!
i've been through the same pain few months back and fixed it using that!
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
}

Comments

0

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

1 Comment

Good point! will try and amend that see how i get on for now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.