Solved

Problems with Creating Jobs in PowerShell - Enabling Schedule, Setting Retention Policy, and Encryption Key

  • 17 August 2023
  • 5 comments
  • 140 views

Userlevel 3

I’m having challenges getting a job creation script to work for creating VM backup jobs. The code has a switch to create a VMWare/Hyper-V job based on provided input. The job is created without needed customizations in three areas (1) setting the schedule, (2) setting the retention policy, and (3) setting an encryption key on the backups. I haven’t gotten #3 in the code yet, but open to thoughts. After I run the script, it succeeds but the settings for #1 and #2 aren’t fully applied.

In the case of 1, the schedule is added but the job schedule isn’t enabled.

In the case of 2, nothing is enabled. I couldn’t find any other approach to do #2 except with a JobOptions object. Any ideas why not?

In the case of 3, I would use JobOptions if I could figure out for both but am contemplating just using Set-VBRAdvancedStorageOptions as that is straightforward.

    #After Job Creation, the $Job variable is applied with the job it just created.
#$JobStartTime is created with Get-Date starting at 20:00
#Apply Settings
#Set schedule
$JobStartTime = $JobStartTime.AddHours(1)
Write-Verbose "Updating Schedule for job $Group with Start Time $($JobStartTime.Hour):00."
$Job = Set-VBRJobSchedule -Job $Job -Daily -At $JobStartTime -DailyKind Ever | Enable-VBRJobSchedule
#Add retention settings
$JobSettings = New-VBRJobOptions -ForBackupJob
$JobSettings.BackupStorageOptions.RetainDays = 3
$JobSettings.BackupStorageOptions.RetentionType = "Days"
#Add Encryption Settings

#Apply Settings
$Job = Set-VBRJobOptions -Job $Job -Options $JobSettings

The outcome is such as seen below: Note the retention policy is unchnaged, and the schedule is applied but not enabled.

Schedule applied but not enabled.


 

Retention policy failed

 

icon

Best answer by NZ_BenThomas 18 August 2023, 02:26

View original

5 comments

Userlevel 7
Badge +20

Here is a script I have for creating backup jobs with multiple options.  I think some of this will help with the options you are trying to set as an example only.  Not meant to replace what you have done.

{{
#Specify the name you want to assign to the backup job

$JobName = "backup job name"



#Specify the array of VMware VMs you want to add to the job.

#If you want to search by part of VM name use *

$VMsNames = Find-VBRViEntity -Name "VM1", "VM2", "VM3"



#Specify the backup repository where the created backup will be stored. Default: default backup repository

#If you want to search by part of repo name use *

$BackupRepo = Get-VBRBackupRepository -Name "name of repo"



#specify the VMware backup proxy for this job

$ProxyName = "name of the proxy"



# Which day you want to run the synthetic full? Options are Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday. type the day between ' '

$syntheticday = 'Day'



# specify the retention for daily jobs and weekly

$DailyRetention = "number of days"

$WeeklyRetention = "number of weeks"



#specify the time of the day you want to run the job? Format is hh:mm. type the time between ' '

$StartTime = 'hh:mm'



#Enter the number of attempts to run the job after failing and define time intervals between them in minutes. type them between ' '

$RetriesNumber = 'number'

$WaitingTime = 'minutes'



Add-VBRViBackupJob -Name $JobName -Entity $VMsNames -BackupRepository $BackupRepo

# Job is created with default parameters



$BackupJob = Get-VBRJob -Name $JobName



# assign VMware proxy to the Backup Job

$proxy = Get-VBRViProxy -Name $ProxyName

$BackupJob | Set-VBRJobProxy -Proxy $proxy



# Set retention for backup

$retention = Get-VBRJobOptions -Job $BackupJob

$retention.BackupStorageOptions.RetentionType = 'Days'

$retention.BackupStorageOptions.RetainDaysToKeep = $DailyRetention

Set-VBRJobOptions -Job $BackupJob -Options $retention



$retention.GfsPolicy.IsEnabled = $true

$retention.GfsPolicy.Weekly.IsEnabled = $true

$retention.GfsPolicy.Weekly.KeepBackupsForNumberOfWeeks = $WeeklyRetention

$retention.GfsPolicy.Weekly.DesiredTime = $syntheticday

Set-VBRJobOptions -Job $BackupJob -Options $retention



# set full synthetic backup

$BackupJob | Set-VBRJobAdvancedBackupOptions -Algorithm Incremental -TransformFullToSyntethic $True -EnableFullBackup $False -FullBackupDays $syntheticday



# Advanced options (for Dell Data Domain)

$BackupJob | Set-VBRJobAdvancedStorageOptions -CompressionLevel 0 -EnableDeduplication $False -StorageBlockSize 5



#enable daily schedule and set the start time

$BackupJob | Enable-VBRJobSchedule

$BackupJob | Set-VBRJobSchedule -Daily -At $StartTime



# set number of reties and waiting time

$ScheduleOptions = $BackupJob | Get-VBRJobScheduleOptions

$ScheduleOptions.Retrytimes = $RetriesNumber

$ScheduleOptions.Retrytimeout = $WaitingTime

Set-VBRJobScheduleOptions -Job $BackupJob -Options $ScheduleOptions



}}

 

Userlevel 7
Badge +20

Mainly for the scheduling and retention period - my examples above. ☝🏻

Userlevel 4
Badge +2

Similar code for a primary backup that might have some extra useful properties. Some of the settings in B&R can only be done by dropping into the underlying .Net methods.

 


#region Setup Repository and Backup Names

if ($paramBackupJobType -eq "COPY") {
$DataDomainRepositorySuffix = "_COPY_REPOSITORY"
$DataDomainTarget = $CopyBackupDataDomain.substring(2)
} else {
$DataDomainRepositorySuffix = "_REPOSITORY"
$DataDomainTarget = $PrimaryBackupDataDomain.substring(2)
}

if ($paramUniqueIdentifier -eq "") {
$BackupJobName = $paramBackupEnvironment + "_" + $paramBackupFrequency + "_" + $paramBackupName + "_BACKUP"
$CopyJobName = $paramBackupEnvironment + "_" + $paramBackupFrequency + "_" + $paramBackupName + "_COPY"
} else {
$BackupJobName = $paramBackupEnvironment + "_" + $paramBackupFrequency + "_" + $paramBackupName + "-" + $paramUniqueIdentifier + "_BACKUP"
$CopyJobName = $paramBackupEnvironment + "_" + $paramBackupFrequency + "_" + $paramBackupName + "-" + $paramUniqueIdentifier + "_COPY"
}


$TargetRepository = $DataDomainTarget + "_" + $paramBackupName + $DataDomainRepositorySuffix

#endregion



#region Setup GFS and retentions

if ($paramBackupFrequency -eq "WEEKLY") {
$BackupRetentionPolicy = "DAYS"
$BackupRetention = 13
$BackupGFSWeekly = 2
$BackupGFSMonthly = 0
$BackupGFSYearly = 0
} else {
$BackupRetentionPolicy = "POINTS"
$BackupRetention = 10
$BackupGFSWeekly = 0
$BackupGFSMonthly = 0
$BackupGFSYearly = 0

}

#endregion



#region Create BACKUP Job

