PowerShell script exchange
Post Reply
nzcamkay
Lurker
Posts: 1
Liked: never
Joined: Nov 08, 2012 10:35 pm
Full Name: Campbell Kay
Contact:

Veeam Script to Check Job Status, then execute another scrip

Post by nzcamkay »

Hi Guys,

i am trying to write a script to check my Backup copy Job Status, if none of them are in a 'working' status, execute another script, which would essentially shutdown my Azure VM

i have this so far, it seems to work when i run it from ISE

Code: Select all

Add-PSSnapin Veeampssnapin
$jobStatus = Get-VBRJob| Where {$_.GetLastState() -ne "Working"}
$JobStatus = ($jobs.GetLastState())
If ($JobStatus -like "Working"){$JobActive ++}
if ($JobActive -eq 0) {
write-host "Shutdown Azure Backup Server"
}
else {
write-host "Backup is running - cycle in 1 hour." }
i have just put write-host in the place of where we could run a script for testing.

when i run it from powershell i get this error

Code: Select all

PS E:\Cloud Backups> .\AzureCheck2.ps1
You cannot call a method on a null-valued expression.
At E:\Cloud Backups\AzureCheck2.ps1:5 char:1
+ $JobStatus = ($jobs.GetLastState())
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

any ideas?
veremin
Product Manager
Posts: 20283
Liked: 2258 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Veeam Script to Check Job Status, then execute another s

Post by veremin »

The error suggest that $Jobs contains nothing and actually I cannot see this variable assigned in your script. Thanks.
npandav
Novice
Posts: 5
Liked: never
Joined: Jan 09, 2018 4:16 pm
Full Name: Naresh Pandav
Contact:

Re: Veeam Script to Check Job Status, then execute another s

Post by npandav »

Did anyone get this working?

I am getting this error:

You cannot call a method on a null-valued expression.
+ $JobStatus1 = ($jobstatus.GetLastState())
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
veremin
Product Manager
Posts: 20283
Liked: 2258 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Veeam Script to Check Job Status, then execute another s

Post by veremin »

Which means $Jobstatus contains nothing. I'd recommend checking how variables are assigned within your script. Thanks.
tdewin
Veeam Software
Posts: 1775
Liked: 646 times
Joined: Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin
Contact:

Re: Veeam Script to Check Job Status, then execute another s

Post by tdewin »

Well, you didn't define $jobs at all, yet you are using it

Code: Select all

$jobStatus = Get-VBRJob| Where {$_.GetLastState() -ne "Working"}
$JobStatus = ($jobs.GetLastState())
So I guess you want to something like the following. Using the @() constructs forces powershell to take the output it and make it an array. This is good cause if your filter returns $null, you can still use .Count
$jobsworking = @(Get-VBRJob| Where {$_.GetLastState() -eq "Working"} )
if ($jobsworking.Count -gt 0) {
write-host "Jobs are still working"
} else {
write-host "Nothing is working, let's shutdown machines"
}
(Didn't test it, just writing the code directly in the forum)
npandav
Novice
Posts: 5
Liked: never
Joined: Jan 09, 2018 4:16 pm
Full Name: Naresh Pandav
Contact:

Re: Veeam Script to Check Job Status, then execute another s

Post by npandav »

Perfect. Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests