Exclude clusters from "Am I doing backups of all my VMs?"

PowerShell script exchange

Exclude clusters from "Am I doing backups of all my VMs?"

Veeam Logoby anglerzac » Fri Feb 10, 2017 2:59 pm

Can anyone give any hints to modifying this script http://www.virtualtothecore.com/en/back ... 6-edition/ so I can exclude Clusters.

I tried simply creating $excludeCluster where I thought it appropriate, e.g.
Code: Select all
#
# Verify protected VM's
#
####################################################################
#
# MIT License
#
#Copyright (c) 2016 Tom Sightler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
####################################################################
#
# Run the script against vCenter and Veeam server and checks which VM's
# have been protected in the last 23 hours, and which are not protected

asnp "VeeamPSSnapIn" -ErrorAction SilentlyContinue

####################################################################
# Configuration
#
# To exclude VMs from report add VM names to be excluded as follows
# simple wildcards are supported:
# $excludevms=@("vm1","vm2", "*_replica")
$excludeVMs = @("")
# Exclude VMs in the following vCenter folder(s) (does not exclude sub-folders)
# $excludeFolder =  = @("folder1","folder2","*_testonly")
$excludeFolder = @("")
# Exclude VMs in the following vCenter datacenter(s)
# $excludeDC =  = @("dc1","dc2","dc*")
$excludeDC = @("OffsiteDC")#
# Exclude VMs in the following vCenter datacenter(s)
# $excludeDC =  = @("dc1","dc2","dc*")
$excludeCluster = @("NoVeeamCluster","TrainingCluster","TestDevCluster")
# This variable sets the number of hours of session history to
# search for a successul backup of a VM before considering a VM
# "Unprotected".  For example, the default of "24" tells the script
# to search for all successful/warning session in the last 24 hours
# and if a VM is not found then assume that VM is "unprotected".
$HourstoCheck = 24
####################################################################

# Convert exclusion list to simple regular expression
$excludevms_regex = ('(?i)^(' + (($excludeVMs | ForEach {[regex]::escape($_)}) -join "|") + ')$') -replace "\\\*", ".*"
$excludefolder_regex = ('(?i)^(' + (($excludeFolder | ForEach {[regex]::escape($_)}) -join "|") + ')$') -replace "\\\*", ".*"
$excludedc_regex = ('(?i)^(' + (($excludeDC | ForEach {[regex]::escape($_)}) -join "|") + ')$') -replace "\\\*", ".*"
$excludeCluster_regex = ('(?i)^(' + (($excludeCluster | ForEach {[regex]::escape($_)}) -join "|") + ')$') -replace "\\\*", ".*"

$vms=@{}

# Build a hash table of all VMs.  Key is either Job Object Id (for any VM ever in a Veeam job) or vCenter ID+MoRef
# Assume unprotected (!), and populate Cluster, DataCenter, and Name fields for hash key value
Find-VBRViEntity |
    Where-Object {$_.Type -eq "Vm" -and $_.VmFolderName -notmatch $excludefolder_regex} |
    Where-Object {$_.Name -notmatch $excludevms_regex} |
    Where-Object {$_.Path.Split("\")[1] -notmatch $excludedc_regex} |
Where-Object {$_.Path.Split("\")[1] -notmatch $excludeCluster_regex} |
    ForEach {$vms.Add(($_.FindObject().Id, $_.Id -ne $null)[0], @("!", $_.Path.Split("\")[1], $_.Path.Split("\")[2], $_.Name))}

Find-VBRViEntity -VMsandTemplates |
    Where-Object {$_.Type -eq "Vm" -and $_.IsTemplate -eq "True" -and $_.VmFolderName -notmatch $excludefolder_regex} |
    Where-Object {$_.Name -notmatch $excludevms_regex} |
    Where-Object {$_.Path.Split("\")[1] -notmatch $excludedc_regex} |
Where-Object {$_.Path.Split("\")[1] -notmatch $excludeCluster_regex} |
    ForEach {$vms.Add(($_.FindObject().Id, $_.Id -ne $null)[0], @("!", $_.Path.Split("\")[1], $_.VmHostName, $_.Name))}

# Find all backup task sessions that have ended in the last x hours and not ending in Failure
$vbrtasksessions = (Get-VBRBackupSession |
    Where-Object {$_.JobType -eq "Backup" -and $_.EndTime -ge (Get-Date).addhours(-$HourstoCheck)}) |
    Get-VBRTaskSession | Where-Object {$_.Status -ne "Failed"}

# Compare VM list to session list and update found VMs status to "Protected"
If ($vbrtasksessions) {
    Foreach ($vmtask in $vbrtasksessions) {
        If($vms.ContainsKey($vmtask.Info.ObjectId)) {
            $vms[$vmtask.Info.ObjectId][0]=$vmtask.JobName
        }
    }
}

$vms = $vms.GetEnumerator() | Sort-Object Value

# Output VMs in color coded format based on status
# VM's with a job name of "!" were not found in any job
foreach ($vm in $vms) {
    if ($vm.Value[0] -ne "!") {
        write-host -foregroundcolor green (($vm.Value[1]) + "\" + ($vm.Value[2]) + "\" + ($vm.Value[3])) "is backed up in job:" $vm.Value[0]
    } else {
        write-host -foregroundcolor red (($vm.Value[1]) + "\" + ($vm.Value[2]) + "\" + ($vm.Value[3])) "is not found in any backup session in the last" $HourstoCheck "hours"
    }
}
anglerzac
Influencer
 
Posts: 22
Liked: never
Joined: Fri Nov 14, 2014 11:03 am
Full Name: Zac

Re: Exclude clusters from "Am I doing backups of all my VMs?

Veeam Logoby Vitaliy S. » Fri Feb 10, 2017 3:15 pm

I guess this guy should help since it is his blog :) Try to send Luca a PM for assistance.
Vitaliy S.
Veeam Software
 
Posts: 19558
Liked: 1102 times
Joined: Mon Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov

Re: Exclude clusters from "Am I doing backups of all my VMs?

Veeam Logoby tsightler » Fri Feb 10, 2017 3:44 pm

And it's my code so I can probably help. Unfortunately my calendar is full today, but I'll try to poke at it when I manage to get a few minutes. I already have some code that grabs the cluster name (used in Shawn Masterson's brilliant MyVeeamReport), so I'd just need to combine that with a filter, should be easy.
tsightler
Veeam Software
 
Posts: 4768
Liked: 1737 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Re: Exclude clusters from "Am I doing backups of all my VMs?

Veeam Logoby anglerzac » Sat Feb 11, 2017 1:46 pm

many thanks
anglerzac
Influencer
 
Posts: 22
Liked: never
Joined: Fri Nov 14, 2014 11:03 am
Full Name: Zac

Re: Exclude clusters from "Am I doing backups of all my VMs?

Veeam Logoby tsightler » Wed Feb 15, 2017 4:18 pm

Finally managed to take a look at this and turns out it was really easy as you were very close. This only thing you need to do is replace:
Code: Select all
Where-Object {$_.Path.Split("\")[1] -notmatch $excludeCluster_regex} |

with
Code: Select all
Where-Object {$_.Path.Split("\")[2] -notmatch $excludeCluster_regex} |

This is because the cluster name is the third value in the path, not the second one, so simply changing which array object you filtered on is all that needs to be changed.

One thing, templates are not registered to a cluster, only a host, so to exclude those you'd have to enter the host name instead of cluster name into the $excludeCluster variable.
tsightler
Veeam Software
 
Posts: 4768
Liked: 1737 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Re: Exclude clusters from "Am I doing backups of all my VMs?

Veeam Logoby anglerzac » Thu Feb 16, 2017 2:42 pm

really great thanks, I have no powershell knowledge so was just trying through logic and would never have worked that out myself.
anglerzac
Influencer
 
Posts: 22
Liked: never
Joined: Fri Nov 14, 2014 11:03 am
Full Name: Zac


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 9 guests