PowerShell script exchange
Post Reply
ekisner
Expert
Posts: 203
Liked: 34 times
Joined: Jul 26, 2012 8:04 pm
Full Name: Erik Kisner
Contact:

Mass Job Duplication

Post by ekisner »

I set up a quick powershell script to use a job as a template to create many jobs.

Was hoping someone could look it over and make sure it works as intended... one thing that is not working as intended is the secondary backup (to tape) jobs being copied across. Doing some research on that, and will implement it at some point. I have run it... once... it appeared to do what was intended. Since it appears to have worked on its first run, it is not debugged at all. Review and run with discretion.
Other than that, assuming someone points out a glaring omission, hopefully this will save others some time.

To use it, simply create a backup job, exactly as you want the rest to be. Yes, I could put these as variables at the start, but that's no fun.
You will need to edit:
1) The $vms variable near the beginning
2) The name of the template job to find and use as a template job, 3 lines down
3) Within the foreach loop, edit the naming scheme at the start (mine is simply B2D_<vm name>_PWS).

Code: Select all

#Edit this criteria to select the VMs you want.
$vms = Find-VBRViEntity | ?{$_.criteria -eq "Whatyouwantedtobackup"}

#Select a template job, set up manually via the wizard.  It should be ready to rock and roll.  You will need to edit the name of this job to be the same as what you've defined.
$templatejob = Get-VBRJob -name "B2D_Server01_PWS"
#Get the template job's job options.
$templateoptions = Get-VBRJobOptions $templatejob
#Get the template schedule
$templatesched = Get-VBRJobScheduleOptions $templatejob

#Iterate through each vm in $vms, as gathered by Find-VBRViEntity
foreach ($vm in $vms){
	#Define the job name
	$jobname = "B2D_$($vm.Name)_PWS"

	#Write it to the shell for peace of mind
	write-host $jobname

	#Generate a backup job
	Add-VBRViBackupJob -Name $jobname -BackupRepository (Get-VBRBackupRepository -name PWS*)-entity $vm

	#Find our newly created job
	$job = Get-VBRJob | ?{$_.name -eq $jobname}
	#Add the job options to it
	$job | set-VBRJobOptions -Options $templateoptions
	#Add the job schedule to it
	$job | Set-VBRJobScheduleOptions -Options $templatesched
	#Enable the schedule
	$job.EnableScheduler()
}
veremin
Product Manager
Posts: 20406
Liked: 2298 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Mass Job Duplication

Post by veremin »

Just a small tip - there is no need to use a template job. Instead, you create a template of job general, as well as schedule settings, and assign these setting to existing jobs. It can be done via New-VBRJobOptions and New-VBRJobScheduleOptions cmdlets.

Anyway, thank you for sharing the script; much appreciated.
ekisner
Expert
Posts: 203
Liked: 34 times
Joined: Jul 26, 2012 8:04 pm
Full Name: Erik Kisner
Contact:

Re: Mass Job Duplication

Post by ekisner »

Yea, the goal here was to be able to use the wizard to create the settings via GUI, more as a measure of peace of mind over anything else.
veremin
Product Manager
Posts: 20406
Liked: 2298 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Mass Job Duplication

Post by veremin »

Yes, there is absolutely no harm to create the "template" job via GUI. You can use whatever approach you like to. Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests