Script to Verify VM's are backed up?

PowerShell script exchange

Script to Verify VM's are backed up?

Veeam Logoby mooreka777 » Fri Jul 06, 2012 3:26 pm

Hello There,

I was wondering if there was a script out there that can get a list of all VM's that were backed up in the last 24 hours via veeam and compare it to a list of VM's in vCenter? I want to make sure I have policies for all of the VM's.

Thanks for any help or guidance on this.

-Kelly
mooreka777
Influencer
 
Posts: 18
Liked: 1 time
Joined: Thu Jan 20, 2011 1:05 pm
Full Name: K Moore

Re: Script to Verify VM's are backed up?

Veeam Logoby Vitaliy S. » Fri Jul 06, 2012 4:12 pm

Hi Kelly,

Please take a look at this post from Luca, might be exactly what you're looking for.

Thanks!
Vitaliy S.
Veeam Software
 
Posts: 19960
Liked: 1145 times
Joined: Mon Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov

Re: Script to Verify VM's are backed up?

Veeam Logoby mooreka777 » Fri Jul 06, 2012 4:32 pm

I didn't realize veeam could do custom attributes now. testing now!

TY
mooreka777
Influencer
 
Posts: 18
Liked: 1 time
Joined: Thu Jan 20, 2011 1:05 pm
Full Name: K Moore

Re: Script to Verify VM's are backed up?

Veeam Logoby tsightler » Sat Jul 14, 2012 3:00 am 1 person likes this post

I have created the following script that connects to vCenter, pulls a complete list of all VMs (would be easy to provide filters and subsets), connects to a Veeam server, enumerates all jobs that ended in the last 24 hours, finds all individual VMs that were backed up in those jobs with either "Success" or "Warning", and then outputs them as a list. It requires no custom attribute updates or other changes to your jobs and also supports a simple syntax for excluding VMs from the report. It's still a work in progress but I thought others might find it useful in it's current form or have suggestions for enhancements. Currently it only supports a single vCenter and uses just the VM name, so duplicate VM names would definitely trip it up.

Some of the code is based on concepts that others have posted, for example the output routine is virtually identical to the code in Luca's script.

Code: Select all
asnp "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue
asnp "VeeamPSSnapIn" -ErrorAction SilentlyContinue

####################################################################
# Configuration
# vCenter server
$vcenter = "<vcenter_server>"
# To Exclude VMs from report add VM names to be excluded as follows
# $excludevms=@("vm1","vm2")
$excludedvms=@()
####################################################################


Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | out-null
Connect-ViServer $vcenter | out-null

# Get a list of all VMs from vCenter
$vms = Get-VM | ForEach-Object {$_ | Select-object @{Name="VMname";Expression={$_.Name}}}


# Find all backup jobs that have ended in the last 24 hours
$backupjobs = Get-VBRJob | Where-Object {$_.IsBackup -and $_.findlastsession().progress.stoptime -ge (Get-Date).addhours(-24)}

# Find all successfully backed up objects in those jobs (Success and Warning)
$i=0
foreach ($backupjob in $backupjobs) {
    if ($i -eq 0) {
        $backupsessions = $backupjob.findlastsession().gettasksessions() | Where-Object {$_.Status -ne "Failed"}
    } else {
        $backupsessions += $backupjob.findlastsession().gettasksessions() | Where-Object {$_.Status -ne "Failed"}
    }
    $i++
}

# Reduce to list of VM names in backup sessions
$backedupvms = $backupsessions | ForEach-Object {$_ | Select-object @{Name="VMname";Expression={$_.Name}}}

# Build hash table with excluded VMs
$excludedvmhash= @{}
foreach ($vm in $excludedvms) {
    $excludedvmhash.Add($vm, "Excluded")
}

# Build hash table of VMs from vCenter, skipping excluded VMs
# assume VM is not protected
$vmhash= @{}
foreach ($vm in $vms) {
    if (!$excludedvmhash.ContainsKey($vm.VMname)) {
        $vmhash.Add($vm.VMname, "Unprotected")
    }
}

# Loop through backed up VMs, if VM name exist in hash
# update value to protected
foreach($backedupvm in $backedupvms) {
    if($vmhash.ContainsKey($backedupvm.VMname)) {
        $vmhash[$backedupvm.VMname]="Protected"
    }
}

