PowerShell script exchange
Post Reply
Bazillus
Novice
Posts: 5
Liked: never
Joined: Sep 06, 2013 5:55 am
Full Name: René Schärer
Contact:

automatically disable/enable Jobs

Post by Bazillus » Sep 24, 2013 7:43 am

Hi

We use the logical folder in vCenter to Backup the VM. Each Folder has a backup job (with different Options).
Our environment is really dynamic.
Now sometimes there are no VM in folders but the backup job is enabled.

Is it possible to create a Powershell script which match following points:
- go through all Job's and scan the number of VM's or amount of space to backup.
- if the number or amount zero the job should be automatically disabled if it enabled.
- if something in the backup job and is disabled it should be enabled automatically.

regards
René

veremin
Product Manager
Posts: 16901
Liked: 1438 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: automatically disable/enable Jobs

Post by veremin » Sep 24, 2013 10:23 am 1 person likes this post

Hi, René. From my perspective, the following script should meet your expectations:

Code: Select all

asnp VeeamPSSnapin 
foreach ($Job in (Get-VBRJob | ?{$_.jobtype -eq "Backup"}))
{
  if ($Job.info.IncludedSize -eq 0) {$Job.DisableScheduler()}
} 
Thanks.

ldv
Novice
Posts: 9
Liked: 1 time
Joined: Sep 25, 2013 6:43 am
Full Name: Laura
Contact:

Re: automatically disable/enable Jobs

Post by ldv » Sep 27, 2013 7:49 am 1 person likes this post

Hi!

The script works fine. Thanks!
I updated the script with an additional function to enable all other jobs:

Code: Select all

asnp VeeamPSSnapin 
foreach ($Job in (Get-VBRJob | ?{$_.jobtype -eq "Backup"}))
{
    IF ($Job.info.IncludedSize -eq 0) {$Job.DisableScheduler()}

    ELSE
 {
 $Job.EnableScheduler()
 }
}
But there's one little problem... The recalculation of the size (into a job) doesn't update automatically.
So, empty jobs on which you add new VMs will stay disabled because the size is still 0.0kb. If you remove all VMs on a enabled job it should be 0.0kb but it doesn't update it.
We think the reason for this problem is that we work with logical folders from the vCenter in the job.
Has anybody an idea for a workaround?

Thanks!

Regards
Laura

veremin
Product Manager
Posts: 16901
Liked: 1438 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: automatically disable/enable Jobs

Post by veremin » Sep 27, 2013 9:17 am

Hi, Laura. I’m wondering what the version you’re using. Is it still the case with version 7? Thanks.

ldv
Novice
Posts: 9
Liked: 1 time
Joined: Sep 25, 2013 6:43 am
Full Name: Laura
Contact:

Re: automatically disable/enable Jobs

Post by ldv » Sep 27, 2013 9:30 am

Hi Vladimir. We're using version 6.5 (6.5.0.144). We plan the update to v7 next week. Do you think the problem will be solved with v7?

veremin
Product Manager
Posts: 16901
Liked: 1438 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: automatically disable/enable Jobs

Post by veremin » Sep 27, 2013 10:17 am

Yep, I believe so. The approximate size showing mechanism should have been fixed there. Thanks.

ldv
Novice
Posts: 9
Liked: 1 time
Joined: Sep 25, 2013 6:43 am
Full Name: Laura
Contact:

Re: automatically disable/enable Jobs

Post by ldv » Sep 27, 2013 10:19 am

Nice! So, we will see next week. Thanks!

ldv
Novice
Posts: 9
Liked: 1 time
Joined: Sep 25, 2013 6:43 am
Full Name: Laura
Contact:

Re: automatically disable/enable Jobs

Post by ldv » Oct 07, 2013 5:48 pm

We updated last Wednesday - Unfortunately the approximate size showing problem is still the same. It will be the easiest way to create a powershell script which will readout from vCenter directly. I think the problem is because we work with logical folders from the vCenter and not with directly added VMs...

veremin
Product Manager
Posts: 16901
Liked: 1438 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: automatically disable/enable Jobs

Post by veremin » Oct 08, 2013 9:33 am

Then, you might want to use PowerCLI, as well. The algorithm should be as the following:

1) Get the name of the folder from the backup job. (VeeamPSSnapin)
2) Connect to vCenter and check whether there are any VMs located under the said folder (PowerCLI)
3) If the answer is yes, enable the backup job, otherwise, disable it.

So, if every backup job uses folder as a source, you should utilize something like this:

Code: Select all

add-pssnapin VMware.VimAutomation.Core
Add-PSSnapin VeeamPSSnapin
Connect-VIServer -Server "Name of vCenter" -User "user" -Password "password"
foreach ($Job in (Get-VBRJob | ?{$_.jobtype -eq "Backup"}))
{
$Foldername = $Job | Get-VBRJobObject | select name
$Folder = Get-Folder -name $Foldername
If ((Get-VM -Location $Folder) -eq $null) {$Job.DisableScheduler()}
Else {$Job.EnableScheduler()}
} 
Thanks.

ldv
Novice
Posts: 9
Liked: 1 time
Joined: Sep 25, 2013 6:43 am
Full Name: Laura
Contact:

Re: automatically disable/enable Jobs

Post by ldv » Oct 21, 2013 12:41 pm

Hi Vladimir. Thank you very much for your help and the script! Now I am happy! :)

I updated the script a little bit and now it works fine for me:

1) Save creds into a file:

Code: Select all

Add-PSSnapIn VMware.VimAutomation.Core
New-VICredentialStoreItem -host "vCenter Server" -user "user" -password "password" -file C:\Scripts\credfile.xml
2) Updated script (updated line: $Foldername = $Job | Get-VBRJobObject | ForEach-Object {$_.name}):

Code: Select all

Add-PSSnapin VMware.VimAutomation.Core
Add-PSSnapin VeeamPSSnapin
    
$creds = Get-VICredentialStoreItem -file “C:\Scripts\credfile.xml”
Connect-viserver -Server $creds.Host -User $creds.User -Password $creds.Password

foreach ($Job in (Get-VBRJob | ?{$_.jobtype -eq "Backup"}))
{
$Foldername = $Job | Get-VBRJobObject | ForEach-Object {$_.name}
$Folder = Get-Folder -name $Foldername
If ((Get-VM -Location $Folder) -eq $null) {$Job.DisableScheduler()}
Else {$Job.EnableScheduler()}
}

veremin
Product Manager
Posts: 16901
Liked: 1438 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: automatically disable/enable Jobs

Post by veremin » Oct 21, 2013 12:46 pm

Glad to hear that my input was helpful. Should any other questions arise, don’t hesitate to let us know. Thanks.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests