PowerShell script exchange
Post Reply
pirx
Veteran
Posts: 663
Liked: 99 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: 3083
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: 663
Liked: 99 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: 663
Liked: 99 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 » 1 person likes this post

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}
}
pirx
Veteran
Posts: 663
Liked: 99 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 »

Above did not work, I've now below code. But I think it's still nor correct. With my test VMs it worked as I expected. But I noticed later that it did not return all VMs with excludes that exist and I could not figure our yet why.
$allVmIds = (Find-VBRViEntity | Where-Object{$_.Type -eq "Vm"}).Reference
$backupJobs = Get-VBRJob -Name * | where {$_.JobType -eq 'Backup'}


foreach ($backupJob in $backupJobs) {
$jObjects = Get-VBRJobObject -Job $backupJob | Where-Object {$_.TypeDisplayName -eq "Virtual Machine" -and ($_.Type -eq "Exclude" -or $_.Type -eq "ExcludeDisksChild") }
# VM Excludes
foreach ($jObject in $jObjects) {
if ($jObject | Where-Object {$allVmIds -notcontains $_.Object.ObjectRef }) {
Write-Host "$($jObject.Name), $($backupJob.Name), $($jObject.Type)"
}
}
}
david.domask
Veeam Software
Posts: 3083
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,

Can you show an example of objects that were not successfully returned with the code blocks above? (yours and mine)

My initial code was a quick test to get VM excludes, I did not test on disks, but the following quick test worked:

Code: Select all

$allVmIds = Find-VBRViEntity -VMsAndTemplates | Where-Object{$_.Type -eq "Vm"} | Select -Property Reference
$backupJob = Get-VBRJob -Name "dd-ps-exclude-test"
$obj = Get-VBRJobObject -Job $backupJob
$obj | Where-Object {$_.Object.ObjectRef -notin $allVmIds -and $_.Object.ViType -eq "VirtualMachine"}

Name                      Type       ApproxSize       Location
----                      ----       ----------       --------
dd-tinyvm3_replica        Exclude    0 B              some-vecenter.local
dd-multi-disk             Exclude... 0 B               some-vecenter.local
You're correct I forgot to filter the JobObjects to only return VMs, but not sure why the above would not work, so maybe if you can examine unexpected objects in the output, it will reveal what we're missing.
David Domask | Product Management: Principal Analyst
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest