Query Job Completion

PowerShell script exchange

Query Job Completion

Veeam Logoby crichardson » Thu Dec 08, 2011 2:18 pm

Is there any way to query if a backup job has completed from the command line? I think I can use PowerShell? Maybe I'm blind (high possibility), but I can't seem to locate the white papers for this...

Basically I would like to build a script which runs near the end of the backup window. It will query all Veeam Backup jobs one by one and output if they are completed or not. If they are not, the script will sleep for 15 minutes. If the jobs are complete the script will execute another command.

This will be used to kick off our HP Data Protector software to backup our repositories to tape. I have the white paper which was introduced a few years ago but unfortunately it does not fit my needs.
crichardson
Enthusiast
 
Posts: 39
Liked: never
Joined: Thu Dec 09, 2010 1:25 pm
Full Name: Corey

Re: Query Job Completion

Veeam Logoby Gostev » Thu Dec 08, 2011 2:22 pm

Sure, please check the PowerShell subforum.
Gostev
Veeam Software
 
Posts: 21385
Liked: 2348 times
Joined: Sun Jan 01, 2006 1:01 am
Location: Baar, Switzerland

Re: Query Job Completion

Veeam Logoby foggy » Thu Dec 08, 2011 2:23 pm

Corey, you can use this script to monitor current job status. Thanks.
foggy
Veeam Software
 
Posts: 14716
Liked: 1075 times
Joined: Mon Jul 11, 2011 10:22 am
Full Name: Alexander Fogelson

Re: Query Job Completion

Veeam Logoby Sethbartlett » Thu Dec 08, 2011 2:36 pm

In powershell you can do the following:

v6
Code: Select all
$job = Get-VBRJob -name "Jobname"
$job.GetLastState()


v5
Code: Select all
$job = Get-VBRJob | ?{$_.name -eq "Job Name"}
$job.GetLastState()
Skype: Sethbartlett88 - Make sure to label who you are and why you want to add me ;)
Twitter: @sethbartlett
If my post was helpful, please like it. Sometimes twitter is quicker to hit me up if you need me.
Sethbartlett
Expert
 
Posts: 282
Liked: 25 times
Joined: Wed Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett

Re: Query Job Completion

Veeam Logoby crichardson » Thu Dec 08, 2011 2:39 pm

You guys rock. I was looking for white papers and then I must have mistypes something in the forum search box, because I just tried again and found dozens of posts. Thanks!!!
crichardson
Enthusiast
 
Posts: 39
Liked: never
Joined: Thu Dec 09, 2010 1:25 pm
Full Name: Corey

Re: Query Job Completion

Veeam Logoby crichardson » Thu Dec 08, 2011 3:13 pm

This script appears to query all jobs and continue runing until all jobs are stopped - am I correct? The big issue with this is it seems to take replica's into account as well. Is there a way to only query backup jobs? I tried -BackupTargetType but that didn't do the trick.

Just so you know, I'm extremely new to PowerShell. I'm making the transition from VBScript (yeah yeah, I know... I'm behind the times...)

Code: Select all
Add-PSSnapin Veeampssnapin
$JobActive=0
Do {
foreach ($v in (Get-VBRJob))
{
   $JobStatus = ($v.GetLastState())
   If ($JobStatus -notlike "Stopped"){$JobActive ++}
}}
while($JobActive -notlike "0")
#Insert backup command here
crichardson
Enthusiast
 
Posts: 39
Liked: never
Joined: Thu Dec 09, 2010 1:25 pm
Full Name: Corey

Re: Query Job Completion

Veeam Logoby ThomasMc » Thu Dec 08, 2011 3:44 pm

Change your Get-VBRJob to this

Code: Select all
Get-VBRJob | ?{$_.IsBackup -eq $true}
ThomasMc
Expert
 
Posts: 293
Liked: 17 times
Joined: Wed Apr 13, 2011 12:45 pm
Full Name: Thomas McConnell

Re: Query Job Completion

Veeam Logoby crichardson » Thu Dec 08, 2011 4:05 pm

I added some Write-Host entries to help with troubleshooting. I ran the script below while a backup job was running. While the job is running the script outputs "Working 0" then "Working 1" and then "Working 2" and so on. I then stopped the backup job to see if the script would output "Run Script!". The job was stopped for 15 minutes and the script never stopped or outputted "Run Script!". Did I screw something up?

Code: Select all
Add-PSSnapin -Name Veeampssnapin -ErrorAction SilentlyContinue

$JobActive=0
Do {
foreach ($v in (Get-VBRJob | ?{$_.IsBackup -eq $true}))
{
   $JobStatus = ($v.GetLastState())
   If ($JobStatus -notlike "Stopped"){
        Write-Host $JobStatus $JobActive
        $JobActive ++
   }
}}
while($JobActive -notlike "0")
#Insert backup command here
Write-Host "Run Script!"
crichardson
Enthusiast
 
Posts: 39
Liked: never
Joined: Thu Dec 09, 2010 1:25 pm
Full Name: Corey

Re: Query Job Completion

Veeam Logoby Sethbartlett » Thu Dec 08, 2011 4:09 pm

You never decrement JobActive. So when it gets set to stopped, you do not --
Skype: Sethbartlett88 - Make sure to label who you are and why you want to add me ;)
Twitter: @sethbartlett
If my post was helpful, please like it. Sometimes twitter is quicker to hit me up if you need me.
Sethbartlett
Expert
 
Posts: 282
Liked: 25 times
Joined: Wed Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett

Re: Query Job Completion

Veeam Logoby jteager » Thu Dec 08, 2011 4:45 pm

So wait, are you trying to do like a "post-all-my-jobs" command? If so, you could probably just do something like
Code: Select all
asnp VeeamPSSnapin -ErrorAction SilentlyContinue

while(get-vbrjob | ?{$_.IsBackup  -and $_.GetLastState() -notlike "Stopped"}){
   sleep(10) # That value is in seconds -- this is so it doesn't tank your box :)
}

write-host "This is only printing if no jobs are running"


Does that make sense?
jteager
Veeam Software
 
Posts: 10
Liked: never
Joined: Mon Mar 28, 2011 2:13 pm
Full Name: Justin Teager

Re: Query Job Completion

Veeam Logoby ThomasMc » Fri Dec 09, 2011 10:44 am

Nicely done JT :)
ThomasMc
Expert
 
Posts: 293
Liked: 17 times
Joined: Wed Apr 13, 2011 12:45 pm
Full Name: Thomas McConnell

Re: Query Job Completion

Veeam Logoby crichardson » Fri Dec 09, 2011 3:16 pm

Nice! Thanks! I'm so far behind when it comes to PoweShell :cry:

So basically what I'm going to do is kick off the script at midnight every night. It will cotninue to run and check every X amount of seconds to see if a job is running (I'll probably change this to every 10 minutes or something). When the jobs are all stopped, it will kick off a command to run an on-demand HP Data Protector job which will send our entire backup repository to tape.
crichardson
Enthusiast
 
Posts: 39
Liked: never
Joined: Thu Dec 09, 2010 1:25 pm
Full Name: Corey

Re: Query Job Completion

Veeam Logoby davidl » Sun Dec 11, 2011 6:09 am

Have you considered using a post job activity on the backup which runs last in your backup set ?

We do this for any of our customers who are looking to use a backup to tape solution for their Veeam Data and have found it to be very reliable and compatible with pretty much any backup system which has some level of PowerShell Interaction support.

Provided you can write a PowerShell script or similar to initiate the backup on your tape backup system you can simply define a script to run when the final Veeam job completes.

By using a script to first output whether jobs are running and then having another script check the output is a less than optimal method to do what your trying to achieve imho

In Veeam v6 you can define the Post Job Script to only run on a specific day so if your only doing this on a weekend for example you don't need to add a check into the script to find out what day it is before executing you can just define the schedule within the Veeam Job.

Obviously everyones situation is different and it may be hard to define which backup will complete last in a given backup window for some but for many standard re-occurring schedules I have found the post job activity feature to be a very reliable method to get Veeam data written to tape.
davidl
Novice
 
Posts: 9
Liked: never
Joined: Sat Jul 23, 2011 6:23 am
Full Name: David Lee


Return to PowerShell



Who is online

Users browsing this forum: Yahoo [Bot] and 5 guests