PowerShell script exchange
Post Reply
pirx
Veteran
Posts: 660
Liked: 98 times
Joined: Dec 20, 2015 6:24 pm
Contact:

find non existing VMs in excludes and collect VM and Disk excludes in a useful manner

Post by pirx »

We were great in adding excluded in the past 10 years. Problem is that an excluded keeps existing even if the source VM was removed long ago. The VM or it's excluded disks are still shown and returned in powershell queries. I've 2 scripts that are based on...
Get-VBRJobObject -Job $Job | Where Type -eq "ExcludeDisksChild")
Get-VBRJobObject -Job $job | Where Role -eq "EExclude"))
...is there an easy way to also find VMs in excludes that do not exist anymore in vSphere? That would be #1 excluded that I would remove, ideally automatically in the same script.

If someone has a nice script that already helps to find and cleanup excludes, I'd be very much interested.
david.domask
Veeam Software
Posts: 3072
Liked: 710 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: find non existing VMs in excludes and collect VM and Disk excludes in a useful manner

Post by david.domask »

Hi pirx,

I don't have a ready-made script handy, but I suppose the easiest way is a combination of Get-VBRJobObject and Find-VBRViEntity (or Find-VBRHvEntity for HyperV)

Objects returned by Get-VBRJobObject have an Object property, and the Object property has a Reference property that will align with the actual MoRef ID / VM GUID from production.

I think easiest way is just make an array of all the VM references at the start of the clean-up script, then compare with -notin to find jobs with objects that no longer exist in production and pass those to Remove-VBRJobObject with the -Completely parameter. Very simple example:

Code: Select all

$allVmIds = Find-VBRViEntity | Where-Object{$_.Type -eq "Vm"} | Select -Property Reference
$backupJob = Get-VBRJob -Name "dd-ps-exclude-test"
$jObjects = Get-VBRJobObject -Job $backupJob | Where-Object {$_.Type -eq "Exclude"}
$jObjects | Where-Object {$_.Object.Reference -notin $allVmIds}

Name                      Type       ApproxSize       Location
----                      ----       ----------       --------
dd-fake-exa-deb           Exclude    0 B             some-vcenter.justadomain.win

Remove-VBRJobObject -Objects $jObjects -Completely
David Domask | Product Management: Principal Analyst
pirx
Veteran
Posts: 660
Liked: 98 times
Joined: Dec 20, 2015 6:24 pm
Contact:

Re: find non existing VMs in excludes and collect VM and Disk excludes in a useful manner

Post by pirx »

hm, your script returns the excludes - but the ones where a VM still exist. And the exclude works as those VMs do not have any backup. I think it has to be $jObjects.Object.ObjectRef. But it still returns just all VMs, event the ones that are in $allVmIds. I see that it should work but it doesn't :)

Name Type ApproxSize Location
---- ---- ---------- --------
NDEISE030 Exclude 0 B s...
SDES0120 Exclude 0 B s...
SDES0160 Exclude 0 B s...
SDES0130 Exclude 0 B s...
SDES0170 Exclude 0 B s...

#> $allVmIds

Reference
---------
vm-1378051
vm-3910277
vm-3922146
vm-3910276
...

#> jObjects

Name Type ApproxSize Location
---- ---- ---------- --------
NDEISE030 Exclude 0 B s...
SDES0120 Exclude 0 B s...
SDES0160 Exclude 0 B s...
SDES0130 Exclude 0 B s...
SDES0170 Exclude 0 B s...

#> $jObjects.Object.Reference
- empty -

> $jObjects.Object.ObjectRef
vm-1378051
pirx
Veteran
Posts: 660
Liked: 98 times
Joined: Dec 20, 2015 6:24 pm
Contact:

Re: find non existing VMs in excludes and collect VM and Disk excludes in a useful manner

Post by pirx »

This is now working for me

$allVmIds = (Find-VBRViEntity | Where-Object{$_.Type -eq "Vm"}).Reference
$backupJobs = Get-VBRJob -Name "*" | where {$_.JobType -eq 'Backup'}
$jObjects = Get-VBRJobObject -Job $backupJob | Where-Object {$_.TypeDisplayName -eq "Virtual Machine" -and $_.Type -eq "Exclude"}

foreach ( $jObject in $jObjects) {
$jObject | Where-Object {$_.Object.ObjectRef -notin $allVmIds}
}
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 8 guests