'I'm trying to handle exeption with powershell on Azure DevOps - try{./app.exe} catch{ Write-Output $PSItem.Exception; exit(1)}

When building a pipeline in Azure DevOps I am trying to use in powershell

try{./app.exe} catch{ Write-Output $PSItem.Exception; exit(1)}

and after app execution it should return true (interrupt a pipe step) or false (mark successfully complete).

What am I doing wrong? Pipeline ends sucessfully all times.



Solution 1:[1]

Make sure the $ErrorActionPreference variable is set to Stop in the current scope:

function Test-ExeInvocationFailure {

  $ErrorActionPreference = 'Stop'

  try {
    $null = ./app.exe
  }
  catch {
    return $true
  }

  return $LASTEXITCODE -ne 0
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mathias R. Jessen