0

I have a couple of Start-Job in a powershell script, I'd like to know if there is a better way to know if one of them Failed, this is what I got so far, thanks.

#Wait for the background jobs
$Jobs | Wait-Job
#Get the data from them
$Data = $Jobs | Receive-Job

Write-Host "Printing Data"


foreach($job in $Jobs)
{
    if($job.State -eq "Failed")
    {
        exit -1
    }
}

exit 0
Alex
  • 2,247
  • 1
  • 27
  • 37

1 Answers1

1
exit (0, -1)[$Jobs.State -contains 'Failed']
  • Using member-access enumeration, $Jobs.Failed returns the .State property values of all jobs in array $Jobs as an array.

  • -contains 'Failed' tests the array of values for containing string Failed.

  • [...] uses the resulting Boolean as an index into array 0, -1; if the Boolean is $false, it is coerced to index 0, if it is $true it is coerced to 1, thereby choosing the appropriate exit code.

mklement0
  • 382,024
  • 64
  • 607
  • 775