I have written a function called Wait-UntilJobFailOrSuccess . This takes the output from Get-Job using pipe command line. For example.
Get-Job|Remove-Job
The same way I want to do for my function. For example
Get-Job | Wait-UntilJobFailOrSuccess
I also attached Wait-UntilJobFailOrSuccess below. Please let us know. Do anyone has a solution for this.
Function Wait-UntilJobFailOrSuccess
{
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[System.Object[]]$jobs
)
while ($true) {
if ('Failed' -in $jobs.State) {
$jobs | Stop-Job
Write-Host "Aborting Jobs ${result}"
break
}
Start-Sleep -Milliseconds 500
}
foreach ($job in $jobs) {
if ($job.State -eq 'Failed') {
Write-Host ($job.ChildJobs[0].JobStateInfo.Reason.Message) -ForegroundColor Red
} else {
Write-Host (Receive-Job $job) -ForegroundColor Green
}
}
$jobs|remove-Job
}