Veeam Powershell Automatically Add Backup Jobs
One of the great things with Veeam Backup & Replication is the integration with PowerShell and the ability to automate many of the job options using PowerShell. ย This is extremely handy when it comes to mass creating Veeam jobs in your environment by simply being able to query a list of VMs from vCenter, etc. ย Recently, I had to throw together a script to create Veeam jobs quickly in an environment using PowerShell. ย Let’s take a look at how to with Veeam PowerShell automatically add backup jobs.
Veeam Powershell Automatically Add Backup Jobs
There are several commandlets we can utilize to get the VMs from our VMware environment over to our Veeam backup environment. ย We can utilize the following Veeam commandlets to query and create the jobs:
- Get-VBRencryptionkey
- Find-VBRViEntity
- Add-VBRViBackupJob
- Set-VBRJobAdvancedStorageOptions
- Set-VBRJobSchedule
Let’s look at the PowerShell Code block:
$backupjobs = get-content c:vms.txt $Key = Get-VBREncryptionKey $results = foreach ($backupjob in $backupjobs){ Write-Host "Adding Backup job for $backupjob" find-vbrvientity -name $backupjob* | where-object {$_.VMFolderName -ne "SomeFolder"} | Add-VBRViBackupJob -Name $backupjob"_new" -BackupRepository 'MyRepo' Set-VBRJobAdvancedStorageOptions -Job $backupjob"_new" -EnableEncryption $true -EncryptionKey $Key Set-VBRJobSchedule -Job $backupjob"_new" -Periodicaly -FullPeriod 1 -PeriodicallyKind Hours $jobname=get-VBRJob -Name $backupjob"_new" $Options = $jobname.GetOptions() $Options.BackupStorageOptions.RetainCycles='21' $jobname.SetOptions($Options) } $results | out-file c:copyjob.log -Append
In the above, we get the list of VMs from aย get-content read of a text file. ย This provides the list of VMs that we want to actually create the jobs for. ย We set a variable to get the encryption key for easy usage.
Now we actually start looping through the VMs. ย We use the Find-VBRViEntity to find the name of the VM in our vSphere environment. ย Then for this VM, we use theย Add-VBRViBackupJob commandlet to create the job based on the name of the VM. ย Here I am appending the “_new” to the name for a special use case. ย For most you may not want to append anything. ย With theย Set-VBRJobSchedule we set the job schedule parameters. ย Here I am setting the schedule to backup the VM every hour.
In the final section of the block, since there isn’t a Veeam PowerShell commandlet for this purpose (that I could find), I am using this chunk of code to set the number of Retension points that we want to keep for each job.
Finally, we log the results to a variable if we want to use this or simply create the log file.
Thoughts
The above is certainly a gorilla script without much time for commenting or any other creative work, however, we can see the power of usingย Veeam Powershell Automatically Add Backup Jobs to our environment and change settings of these on the fly as they are created. ย This is a much easier way of creating multiple jobs than using the wizard for each individual one.ย [jetpack-related-posts]