if ($paramBackupJobType -eq "BACKUP") {

foreach ($Server in $aryServerList.Name) {

Write-Host "Checking server $Server exists..."
$objServer = Find-VBRViEntity -VMsAndTemplates -Name $Server

if ($null -eq $objServer) {
Write-Host "Invalid server name: $Server"
exit
}
}

Write-Host "Creating Veeam BACKUP job '$BackupJobName' using repository $TargetRepository..."

Try {
Add-VBRViBackupJob -Name $BackupJobName -BackupRepository $TargetRepository -Description $paramBackupDescription `
-Entity (Find-VBRViEntity -VMsAndTemplates -Name $aryServerList.Name) | Out-Null
}
Catch
{
Write-Host "Failed to create the backup job:" $Error[0].Exception.Message -ForegroundColor Red
Exit
}


Write-Host "Updating Storage options..."
Try {
Get-VBRJob -name $BackupJobName | Set-VBRJobAdvancedStorageOptions -CompressionLevel 4 -StorageBlockSize 5 -EnableDeduplication $false | Out-Null
}
Catch {
Write-Host "Unable to update the Storage options:" $Error[0].Exception.Message -ForegroundColor Red
exit
}


Write-Host "Updating Full Backup options..."
Try {
Get-VBRJob -name $BackupJobName | Set-VBRJobAdvancedBackupOptions -EnableFullBackup $true -TransformFullToSyntethic $false | Out-Null
}
Catch {
Write-Host "Unable to configure Full Backup:" $Error[0].Exception.Message -ForegroundColor Red
exit
}


Write-Host "Updating settings for deleted VM's..."
Try {
Get-VBRJob -name $BackupJobName | Set-VBRJobAdvancedOptions -RetainDays 90 | Out-Null
}
Catch {
Write-Host "Unable to configure settings for deleted VM's:" $Error[0].Exception.Message -ForegroundColor Red
exit
}


Write-Host "Updating retention settings..."
Try {
$BackupJobOptions = Get-VBRJobOptions -Job $BackupJobName
$BackupJobOptions.BackupStorageOptions.EnableDeletedVmDataRetention = $true

if ($BackupRetentionPolicy -eq "DAYS") {
$BackupJobOptions.BackupStorageOptions.RetentionType = "Days"
$BackupJobOptions.BackupStorageOptions.RetainDaysToKeep = $BackupRetention
$BackupJobOptions.GfsPolicy.IsEnabled = $True

if ($BackupGFSWeekly -gt 0) {
$BackupJobOptions.GfsPolicy.Weekly.IsEnabled = $True
$BackupJobOptions.GfsPolicy.Weekly.KeepBackupsForNumberOfWeeks = $BackupGFSWeekly
}

if ($BackupGFSMonthly -gt 0) {
$BackupJobOptions.GfsPolicy.Monthly.IsEnabled = $True
$BackupJobOptions.GfsPolicy.Monthly.KeepBackupsForNumberOfMonths = $BackupGFSMonthly
}

if ($BackupGFSYearly -gt 0) {
$BackupJobOptions.GfsPolicy.Monthly.IsEnabled = $True
$BackupJobOptions.GfsPolicy.Monthly.KeepBackupsForNumberOfMonths = $BackupGFSYearly
}
}

if ($BackupRetentionPolicy -eq "POINTS") {
$BackupJobOptions.BackupStorageOptions.RetentionType = "Cycles"
$BackupJobOptions.BackupStorageOptions.RetainCycles = $BackupRetention
}

Set-VBRJobOptions -Job $BackupJobName -Options $BackupJobOptions | Out-Null

$CopyJobOptions = New-VBRJobOptions -ForBackupJob
$CopyJobOptions.BackupStorageOptions.RetainCycles = 8
$CopyJobOptions.GenerationPolicy.SyncIntervalStartTime = (Get-Date($BackupTime)).AddHours(-2).TimeOfDay # Should be primary backup time +2 hours
$CopyJobOptions.GfsPolicy.IsEnabled = $True
$CopyJobOptions.GfsPolicy.Weekly.IsEnabled = $True
$CopyJobOptions.GfsPolicy.Weekly.KeepBackupsForNumberOfWeeks = 5
$CopyJobOptions.GfsPolicy.Monthly.IsEnabled = $True
$CopyJobOptions.GfsPolicy.Monthly.KeepBackupsForNumberOfMonths = 3

}
Catch {
Write-Host "Unable to update the Storage options:" $Error[0].Exception.Message -ForegroundColor Red
exit
}


Write-Host "Updating Guest Interaction options..."

Try {
Enable-VBRJobVSSIntegration -Job $BackupJobName | Out-Null

$VBRGuestCredentials = Get-VBRCredentials -Name $GuestProcessingAccount
Set-VBRJobVssOptions -Job $BackupJobName -Credentials $VBRGuestCredentials | Out-Null

# Read in the backup as an object so we can access the individual VM settings
$VBRJobObject = Get-VBRJobObject -Job $BackupJobName

foreach ($VMObject in $VBRJobObject) {
# Read in each of the VM's and set their VSS options
Write-Host "Configuring VSS settings on $($VMObject.Name)..."
$JobVSSOptions = $VMObject | Get-VBRJobObjectVssOptions
$JobVSSOptions.VssSnapshotOptions.IgnoreErrors = $true
$JobVSSOptions.VssSnapshotOptions.IsCopyOnly = $true
Set-VBRJobObjectVssOptions -Object $VMObject -Options $JobVSSOptions | Out-Null
}
}
Catch {
Write-Host "Unable to update the Guest Processing options:" $Error[0].Exception.Message -ForegroundColor Red
exit
}


Try {
Write-Host "Configuring backup scheduling options..."
Set-VBRJobSchedule -Job $BackupJobName -DailyKind SelectedDays -Days $VBRBackupDays -At $BackupTime | Out-Null
Enable-VBRJobSchedule -Job $BackupJobName | Out-Null
}
Catch {
Write-Host "Unable to update the backup scheduling options:" $Error[0].Exception.Message -ForegroundColor Red
exit
}


Write-Host "Setting disk exclusions for SQL servers..."

Try {
# Read in the backup as an object so we can access the individual VM settings
$VBRJobObjects = Get-VBRJob -Name $BackupJobName | Get-VBRJobObject

$SQLServerDefaultIncludeDisks = $SQLServerDefaultIncludeDisks -Split ","

Foreach ($VBRJobObject in $VBRJobObjects) {

$ServerType = ($aryServerList | Where-Object -Property Name -eq $VBRJobObject.Name).Type

If ($ServerType -eq "SQL") {

Write-Host "Excluding disks on $($VBRJobObject.Name) - SQL server specified"

$DiskArray = @()

foreach($DiskKey in $SQLServerDefaultIncludeDisks) {
$VDiskKey = [Veeam.Backup.Model.VDiskKey]::New($DiskKey)
$DiskArray += [Veeam.Backup.Model.CDiskKeyInfo]::CreateDiskInfo($VDiskKey)
}

$CDiskFilterInfo = [Veeam.Backup.Model.CDiskFilterInfo]::CreateSelectedDisks($DiskArray)

$VSSOptions = $VBRJobObject.VSSOptions
$JobInfoType = $VBRJobObject.Info.Type
$VBRJobObject.Update($VSSOptions, $CDiskFilterInfo, $True, $JobInfoType)

Write-Host "Removing Application Processing from the server"
$JobVSSOptions = $VBRJobObject | Get-VBRJobObjectVssOptions
$JobVSSOptions.VssSnapshotOptions.ApplicationProcessingEnabled = $False
$JobVSSOptions.VssSnapshotOptions.Enabled = $False
Set-VBRJobObjectVssOptions -Object $VBRJobObject -Options $JobVSSOptions | Out-Null
$VBRJobObject

}

}
}
Catch {
Write-Host "Unable to update the disk exclusion options:" $Error[0].Exception.Message -ForegroundColor Red
exit
}

Write-Host "Creation of $BackupJobName complete"

}

#endregion

 

Userlevel 6
Badge +3

@jreinhardtpa for the schedule not enabling, I think it’s possible just a result of trying to chain too many things together in a single line and I’m not sure `Set-VBRJobSchedule` can be piped through to `Enable-VBRJobSchedule`

Try splitting them out into their own lines like below, using the $job as the source pipe for each

#Set schedule start time
$JobStartTime = $JobStartTime.AddHours(1)
Write-Verbose "Updating Schedule for job $Group with Start Time $($JobStartTime.Hour):00."
# Enable first
$Job | Enable-VBRJobSchedule
# Apply Schedule Settings
$Job | Set-VBRJobSchedule -Daily -At $JobStartTime -DailyKind Everyday

 

Userlevel 3

Thank you all - Yes, @NZ_BenThomas was on the right track that the order was out of whach. Using the $Job.GetOptions() method instead of New-VBRStorageOptions made sure that when I modified the retetnion settings I wouldn’t wipe out anything I had already set. In the end, my sequence was (1) apply advanced storage options, (2) update retention, and (3) set the schedule. 

Also, the retention problem was specifically that I was modifying the wrong setting to do what I wanted - to change the retention setting available in the GUI, I used “RetainDaysToKeep” to 3. So, I am now in good shape on this. Thanks so much for the community’s support.

Comment