The script should find primary 'backup' jobs run within 30 days. It should find the VM's that were backed up by those jobs. It should check SureBackup sessions to find the last time each VM was verified using SureBackup. It should pick the two VM's that have gone the longest without being run through SureBackup. It should then create the app group and the surebackup job, run the job, and clean up after itself. The next time the script runs (week 2), those two VM's verified in the first week will show a recent verification date, so other different VM's will be picked.
The problem I am having is with the "Add-VBRViApplicationGroup" cmdlet and the requirement to use New-VBRSureBackupVM to pass it the list of VM's.
Here is a cut-down example of the script for consideration...
Code: Select all
# Variables
$AppGroupName = "Dynamic App Group"
$SbJobName = "Dynamic Surebackup Job"
$SbJobDesc = "Dynamic App Testing"
$VirtualLab = "Virtual Lab 1"
[int]$NumberofVMs = 2
[int]$CheckBackupsNewerThanDays = 30
# Populate these arrays
$JobList = @()
$VmList = @()
# Populate $JobList
foreach ($vbrRecentJob in Get-VBRJob | ? { $_.IsBackup -and $_.LatestRunLocal -gt (get-date).AddDays(-$CheckBackupsNewerThanDays) }) {
$objJob = [pscustomobject]@{
Name = $vbrRecentJob.Name
LastRun = $vbrRecentJob.LatestRunLocal
}
$JobList += $objJob
}
# Populate $VmList
foreach ($job in $JobList | sort LastRun -Descending) {
$VbrJob = Get-VBRJob -Name $job.Name
foreach ($vm in Get-VBRTaskSession -Session $VbrJob.FindLastSession()) {
$objVM = [pscustomobject]@{
Name = $vm.Name
Job = $job.Name
LastVerify = ''
}
$VmList += $objVM
}
}
# Update the $VmList 'LastVerify' property of each VM
foreach ($VsbSession in get-vsbsession | sort CreationTime) {
foreach ($TaskSession in $VsbSession.GetTaskSessions()) {
if ($Match = $VmList | ? { $_.Name -eq $TaskSession.Name }) {
$Match.LastVerify = $VsbSession.CreationTime
}
}
}
# Create and run the SureBackup job (using existing lab name defined in $VirtualLab)
$TestVMs = $VmList | sort LastVerify | select -first $NumberofVMs
$VirtualLab = Get-VBRVirtualLab -Name $VirtualLab -Verbose
$backupobjects = @()
foreach ($testvm in $TestVMs) {
$backupobjects += New-VBRSureBackupVM -VM (Get-VBRJobObject -Job $testvm.Job -Name $testvm.Name)
}
$AppGroup = Add-VBRViApplicationGroup -VM (New-VBRSureBackupVM -VM $backupobjects) -Name $AppGroupName -Description $SbJobDesc -Verbose
$VsbJob = Add-VBRViSureBackupJob -VirtualLab $VirtualLab -ApplicationGroup $AppGroup -Description $SbJobDesc -Verbose
Start-VSBJob -Job $VsbJob -Verbose
Remove-VSBJob -Job $VsbJob -Confirm:$false -Verbose
Remove-VBRApplicationGroup -ApplicationGroup $AppGroup -Confirm:$false -Verbose
I have tried creating the app group using each individual VM from the 'New-VBRSureBackupVM' output and using Set-VBRViApplicationGroup to add the second/subsequent vm's.
I have tried creating an array of 'FindVBRViEntity' objects to pass to Add-VSBViApplicationGroup (deprecated cmdlet).
Another issue is that the combination of Get-VBRJobObject with New-VBRSureBackupVM doesn't seem to work as documented.
For example, consider this sample code
Code: Select all
$testvm = [pscustomobject]@{
Job = 'Job Name'
Name = 'VM Name'
}
Get-VBRJobObject -Job $testvm.Job -Name $testvm.Name
New-VBRSureBackupVM -VM (Get-VBRJobObject -Job $testvm.Job -Name $testvm.Name)
Code: Select all
New-VBRSureBackupVM : Cannot find the specified backup object 2c764a94-821f-49c8-b54c-3424345955ee in the selecte job cd34c8b2-866b-425a-9f88-7ed3f323098d.
At line:1 char:1
+ New-VBRSureBackupVM -VM (Get-VBRJobObject -Job $testvm.Job -Name $tes ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-VBRSureBackupVM], Exception
+ FullyQualifiedErrorId : ParameterSetId,Veeam.Backup.PowerShell.Cmdlets.NewVBRSureBackupVM