0

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
}
1
  • Are you asking how to accept the pipeline input in your function? Commented Aug 6, 2020 at 20:18

1 Answer 1

1

Solution for what exactly? You haven't stated a problem.

Anyway, in your code you name you your parameter "Jobs" then make it as [ValueFromPipelineByPropertyName]. Job objects don't have a Jobs property so this will not work. Consider a separate parameter Id for this. Also, rather than typing the parameter as [object[]], type it as [System.Management.Automation.Job[]] which is the type of the job object.

You should have logic in the process block to accumulate all of the Job objects then move the loops to the end block once all of the jobs have been gathered.

I've reformatted your code and cleaned it up a bit, but I still can't figure out what you're actually trying to do:

Function Wait-UntilJobFailOrSuccess
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory, ValueFromPipeline)]
        [System.Management.Automation.Job[]]
            $jobs
    )

    begin
    {
        $joblist = @()
    }

    process 
    {
        $joblist += $jobs
    }

    end
    {
        foreach ($job in $joblist)
        {
            if ($job.State -eq 'Failed') 
            {
                Stop-Job -Job $job 
                Write-Host "Aborting Job $(job.name)"
                break
            }
        }

        Start-Sleep -Milliseconds 500

        foreach ($job in $jobslist) 
        {
            if ($job.State -eq 'Failed')
            {
                Write-Host ($job.ChildJobs[0].JobStateInfo.Reason.Message) -ForegroundColor Red
            } 
            else 
            {
                Write-Host (Receive-Job $job) -ForegroundColor Green
            } 
        }
        $joblist | Remove-Job -Force # Is this what you really want to do?
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much for the suggestion. I modified the code what you suggested. I will tell you why I need to use the pipe. I'm having a lot of script block. As of now, I'm creating a variable called $jobs and getting all the jobs to a variable and then calling Wait-UntilJobFailOrSuccess. Just reduce this complexity I was trying to use the pipe. But it is not working for me.
Adding to above command, We can't take the pipe output as [System.Management.Automation.Job[]] in powershell from Get-Job command? is it like I need to explicity specificy what all column I required?
@Vipin A better practice is to either be explicit with your types ([object[]] isn't descriptive) or let the interpreter figure out the types implicitly.
Thank you very much for the suggestions BrucePayette. I'm very new to PowerShell. I did not know that there is something begin, process and end in PowerShell methods. If my questions confuse you sorry for that. I think I got my answers from your comments. Thank you so much once again for @TheIncorrigible1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.