Catch exitcode from EXE

asked 2020-09-05T09:43:39.89+00:00
Marcus Östman 21 Reputation points

Hi,
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"
$archive1="TMP.rar"
$argList = @("a", ('"'+$archive1+'"'), ("d:\Temp\exists\"))
Start-Process -FilePath $rar -ArgumentList $argList -Wait #-NoNewWindow
$LASTEXITCODE

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

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

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,615 questions
{count} votes

1 additional answer

Sort by: Most helpful
  1. answered 2020-09-07T19:31:53.057+00:00
    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