0

Morning folks,

I'm working on a Do/Until loop that tracks an invoke-command job against multiple servers. What I don't know how to capture in the Until block is to continue doing the loop until all servers report a Completed status.

below is my test code

Invoke-Command -ComputerName Server01,Server02,Server03 -ScriptBlock{sleep -s 120} -AsJob -JobName "Test Job"

Do{
Write-Host "Job is still running"
sleep -s 10
}Until((Get-Job "Test Job").state -eq "Completed"

I'm thinking I need to put the following in the Until block but I'm not sure

Until(ForEach($job in (Get-Job "Test Job")){
$Job.State -eq "Completed"
})
Fitzgery
  • 558
  • 5
  • 14
  • 2
    wait-job or receive-job -wait – js2010 Oct 12 '22 at 13:56
  • Are you doing this for a specific reason? Like tracking progress? – Santiago Squarzon Oct 12 '22 at 14:35
  • 1
    @SantiagoSquarzon yes I'm doing it to track progress. – Fitzgery Oct 12 '22 at 16:08
  • Does this answer your question? https://stackoverflow.com/questions/71918277/add-write-progress-to-get-job-wait-job/71918903#71918903 – Santiago Squarzon Oct 12 '22 at 16:13
  • 1
    @SantiagoSquarzon that post seems to be a better answer to what I was looking for than I thought. I’ve given it some slight testing today and will continue into tomorrow – Fitzgery Oct 12 '22 at 23:32
  • @SantiagoSquarzon I’m wondering if you could help me out a bit here. I’m taking your function from your answer you posted above. I’m wanting to change the progress bar into seconds remaining from ``timeout`` value, but I can’t for the life of me figure that part out and could use a nudge in the right direction – Fitzgery Oct 13 '22 at 18:43
  • I think it's worth asking a new question for this, you can make a reference of my function. I'm pretty busy right now but likely you can get a proper answer – Santiago Squarzon Oct 13 '22 at 18:56

1 Answers1

0

I don't have the means to test this at the moment, but if you're after a progress output, I don't see why this wouldn't work.

$jobs = Invoke-Command -ComputerName Server01,Server02,Server03 -ScriptBlock{sleep -s 120} -AsJob
do {
    foreach ($job in ($jobs.ChildJobs | ? 'State' -EQ 'Running')
    {
        Write-Host -Object (
            "Job {0} is still running on computer {1}." -f $job.Name, $job.Location
        )
    }
    Start-Sleep -Seconds 1
} until ($jobs.ChildJobs.State -notcontains 'Running')

As mentioned above, I don't have the means to test this, nor do I recall if the State property you'd get from the parent job takes into account for the child jobs running. Technically speaking, what you tried would have worked as well given it had the closing parenthesis.

Do{
Write-Host "Job is still running"
sleep -s 10
}Until((Get-Job "Test Job").state -eq "Completed")

I didn't account for every issue you may come across but one thing you may be wary of is that the State property may not always be in a "Completed", or "Running" state. This could lead to an indefinite loop testing for a "Completed" status.