PowerShell script exchange
Post Reply
pizzim13
Enthusiast
Posts: 94
Liked: 6 times
Joined: Apr 21, 2011 7:37 pm
Contact:

Function for setting backup job options

Post by pizzim13 »

This function to quickly set a lot of the parameters needed for a new backup job. This function will pick a random time to run the job and a random day of the week for the synthetic full. Thanks to Andy and Seth for some of the examples

Code: Select all

function Set-VeeamBackupOptions
	{
	param	(
			[Parameter(
			Position=0, 
        		Mandatory=$true, 
        		ValueFromPipeline=$true,
        		ValueFromPipelineByPropertyName=$false)
			]
			[Veeam.Backup.Core.CBackupJob]$Job
			)
	#Set Job Options
	$JobOptions = $Job | Get-VBRJobOptions
		$JobOptions.BackupStorageOptions.RetainCycles = 99
		$JobOptions.JobOptions.SourceProxyAutoDetect = $true
		$JobOptions.JobOptions.RunManually = $false
		$JobOptions.BackupStorageOptions.RetainDays = 99
		$JobOptions.BackupStorageOptions.EnableDeduplication = $true
		$JobOptions.BackupStorageOptions.StgBlockSize = "KbBlockSize512"
		$JobOptions.BackupTargetOptions.Algorithm = "Increment"
		$JobOptions.BackupTargetOptions.TransformToSyntethicDays = ((Get-Date).adddays((Get-Random -Minimum 0 -Maximum 6))).dayofweek
		$JobOptions.BackupTargetOptions.TransformIncrementsToSyntethic = $true
	$Job | Set-VBRJobOptions -Options $JobOptions

	#Set schedule options
		#Create random backup time between 6PM and 4AM
		$Hours = (18,19,20,21,22,23,'00','01','02','03','04') | Get-Random | Out-String
		$Minutes = "{0:D2}" -f (Get-Random -Minimum 0 -Maximum 59) | Out-String
		$Time = ($Hours+':'+$Minutes+':00').replace("`n","")
		
	$JobScheduleOptions = $Job | Get-VBRJobScheduleOptions
		$JobScheduleOptions.OptionsDaily.Enabled = $true
		$JobScheduleOptions.OptionsDaily.Kind = "Everyday"
		$JobScheduleOptions.OptionsDaily.Time = $Time
		$JobScheduleOptions.NextRun = $Time
		$JobScheduleOptions.StartDateTime = $Time
	$Job | Set-VBRJobScheduleOptions -Options $JobScheduleOptions
	$Job.EnableScheduler()

	#Set VSS Options
	$JobVSSOptions = $Job | Get-VBRJobVSSOptions
		$VSSUSername = 'DOMAIN\USERNAME'
		$VSSPassword = 'PASSWORD'
		$VSSCredentials = New-Object -TypeName Veeam.Backup.Common.CCredentials -ArgumentList $VSSUSername,$VSSPassword,0,0
		$JobVSSOptions.Credentials = $VSSCredentials
		$JobVSSOptions.Enabled = $true
		#Change default behavior per job object
		foreach ($JobObject in ($Job | Get-VBRJobObject))
			{
			$ObjectVSSOptions = Get-VBRJobObjectVssOptions -ObjectInJob $JobObject
			$ObjectVSSOptions.IgnoreErrors = $true
			Set-VBRJobObjectVssOptions -Object $JobObject -Options $ObjectVSSOptions
			}
	$Job | Set-VBRJobVssOptions -Options $JobVSSOptions
	}
The following will create a job named Job1, pick a random repo, add vms from the $VMNames array, then set the options for that job.

Code: Select all

$VMNames = @("VM1","VM2","VM3","VM4","VM5")
Add-VBRViBackupJob -Name "Job1" -BackupRepository (Get-VBRBackupRepository | get-random) -Entity (Find-VBRViEntity -Name $VMNames) | Set-VeeamBackupOptions
pizzim13
Enthusiast
Posts: 94
Liked: 6 times
Joined: Apr 21, 2011 7:37 pm
Contact:

Re: Function for setting backup job options

Post by pizzim13 »

Be aware, with the last few patches setting the password has been problematic.
Phill Rogers
Novice
Posts: 3
Liked: never
Joined: Nov 01, 2012 12:07 pm
Full Name: Phill Rogers
Contact:

Re: Function for setting backup job options

Post by Phill Rogers »

Thanks for the code. It worked but I couldn't see where mine was different. So I gradually cut yours down to the absolute minimum to still work then compared line by line. I was missing this ..
$vbrjoboptions.JobOptions.RunManually = $false
.. but had it in an earlier version. Must have lost it somewhere in the editing and not noticed!

Phew!
Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests