Search code examples
powershellexit-codestart-process

Why is Powershell returning no ExitCode on Windows 7


I'm trying to write a script to install software on many computers. The problem is, they have to be installed in specific order and I need to know that the first installation succeeded before running the second one.

here's the important code:

[...]

# source_one
$Argumente = @("/i", $path_to_source, "/qb", "ADDLOCAL=ALL")
if (!$visWeb_upToDate)    { 
  Write("VIS Web-Client Installation...")
  $procWeb = Start-Process msiexec.exe -ArgumentList $Argumente -Verb runAs -PassThru 
  $procWeb.WaitForExit()
  Write-Debug($procWeb.ExitCode)

  if ($procWeb.ExitCode -eq 0){
    Write("... erfolgreich.")
    $visWeb_upToDate = $true;
  }
  else {
    Write-Error("... nicht erfolgreich.")
    Write-Error("Bitte Clients manuell installieren.")
  }
}

# source_two
if ($visWeb_upToDate -and !$vis64_upToDate){
  Write("VIS 64-Bit Client Installation...")
  $Argumente[1] = $path_to_another_source
  $procWeb64 = Start-Process msiexec.exe -ArgumentList $Argumente -Verb runAs -PassThru 
  $procWeb64.WaitForExit()
  Write-Debug($procWeb64.ExitCode)

  if ($procWeb64.ExitCode -eq 0){
    Write("... erfolgreich.")
    $vis64_upToDate = $true
  }
  else {
    Write-Error("... nicht erfolgreich.")
    Write-Error("Bitte 64-Bit Client manuell installieren.")
  }
}

[...]

I don't get $procWeb.ExitCode on Windows7 Powershell. On Win8 it works just fine and installs source_one and source_two only on success of the first.

Does somebody know how to fix this, or is there some other way to set $visWeb_upToDate = $true for both systems?

Many thanks in advance.


Solution

  • Your issue is identified as a bug in powershell.

    You can get the exitcode, but only after Calling the .HasExited property of the process and using a strange syntax.

    So as an example change the code to:

    if ($procWeb.HasExited -and ($procWeb.GetType().GetField("exitCode", "NonPublic,Instance").GetValue($procWeb)) -eq 0){
      Write("... erfolgreich.")
      $visWeb_upToDate = $true;
    }
      else {
        Write-Error("... nicht erfolgreich.")
        Write-Error("Bitte Clients manuell installieren.")
      }
    }
    

    Find the details on the bug here