# Output VMs in color coded format based on status.
foreach ($vm in $vmhash.Keys)
{
  if ($vmhash[$vm] -eq "Protected") {
      write-host -foregroundcolor green "$vm is backed up"
  } else {
      write-host -foregroundcolor red "$vm is NOT backed up"
  }
}
tsightler
Veeam Software
 
Posts: 4872
Liked: 1821 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Re: Script to Verify VM's are backed up?

Veeam Logoby tsightler » Sun Jul 15, 2012 12:45 am 2 people like this post

OK, so my first cut had a lot of bugs, and was easily tripped up by things like failed jobs or jobs that run more than once a day. This new version should take care of all of those cases as it properly looks through all backup sessions, even retries. Also, the code has been significantly cleaned up to read the VM list directly into a hash table and then update the status based on whether the VM is found in a session removing a lot of ugly code. I'll try to get these up on my website and provide a link so that users can track future enhancements but I wanted to get this version out to people as it seems to work pretty well in the environments I have tested it in, although a little slow.

Code: Select all
asnp "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue
asnp "VeeamPSSnapIn" -ErrorAction SilentlyContinue

####################################################################
# Configuration
# vCenter server
$vcenter = "<vcenter_server>"
# To Exclude VMs from report add VM names to be excluded as follows
# $excludevms=@("vm1","vm2")
$excludevms=@()
####################################################################

# Connect to vCenter
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | out-null
Connect-ViServer $vcenter | out-null

# Build hash table with excluded VMs
$excludedvms=@{}
foreach ($vm in $excludevms) {
    $excludedvms.Add($vm, "Excluded")
}

# Get a list of all VMs from vCenter and add to hash table, assume Unprotected
$vms=@{}
foreach ($vm in (Get-VM | ForEach-Object {$_ | Select-object @{Name="VMname";Expression={$_.Name}}}))  {
    if (!$excludedvms.ContainsKey($vm.VMname)) {
        $vms.Add($vm.VMname, "Unprotected")
    }
}

# Find all backup job sessions that have ended in the last 24 hours
$vbrsessions = Get-VBRBackupSession | Where-Object {$_.JobType -eq "Backup" -and $_.EndTime -ge (Get-Date).addhours(-24)}

# Find all successfully backed up VMs in selected sessions (i.e. VMs not ending in failure) and update status to "Protected"
$backedupvms=@{}
foreach ($session in $vbrsessions) {
    foreach ($vm in ($session.gettasksessions() | Where-Object {$_.Status -ne "Failed"} | ForEach-Object { $_ | Select-object @{Name="VMname";Expression={$_.Name}}})) {
        if($vms.ContainsKey($vm.VMname)) {
            $vms[$vm.VMname]="Protected"
        }
    }
}

# Output VMs in color coded format based on status.
foreach ($vm in $vms.Keys)
{
  if ($vms[$vm] -eq "Protected") {
      write-host -foregroundcolor green "$vm is backed up"
  } else {
      write-host -foregroundcolor red "$vm is NOT backed up"
  }
}
tsightler
Veeam Software
 
Posts: 4872
Liked: 1821 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Re: Script to Verify VM's are backed up?

Veeam Logoby dellock6 » Mon Jul 16, 2012 1:41 pm

Hi Tom,
tried to run your script in my test environment, and I get this error:

Code: Select all
You cannot call a method on a null-valued expression.
At C:\scripts\backup-check.ps1:38 char:46
+     foreach ($vm in ($session.gettasksessions <<<< () | Where-Object {$_.Status -ne "Failed"} | ForEach-Object { $_ | Select-object @{Name="VMname";Expression={$_.Name}}
})) {
    + CategoryInfo          : InvalidOperation: (gettasksessions:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull


Any idea?

Also, a small improvement would be to change the 24 hours into a variable so the user can set the desired delta between actual date and last backup date.

Great job, if you don't mind I would like to integrate this script once it's ok into my draft post where I was describing mine (which is worst, since it needs connection to vCenter and additional notes in VMs).
Luca.
Luca Dell'Oca
EMEA Cloud Architect @ Veeam Software

@dellock6
http://www.virtualtothecore.com
vExpert 2011-2012-2013-2014-2015-2016
Veeam VMCE #1
dellock6
Veeam Software
 
Posts: 5138
Liked: 1381 times
Joined: Sun Jul 26, 2009 3:39 pm
Location: Varese, Italy
Full Name: Luca Dell'Oca

Re: Script to Verify VM's are backed up?

Veeam Logoby tsightler » Mon Jul 16, 2012 1:57 pm

Well, mine still needs a connection to vCenter to pull the list of VMs, in then compares that to a list of VMs found in the job sessions from the last 24 hours so it doesn't need the notes field. I'm assuming you properly modified the script with the name of your vCenter server?

I'm not sure about the error, perhaps you have some sessions in the last 24 hours that have no VMs at all. I may need to check for sessions that are Null. I'll need to see if I can reproduce that or I may have to send you some code with a little debugging.

I'm already planning to "parametrize" some of the options, such as the default length, and perhaps job type (some customers/partners are interested in a similar report for replication). By default the current code logs into vCenter using the account that the script is running under (integrated authentication), so I need to allow users to pass the username/password for vCenter. I'm writing some of these scripts primarily to help me practice and improve my Powershell skills, which I think are getting better (the difference between cut one and cut two is pretty significant, and I've already cleaned up a few more useless lines). I thought that working on some common request from customers/partners would be a good way to improve my own skill and help the community as well.
tsightler
Veeam Software
 
Posts: 4872
Liked: 1821 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Re: Script to Verify VM's are backed up?

Veeam Logoby dellock6 » Mon Jul 16, 2012 3:01 pm

I tried both vCenter via hostname and IP address, same result.
There are some jobs that run only once per week, could be the problem?

Your script anyway is powerful right now, maybe is my environment that is not so "standard" as others. But mayb this strangeness can be helpful in creating a better script. IF you want to send me some debugging version, feel free to DM me.

Luca.
Luca Dell'Oca
EMEA Cloud Architect @ Veeam Software

@dellock6
http://www.virtualtothecore.com
vExpert 2011-2012-2013-2014-2015-2016
Veeam VMCE #1
dellock6
Veeam Software
 
Posts: 5138
Liked: 1381 times
Joined: Sun Jul 26, 2009 3:39 pm
Location: Varese, Italy
Full Name: Luca Dell'Oca

Re: Script to Verify VM's are backed up?

Veeam Logoby tsightler » Tue Jul 17, 2012 8:02 pm 2 people like this post

Another script update which eliminates the requirement for VMware vPower CLI, now only pure Veeam Powershell is used (replaced Get-VM with Find-VBRObject). This also means that vCenter credentials aren't required as the credentials saved by Veeam are used. Duplicate VMs within the same vCenter are still a problem though. Working on this and a few other enhancements. You can track future versions here.
tsightler
Veeam Software
 
Posts: 4872
Liked: 1821 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Re: Script to Verify VM's are backed up?

Veeam Logoby dellock6 » Tue Jul 17, 2012 8:10 pm

I tested this script that Tom sent me yesterday, and I would like to publicly say thanks to him for this great script!!!!

Luca.
Luca Dell'Oca
EMEA Cloud Architect @ Veeam Software

@dellock6
http://www.virtualtothecore.com
vExpert 2011-2012-2013-2014-2015-2016
Veeam VMCE #1
dellock6
Veeam Software
 
Posts: 5138
Liked: 1381 times
Joined: Sun Jul 26, 2009 3:39 pm
Location: Varese, Italy
Full Name: Luca Dell'Oca

Re: Script to Verify VM's are backed up?

Veeam Logoby Gav@GH » Fri Jul 20, 2012 12:44 am

Nice Script @tsightler.

I can see this one going places and added a few ideas. Just saw you've moved it to a blog, so I'll keep an eye out over there for more great stuff.

- Added a $DaysToCheck variable into the config.
- Added Replica Jobs
- I found that (like many using Veeam), it also picked up all of the replica machines, so I tweaked the vCenter list a bit to only inlcude running VMs ie Where-Object {$_.PowerState -eq "PoweredOn"} .

Code: Select all
#Verify if Virtual Machines have been backed up

asnp "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue
asnp "VeeamPSSnapIn" -ErrorAction SilentlyContinue

#-------------------------------------------------------
# Configuration
$vcenter = "vcenter_server"
$excludevms=@()         # eg $excludevms=@("vm1","vm2")
$DaysToCheck= 7
#-------------------------------------------------------

# Connect to vCenter
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | out-null
Connect-ViServer $vcenter | out-null

# Build hash table with excluded VMs
$excludedvms=@{}
foreach ($vm in $excludevms) {
    $excludedvms.Add($vm, "Excluded")
}

# Get a list of all VMs from vCenter and add to hash table, assume Unprotected
$vms=@{}
foreach ($vm in (Get-VM | Where-Object {$_.PowerState -eq "PoweredOn"} | ForEach-Object {$_ | Select-object @{Name="VMname";Expression={$_.Name}}}))  {
   if (!$excludedvms.ContainsKey($vm.VMname)) {
        $vms.Add($vm.VMname, "Unprotected")
    }
}

# Find all backup job sessions that have ended in the last week
$vbrsessions = Get-VBRBackupSession | Where-Object {$_.JobType -eq "Backup" -or $_.JobType -eq "Replica" -and $_.EndTime -ge (Get-Date).adddays(-$DaysToCheck)}

# Find all successfully backed up VMs in selected sessions (i.e. VMs not ending in failure) and update status to "Protected"
$backedupvms=@{}
foreach ($session in $vbrsessions) {
    foreach ($vm in ($session.gettasksessions() | Where-Object {$_.Status -ne "Failed"} | ForEach-Object { $_ | Select-object @{Name="VMname";Expression={$_.Name}}})) {
      if($vms.ContainsKey($vm.VMname)) {
            $vms[$vm.VMname]="Protected"
        }
    }
}

# Output VMs in color coded format based on status.
foreach ($vm in $vms.Keys)
{
  if ($vms[$vm] -eq "Protected") {
      write-host -foregroundcolor green "$vm was backed up in the past $DaysToCheck day(s)"
  } else {
      write-host -foregroundcolor red "$vm was NOT backed up in the past $DaysToCheck day(s)"
  }
}

Gav@GH
Influencer
 
Posts: 21
Liked: 15 times
Joined: Fri Jul 20, 2012 12:27 am

Script for machines not part of any backup?

Veeam Logoby singy2002 » Fri Jul 27, 2012 1:41 pm

[merged]

Hi,
I am wondering if anyone has a script that checks if a VM is not part of any backup?

I am going to be changing the way we backup from cluster based (Select everything and exclude specific) to customer based, so the automatic addition of anything added to the clusters will no longer pick up the new VM's that may be introduced to the enviornment. This will be a safety net to ensure nothing is missed.

If not, has anyone any suggested starting points for this?

Thanks
Andy
singy2002
Service Provider
 
Posts: 63
Liked: 1 time
Joined: Mon Mar 22, 2010 8:43 am
Full Name: Andrew Singleton

Re: Script to Verify VM's are backed up?

Veeam Logoby tsightler » Mon Jul 30, 2012 7:43 pm 1 person likes this post

Just wanted to let people know that I have posted a development version of this script on my blog that includes the following enhancements:

- Proper handling of VMs with duplicate names in different datacenters
- VMs missing from jobs are listed first in the script output
- VMs that are found in backups also display Job Name in which they are found
- Simple wildcards are now support for exclusion list (for example *_replica to exlcude replica VMs)
- Parameter to define length of history to look for VMs in backup jobs (default 24 hours).

If anyone out there using this script can test this version in their environment and report their results that would be great. I'm pretty happy with the data that is currently being provided so assuming there are no major bugs my next step would be to update the output to support an simple HTML report and email options.

The development version can be found in this blog posting.
tsightler
Veeam Software
 
Posts: 4872
Liked: 1821 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Re: Script to Verify VM's are backed up?

Veeam Logoby Gatoo » Tue Aug 14, 2012 11:42 am

Amazing!!!!

Just have some few warning appearing in Red, telling me that the parameter has already been added.

But the colours scheme are perfect.

Miss :
- Ability to exclude *xxxx VMs (ok in the development version, need to test it)
- HTML export
- Mailing
- Multi-vcenter
Gatoo
Service Provider
 
Posts: 6
Liked: 1 time
Joined: Fri Jul 06, 2012 4:55 pm
Full Name: Gatien GHEZA

Re: Script to Verify VM's are backed up?

Veeam Logoby tsightler » Wed Aug 15, 2012 12:57 am

If you would be willing to test the development version and let me know if the errors still appear that would be great. I added checks for duplicate UUID to the hash table which should eliminate the issues you describe.

I'm working on HTML, Mail and multi-vcenter/multi-veeam server but just haven't had a few hours to sit down with the code and get it done. Hopefully I'll have a new development version in the next week or so.
tsightler
Veeam Software
 
Posts: 4872
Liked: 1821 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Next

Return to PowerShell



Who is online

Users browsing this forum: No registered users and 1 guest

cron