geekdu972
geekdu972

Reputation: 199

Install a .msi file and display the exit code of the process(.msi file)

I want to install a .msi file (it's the software winrar) and display the exit code of the process(.msi file). But I get this error:

PS D:\powershell> d:\powershell\boucle.ps1
InvalidOperation: D:\powershell\boucle.ps1:20:1
Line |
  20 |  $p.WaitForExit()
     |  ~~~~~~~~~~~~~~~~
     | You cannot call a method on a null-valued expression.

Then, I can't install the .msi file. Is it a good method in order to install a .msi file? Do you have a solution please?

Here is my code:

function executemsifile {

    param( [string]$msiFile )

    $arguments = @(
        "/i"
        "`"$msiFile`""
        "/q"
        "/l*vx"
        "log.txt"
    )
    
    $p = Start-Process -FilePath msiexec.exe -ArgumentList $arguments
    $p.WaitForExit()
    $p.ExitCode
    Write-Host $p.ExitCode      
}

executemsifile "D:\update4\wrar_600_64bit.msi"

Upvotes: 0

Views: 302

Answers (1)

zett42
zett42

Reputation: 27806

You are missing the -PassThru argument of Start-Process, so you don't receive output and $p will be $null.

As mklement0 noted, a possible simplification is to also use the -Wait switch instead of a separate $p.WaitForExit() call.

$p=Start-Process -FilePath msiexec.exe -ArgumentList $arguments -Wait -PassThru
$p.ExitCode

Upvotes: 2

Related Questions