PowerShell script exchange
Post Reply
anglerzac
Influencer
Posts: 24
Liked: 1 time
Joined: Nov 14, 2014 11:03 am
Full Name: Zac
Contact:

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

Post by anglerzac »

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"
    }
}
Vitaliy S.
VP, Product Management
Posts: 27107
Liked: 2717 times
Joined: Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov
Contact:

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

Post by Vitaliy S. »

I guess this guy should help since it is his blog :) Try to send Luca a PM for assistance.
tsightler
VP, Product Management
Posts: 6011
Liked: 2843 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

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

Post by tsightler »

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.
anglerzac
Influencer
Posts: 24
Liked: 1 time
Joined: Nov 14, 2014 11:03 am
Full Name: Zac
Contact:

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

Post by anglerzac »

many thanks
tsightler
VP, Product Management
Posts: 6011
Liked: 2843 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

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

Post by tsightler »

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.
anglerzac
Influencer
Posts: 24
Liked: 1 time
Joined: Nov 14, 2014 11:03 am
Full Name: Zac
Contact:

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

Post by anglerzac »

really great thanks, I have no powershell knowledge so was just trying through logic and would never have worked that out myself.
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests