PowerShell script exchange
Post Reply
GB@CFS
Novice
Posts: 8
Liked: never
Joined: Oct 28, 2012 9:50 pm
Full Name: Gil Blumberg
Contact:

Scheduled task to report on running backup jobs

Post by GB@CFS »

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
Vitaliy S.
VP, Product Management
Posts: 27055
Liked: 2710 times
Joined: Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov
Contact:

Re: Scheduled task to report on running backup jobs

Post by Vitaliy S. »

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!
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Scheduled task to report on running backup jobs

Post by veremin » 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.
GB@CFS
Novice
Posts: 8
Liked: never
Joined: Oct 28, 2012 9:50 pm
Full Name: Gil Blumberg
Contact:

Re: Scheduled task to report on running backup jobs

Post by GB@CFS »

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: Oct 28, 2012 9:50 pm
Full Name: Gil Blumberg
Contact:

Re: Scheduled task to report on running backup jobs

Post by GB@CFS »

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?
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Scheduled task to report on running backup jobs

Post by veremin » 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.
GB@CFS
Novice
Posts: 8
Liked: never
Joined: Oct 28, 2012 9:50 pm
Full Name: Gil Blumberg
Contact:

Re: Scheduled task to report on running backup jobs

Post by GB@CFS »

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: Oct 28, 2012 9:50 pm
Full Name: Gil Blumberg
Contact:

Re: Scheduled task to report on running backup jobs

Post by GB@CFS »

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
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Scheduled task to report on running backup jobs

Post by veremin »

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.
GB@CFS
Novice
Posts: 8
Liked: never
Joined: Oct 28, 2012 9:50 pm
Full Name: Gil Blumberg
Contact:

Re: Scheduled task to report on running backup jobs

Post by GB@CFS »

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.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 16 guests