Catch exitcode from EXE

Marcus Östman 21 Reputation points

I'm trying to catch errorlevel/exitcode from a executable, both return 0. I don't understand why.

The folder "d:\Temp\exists\" exists.

$rar="c:\Program Files\WinRAR\Rar.exe"
$argList = @("a", ('"'+$archive1+'"'), ("d:\Temp\exists\"))
Start-Process -FilePath $rar -ArgumentList $argList -Wait #-NoNewWindow

The folder "d:\Temp\exists\" does NOT exists.

$rar="c:\Program Files\WinRAR\Rar.exe"
$argList = @("a", ('"'+$archive1+'"'), ("d:\Temp\doesNOTexists\"))
Start-Process -FilePath $rar -ArgumentList $argList -Wait #-NoNewWindow

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,745 questions
{count} votes

Accepted answer
  1. Rich Matheisen 36,246 Reputation points

    While you used the "-Wait" switch you haven't used the "-Passthru" switch and you haven't kept the process object returned by the Start-Process cmdlet. See this link: obtaining-exitcode-using-start-process-and-waitforexit-instead-of-wait

    When the process ends, check the "ExitCode" property of the process object.

    1 person found this answer helpful.
    No comments

1 additional answer

Sort by: Oldest
  1. Marcus Östman 21 Reputation points

    Thanks for the input.
    After some inspiration from the link you provided I went for this solution.

    function rar_update([String] $archive1, [String] $list1, $varName) {
    & "c:\Program Files\WinRAR\Rar.exe" a "$archive1" "$list1"
    Set-Variable -Name $varName -Value $LASTEXITCODE -Scope Global

    rar_update "Path/Backup.rar" "@mutia keyza /Backup.lst" "r_error1"
    if (($r_error1 -ne 0) ... #if not 0 handle it as an error