Waiting on job status

Hey guys!  I am writing a script that will back up our Hyper-V machines.  I want to do them at the same time but then wait for them all to be done...I'll need to move the files when they are finished. 

My question is, will this work?  I haven't tested it but I think it should show you the general idea of what I want.  I need to store the jobs in an array...then loop through the array waiting for them all to be finished or failed, anything but running.  Does the following code look ok?

Remove-Item "C:\Backup\*" -Recurse

$jobs[0] = Export-VM -Name COMPANYWEB -Path "C:\Backup" -AsJob
$jobs[1] = Export-VM -Name TFSDeploy -Path "C:\Backup" -AsJob
$jobs[2] = Export-VM -Name OldVSS -Path "C:\Backup" -AsJob
$jobs[3] = Export-VM -Name MattBuild -Path "C:\Backup" -AsJob
$jobs[4] = Export-VM -Name PSR -Path "C:\Backup" -AsJob
$jobs[5] = Export-VM -Name XPPortal -Path "C:\Backup" -AsJob

$done = true
do
{
    for ($i = 0; $i -le 6; $i++)
    {
        if ($jobs[$i].status -eq "Running")
	    $done = false
    }
} while ($done -eq false)



  • Edited by GaidenFocus Tuesday, March 31, 2015 3:42 PM
March 31st, 2015 3:42pm

Thanks this is how i got it to work.  What I'm working on now is finding a way to get the status.  I want it to retry if it fails to export at any time.

Is there a global method of finding the job status overall?  like if 1 out of 100 are running then it says yep!

so

while (jobs are running)
{
    foreach (job)
    {
        if (job status = failed)
        {
            restart job
        }
    }
}





  • Edited by GaidenFocus Tuesday, March 31, 2015 9:35 PM
Free Windows Admin Tool Kit Click here and download it now
March 31st, 2015 9:32pm

Ok what I've been asked to do is write it to an event.  How do i get the job info for an event log?

I was able to do Get-Job and put it in a text file which was awesome but the EventLog does not work.

	foreach($job in $jobs)
	{
		$text = Get-Job $job

		Write-EventLog -LogName Indico -Source HVBackup -EntryType Information -EventID 1 -Message $text
	}

April 3rd, 2015 4:00pm

Seeing as you opened a new thread for this question here https://social.technet.microsoft.com/Forums/windowsserver/en-US/40171b21-67cd-4d83-bde7-7b478dd01d6a/job-event-logger?forum=winserverpowershell#40171b21-67cd-4d83-bde7-7b478dd01d6a, I suggest you mark the correct answer on this one to close it and follow up on the new question you posed for answers specific to the problem you're having there.

Free Windows Admin Tool Kit Click here and download it now
April 3rd, 2015 4:28pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics