Scheduled task to report on running backup jobs

PowerShell script exchange

Scheduled task to report on running backup jobs

Veeam Logoby GB@CFS » Wed Nov 21, 2012 2:16 am

Hi,

I want to write a script that will return the details of any running SureBackup jobs. I know that using the VEEAM powershell, I can get the status of jobs simply with Get-VBRJob.
However, as it needs to be scheduled, I'll need to add the snap-in first to the Windows Powershell. After doing so and running the same command, the result is very detailed for each job, in which none of the values contain the state of the job, which is the basis for this script.

Does anyone have any experience in this?

Cheers,
GB
GB@CFS
Novice
 
Posts: 8
Liked: never
Joined: Sun Oct 28, 2012 9:50 pm
Full Name: Gil Blumberg

Re: Scheduled task to report on running backup jobs

Veeam Logoby Vitaliy S. » Wed Nov 21, 2012 9:26 am

Hi Gil,

I'm not a PowerShell expert, but I believe you can determine the job current state from this script (at least it worked well for previous versions of Veeam B&R):
Code: Select all
(Get-VBRJob JobName).GetStatus()

Thanks!
Vitaliy S.
Veeam Software
 
Posts: 19558
Liked: 1102 times
Joined: Mon Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov

Re: Scheduled task to report on running backup jobs

Veeam Logoby v.Eremin » Wed Nov 21, 2012 10:29 am 1 person likes this post

Hi.

As far as I can understand, GetStatus() method doesn’t exist any longer, it’s GetLastState() which is now playing such role. Moreover, there is separate cmdlet responsible for getting SureBackup Jobs, which stands for Get-VSBJob.

So, if I were you and if I wanted display data on my screen, I’d write something like this:

Code: Select all
asnp VeeamPSSnapin
$jobs = Get-VSBJob
foreach($jobtoshow in $jobs)
{
 Write-Host($jobtoshow.name, $jobtoshow.GetLastState())
}


And I’d play a little bit with Export cmdlet, if I wanted to output information regarding SBJob’s status in file.

Hope this helps
Thanks.
v.Eremin
Veeam Software
 
Posts: 13266
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Scheduled task to report on running backup jobs

Veeam Logoby GB@CFS » Wed Nov 21, 2012 8:49 pm

Thanks, that is exactly what I need.

I don't have sufficient experience in Powershell at this stage to fully understand the construct of that code, and no doubt it would have taken me some time to get the same result.
As you wrote, just need to do some more work to get that result exported into a variable so that can be included in an email.

Cheers,
Gil
GB@CFS
Novice
 
Posts: 8
Liked: never
Joined: Sun Oct 28, 2012 9:50 pm
Full Name: Gil Blumberg

Re: Scheduled task to report on running backup jobs

Veeam Logoby GB@CFS » Thu Nov 22, 2012 3:56 am

I've made some progress but still can't get what I want.

I replaced "host" with "output" so that it is captured in a variable, so looks like this:
Code: Select all
$Result = Write-Output($jobtoshow.name, $jobtoshow.GetLastState())


What I want to with that variable is include it as the body of the email which gets sent. While that generally works, there are 2 problems with that.
The first being that it doesn't include a carriage return between listing each job. so the output is just one long line.
The other problem is that I only want the output to include any jobs that whose status is "working."

To achieve this I tried doing the following but it made no difference:
Code: Select all
$Result = foreach($jobtoshow in $jobs)
{
Write-Output($jobtoshow.name, $jobtoshow.GetLastState()) | where {$_.$result -contains "Stopped"}}
}


I also tried:
Code: Select all
Write-Output($jobtoshow.name, $jobtoshow.GetLastState()) | where {$_.$jobtoshow.GetLastState()) -contains "Stopped"}}


I'm in quite unfamiliar territory here, suggestions?
GB@CFS
Novice
 
Posts: 8
Liked: never
Joined: Sun Oct 28, 2012 9:50 pm
Full Name: Gil Blumberg

Re: Scheduled task to report on running backup jobs

Veeam Logoby v.Eremin » Thu Nov 22, 2012 9:14 am 1 person likes this post

Hi again, Gil.

What about this solution:

1)Create a .txt-file, which can be simply attached to any e-mail, and in which you’re going to store needed information. Say, it’d be c:\temp\test.txt. (Or,otherwise, you can create it via PowerShell console)

2)Then implement this script:

Code: Select all
asnp VeeamPSSnapin
$logFile = 'c:\temp\test.txt'
Clear-Content c:\temp\test.txt
$jobs = Get-VSBJob | ? {$_.GetLastState() -ne "Stopped"}
foreach($jobtoshow in $jobs)
{
  $jobtoshow.name + " " + $jobtoshow.GetLastState() >> $logFile
}

Be aware that in the begging this script cleans all data previously stored in .txt-file. (Clear-Content). It has been made to avoid the situation when you just add new information to the old one, causing confusion.

If you don’t understand some part of the script or which role it’s playing don’t hesitate to let me know for further explanations.

Furthermore, feel free to share or modify it, since it’s nothing but a draft-work.

Hope this helps.
Thanks.
v.Eremin
Veeam Software
 
Posts: 13266
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Scheduled task to report on running backup jobs

Veeam Logoby GB@CFS » Thu Nov 22, 2012 8:27 pm

Thanks Vladimir, that is really great.

Looks like I was on the right track piping to my where statement but just not quite there.
As we are not likely to have many SB jobs running at any one time, I do still prefer to write to a variable.
In the next part of the script, I generate an email with the body of the email being the contents of the variable. I'm sure it can be done and will continue to work on it, If not, will just include the attachment.

I don't fully understand how you managed to actually get the state of each job (ie. working or stopped). That's really what kicked this whole thing off.
Running
Code: Select all
Get-VSBJob | ? {$_.GetLastState() -ne "Stopped"}
still doesn't return the actual state of "stopped". So how does the next part of the code do this?

And where does "GetLastState()" fit into all this? It isn't a value that I can see from Get-VSBJob. Is it some sort of built-in/reserved field? Are there other qualifiers other than "()"?
GB@CFS
Novice
 
Posts: 8
Liked: never
Joined: Sun Oct 28, 2012 9:50 pm
Full Name: Gil Blumberg

Re: Scheduled task to report on running backup jobs

Veeam Logoby GB@CFS » Thu Nov 22, 2012 11:17 pm

Change code slightly so that output just shows active jobs, the result of which becomes the body of the email. Adjusted code is now:

Code: Select all
asnp VeeamPSSnapin
$jobs = Get-VSBJob | ? {$_.GetLastState() -ne "Stopped"}
$Result = foreach($jobtoshow in $jobs)
{
$jobtoshow.name
}


Last bit is to trigger the email only if any jobs are in "working" state. I'll post the completed code once done
GB@CFS
Novice
 
Posts: 8
Liked: never
Joined: Sun Oct 28, 2012 9:50 pm
Full Name: Gil Blumberg

Re: Scheduled task to report on running backup jobs

Veeam Logoby v.Eremin » Fri Nov 23, 2012 8:42 am

I don't fully understand how you managed to actually get the state of each job (ie. working or stopped). That's really what kicked this whole thing off.
Running
Code: Select all
Get-VSBJob | ? {$_.GetLastState() -ne "Stopped"}
still doesn't return the actual state of "stopped". So how does the next part of the code do this?


Thanks for feedback.

I’ll try my best to shed light on dark sides of the script.

First of all, by the means of this part:
Code: Select all
$jobs = Get-VSBJob | ? {$_.GetLastState() -ne "Stopped"}

I don’t get job state, however, I do get list of the jobs which state at the moment isn’t equal to “Stopped”. All the jobs with status like “starting”, “running” will fall into this category. Note:"?" means “where”, "-ne" can be interpreted as “not equal”

After having received a list of the jobs, which state isn’t equal to “Stopped”, we go over them in cycle, outputting their name in file or at the screen.
That's all.

And where does "GetLastState()" fit into all this? It isn't a value that I can see from Get-VSBJob. Is it some sort of built-in/reserved field? Are there other qualifiers other than "()"?


GetLastState() along with GetOptions(), SetOptions(), is a method of our variable received by Get-VBS/VBRJob.

Furthermore, all available methods can be easily seen in PowerShell ISE. All you need is:

1)Assign to variable a result of implementation some function. Be aware that in this case it should be unique job, not a list of them. Say, it’d get-VbrJob
2)Run this script once.
3)Enter name of your variable, put period “.” after it and wait a little bit.
4)PowerShell ISE should output list of all methods available for this variable.

Image

If you have any other questions, don't hesitate to let me know.

Hope this helps.
Thanks.
v.Eremin
Veeam Software
 
Posts: 13266
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Scheduled task to report on running backup jobs

Veeam Logoby GB@CFS » Sun Nov 25, 2012 11:11 pm

I wasn't using Powershell ISE, just the command line, so therefore did not see these additional options.

I completed my original goal, which was to set up the scheduled task to email nominated users if any SureBackup jobs are running and the names of these jobs.
I intend to build on this so that the format is in HTML with appropriate columns for the job name(s) and who initiated each job.

I'll post the full code once this has been completed.

Thanks again for your help.
GB@CFS
Novice
 
Posts: 8
Liked: never
Joined: Sun Oct 28, 2012 9:50 pm
Full Name: Gil Blumberg


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 3 guests