Solved

Automatically remove machines from Job

  • 8 November 2021
  • 7 comments
  • 334 views

Userlevel 1

Hey All,

I’ve tried to get some automation for the backup of my Citrix base images.  I have a script that adds a machine to the backup job based on the name.  Of course, these machines get created and deleted like flies, and I was wanting to automatically remove them if they’ve been removed from vCenter.  I guess I could use tags, but then I’d have to rely on my Citrix admin to use the tags, and if it’s forgotten then there’s no backup, so I’ve decided a script is the way to go. 

So adding machines to the jobs works wonderfully, but I can’t seem to nail the way to remove the machines that aren’t in vCenter inventory. I also have the auto delete from backups turned on to prune the base images after x days.  

 

Here’s the snippet of powershell causing issues:

#Searches the backup job to find any VM that don't have a size associated with the VM and assigns it to the variable  ie. VM is still in the job but the VM is deleted.
$NullVM = Get-VBRJobObject -job $Citrixbackupjob | Where-Object -Property ApproxSizeString -eq -Value "0 B"


#Checks if the above variable is null.  If it isn't, it removes them from the job.
if ($NullVM){
Remove-VBRJobObject $NullVM
}

 

This part of the script used to work since the approxsizestring use to report the size in vCenter, and if the machine isn’t there, it would report a size of 0GB, now it’s reporting the VM size when it was added to the job.  

Anyone have a good way to automatically remove a VM from a job when the VM size is 0 (or any other programmatic method that can be nailed down in a script)?

 

Thanks,

Ryan

icon

Best answer by ddomask 24 November 2021, 13:56

View original

7 comments

Userlevel 5
Badge +3

Always welcome, and yep, small typo on my part. Fixed.

Userlevel 1

Thanks for the info...I’m thinking that fixed my issue.  Just a typo on get-vbrjobobjects...it should be singular.

Userlevel 5
Badge +3

This part of the script used to work since the approxsizestring use to report the size in vCenter, and if the machine isn’t there, it would report a size of 0GB, now it’s reporting the VM size when it was added to the job.  

Anyone have a good way to automatically remove a VM from a job when the VM size is 0 (or any other programmatic method that can be nailed down in a script)?

 

 

Old thread but in fact this is not quite accurate @7Ryan7 

You original code likely still works, it’s just that Veeam does a lot of caching of data from the Vmware hierarchy to avoid constantly spamming the vCenter; CObjectinJob objects aren’t updated as fast until it’s absolutely necessary to poll them, so my guess is your script does work, it just isn’t immediate.

 

Likely, if you just let the script run, it will “garbage collect” as expected once the job runs and updates the object after realizing it’s gone.

Is it essential to prune the jobs immediately for some reason?

If it is, then vNote’s solution is the right one. Fetch the data via Find-VBRViEntity into an array and then check your jobs against the array:

$AllVms = Find-VBRViEntity -Server "Relevant vCenter/Host" -VMsandTemplates
$JobObjects = Get-VBRJob -Name "Some job" | Get-VBRJobObject
$RemovedVMs= $JobObjects | Where-Object{$_.name -notin $AllVms.Name}

For big environments, Find-VBRViEntity might be a bit costly so you probably want to set this as a daily maintenance script (probably before your backups).

 

 

Userlevel 7
Badge +13

When I get this right, you can use PowerCLI Get-VM for this. If you you Get-VM returns nothing, VM does not exist in vSphere. If you go this way, you will see much better performance if you get all VMs upfront into an array and query this array.

Userlevel 6
Badge +3

Hi Ryan, quick thought: Won’t your base images always be created in a specific vSphere folder/resource group/etc.? So any kind of vSphere container which contains the base images and can be used for the backup job? You won’t need tags then and can just add this container.

In the backup job don’t forget the box to remove deleted items after some days so that you don’t keep old images for too long.

Userlevel 7
Badge +17

You can query the VMs which are present on the vCenter and compare these VMs against the VM list defined in your job.

Userlevel 7
Badge +13

Hi Ryan, I don’t think a powershell like this is possible.

Comment