PowerShell script exchange
mooreka777
Influencer
Posts: 18
Liked: 1 time
Joined: Jan 20, 2011 1:05 pm
Full Name: K Moore
Contact:

Script to Verify VM's are backed up?

Post by mooreka777 »

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

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

Post by Vitaliy S. »

Hi Kelly,

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

Thanks!
mooreka777
Influencer
Posts: 18
Liked: 1 time
Joined: Jan 20, 2011 1:05 pm
Full Name: K Moore
Contact:

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

Post by mooreka777 »

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

TY
tsightler
VP, Product Management
Posts: 6009
Liked: 2842 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

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

Post by tsightler » 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
VP, Product Management
Posts: 6009
Liked: 2842 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

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

Post by tsightler » 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"
  }
}
dellock6
Veeam Software
Posts: 6137
Liked: 1928 times
Joined: Jul 26, 2009 3:39 pm
Full Name: Luca Dell'Oca
Location: Varese, Italy
Contact:

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

Post by dellock6 »

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
Principal EMEA Cloud Architect @ Veeam Software

@dellock6
https://www.virtualtothecore.com/
vExpert 2011 -> 2022
Veeam VMCE #1
tsightler
VP, Product Management
Posts: 6009
Liked: 2842 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

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

Post by tsightler »

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.
dellock6
Veeam Software
Posts: 6137
Liked: 1928 times
Joined: Jul 26, 2009 3:39 pm
Full Name: Luca Dell'Oca
Location: Varese, Italy
Contact:

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

Post by dellock6 »

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
Principal EMEA Cloud Architect @ Veeam Software

@dellock6
https://www.virtualtothecore.com/
vExpert 2011 -> 2022
Veeam VMCE #1
tsightler
VP, Product Management
Posts: 6009
Liked: 2842 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

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

Post by tsightler » 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.
dellock6
Veeam Software
Posts: 6137
Liked: 1928 times
Joined: Jul 26, 2009 3:39 pm
Full Name: Luca Dell'Oca
Location: Varese, Italy
Contact:

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

Post by dellock6 »

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
Principal EMEA Cloud Architect @ Veeam Software

@dellock6
https://www.virtualtothecore.com/
vExpert 2011 -> 2022
Veeam VMCE #1
Gav@GH
Influencer
Posts: 21
Liked: 15 times
Joined: Jul 20, 2012 12:27 am
Contact:

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

Post by Gav@GH »

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)"
  }
}

singy2002
Service Provider
Posts: 66
Liked: 1 time
Joined: Mar 22, 2010 8:43 am
Full Name: Andrew Singleton
Contact:

Script for machines not part of any backup?

Post by singy2002 »

[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
tsightler
VP, Product Management
Posts: 6009
Liked: 2842 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

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

Post by tsightler » 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.
Gatoo
Novice
Posts: 6
Liked: 1 time
Joined: Jul 06, 2012 4:55 pm
Full Name: Gatien GHEZA
Contact:

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

Post by Gatoo »

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
tsightler
VP, Product Management
Posts: 6009
Liked: 2842 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

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

Post by tsightler »

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.
Gatoo
Novice
Posts: 6
Liked: 1 time
Joined: Jul 06, 2012 4:55 pm
Full Name: Gatien GHEZA
Contact:

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

Post by Gatoo » 1 person likes this post

Hi,

I tried the dev script and everything works perfect! No more errors. Exclusions are ok, colors ok.

Some adds possible :
- Sort the result in alphabetical order (A-Z)
- Mail sending
- Export to html

Congrats!
tsightler
VP, Product Management
Posts: 6009
Liked: 2842 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

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

Post by tsightler » 1 person likes this post

Find-VBRObject : Cannot validate argument on parameter 'Server'. The argument is null. Supply a non-null argument and try the command again.
At C:\sources\powershell\Veeam\vm_backup_status_dev.ps1:23 char:33
+ $vmobjs = Find-VBRObject -Server <<<< $vcenterobj | Where-Object {$_.Type -eq "VirtualMachine"}
This error indicates that it didn't properly locate the vCenter server object which implies that the $vCenter server parameter didn't match the name of the vCenter as it was added to VMware. The only lines of code involved are as follows:

Code: Select all

# vCenter server
$vcenter = "<vcenter_name>"
$vcenterobj = Get-VBRServer -Name $vcenter
$vmobjs = Find-VBRObject -Server $vcenterobj | Where-Object {$_.Type -eq "VirtualMachine"}
These lines aren't really different between the development version and the older versions. The "<vcenter_name>" must match the name of the VM as it's shown within Veeam so that the $vcenterobj will be populated. Any chance you had a typo in the vCenter server name?
Gatoo
Novice
Posts: 6
Liked: 1 time
Joined: Jul 06, 2012 4:55 pm
Full Name: Gatien GHEZA
Contact:

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

Post by Gatoo »

I edited my message but too slowly appearently ;)

Sorry, I put the fqdn of my server instead of it's netbios name (as it's registered in the VEEAM server).
tsightler
VP, Product Management
Posts: 6009
Liked: 2842 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

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

Post by tsightler »

Gatoo wrote:I tried the dev script and everything works perfect! No more errors. Exclusions are ok, colors ok.

Some adds possible :
- Sort the result in alphabetical order (A-Z)
- Mail sending
- Export to html
Thanks for testing. The mail and HTML options are planned as soon as I get time. The current results should be sorted by Job Name A-Z, are you saying you also want the VMs within the jobs to be sorted this way or are you seeing some other sorting behavior?
Gatoo
Novice
Posts: 6
Liked: 1 time
Joined: Jul 06, 2012 4:55 pm
Full Name: Gatien GHEZA
Contact:

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

Post by Gatoo »

Oh yeah you sort by JOB so all the VM are in order of the job.

I was speaking about VM order absolutely, but that's a details and it's not a "must have".

EDIT : just launch the script again and you're right, it's definitely more practical to sort by jobs than by vms ;)
Gatoo
Novice
Posts: 6
Liked: 1 time
Joined: Jul 06, 2012 4:55 pm
Full Name: Gatien GHEZA
Contact:

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

Post by Gatoo »

Hey! Do you have any update about this dude?
Vitaliy S.
VP, Product Management
Posts: 27055
Liked: 2710 times
Joined: Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov
Contact:

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

Post by Vitaliy S. »

Hi Gatien, I guess Tom has put this script on hold, since this report and other requested functionality will be available in Veeam ONE natively. If you're going to VMworld in Barcelona make sure you stop at our booth to see everything yourself.
tsightler
VP, Product Management
Posts: 6009
Liked: 2842 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

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

Post by tsightler » 1 person likes this post

Yes, I'll admit I haven't spent much time on this lately due to exactly that reason. The code as is met the immediate need I was trying to address and actually exceeded my original goals, and while I may still enhance this code in the future (perhaps not everyone will purchase Veeam ONE, though we can hope), I just haven't had the time to work on it lately.
Marshawnm
Novice
Posts: 3
Liked: 1 time
Joined: Aug 16, 2012 6:04 pm
Full Name: Marshawn McLeod
Contact:

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

Post by Marshawnm »

Great script! I am still learning ps, so I wanted to know if the out put can be sent to Out-File instead of writing to the screen?
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

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

Post by veremin »

Great script! I am still learning ps, so I wanted to know if the out put can be sent to Out-File instead of writing to the screen?
Yep, it can be done easily with a few modifications:

Code: Select all

$String = foreach ($vm in $vms.Keys)
{
  if ($vms[$vm] -eq "Protected") {
      write-output "$vm was backed up in the past $DaysToCheck day(s)" 
  } else {
      Write-Output "$vm was NOT backed up in the past $DaysToCheck day(s)" 
   }
} 
$String > D:\Test.txt
Thus, in the end of the script .txt-file will be created, containing all information regarding protected VMs.

If we tried to be a little bit more creative, we could put into use the following example:

Code: Select all

$FilePath = 'D:\VMProtectedReport\'+("{0:yyyy-MM-dd}" -f (get-date))+'.txt'
New-Item -Path $filepath -ItemType file -Force
$String > $filepath


In this case, VMProtectedReport folder will be created and corresponding report file will fall into it. Additionally, this file will be named in a certain way (date when the script is executed).

Hope this helps.
Thanks.
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

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

Post by veremin » 1 person likes this post

Furthermore, it might be worth taking a look at special report available in VeeamOne, called Protected VMs report. It shows what VMs in your virtual environment are protected with backups and replicas. It provides information on the latest backup/replica state and shows the number of stored restore points for every protected VM.

Hope this helps.
Thanks.
cerede2000
Influencer
Posts: 16
Liked: 1 time
Joined: Sep 09, 2014 2:24 pm
Full Name: Benjamin CEREDE
Contact:

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

Post by cerede2000 » 1 person likes this post

Hello,

More simply for compare ?

Code: Select all

$esx=Get-VM | Where-Object {$_.PowerState -eq "PoweredOn"} | foreach{$_.Name} | Sort-Object
$backup=Get-VBRBackupSession | Where-Object {$_.JobType -eq "Backup" -or $_.JobType -eq "Replica" -and $_.EndTime}|foreach{$_.gettasksessions() | Where-Object {$_.Status -ne "Failed"}} |foreach{$_.Name} | Sort-Object | Get-Unique
Compare-Object $esx $backup
Andreas Neufert
VP, Product Management
Posts: 6707
Liked: 1401 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

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

Post by Andreas Neufert »

I searched for a way to output a single VM status from a job and found this topic. I place this example here for those who just want to find this out.

Code: Select all

Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
$JobName = "YourJobName"
$JobVMName = "YourVMNameinthatJob"
$JobObjectSession = Get-VBRBackupSession | Where-Object  {$_.JobName -eq $JobName } | Sort CreationTime -Descending | Select -First 1
$JobObjectSessionLastVM = $JobObjectSession.gettasksessions() | Where-Object {$_.Name -eq $JobVMName}
[string]$JobObjectSessionLastVM.Status
Output is "Success", "Warning", "InProgress", "Failed" or "" (Empty).
It can be empty when the job never runned or the actual job running but do not process this VM.
Geoffroi
Lurker
Posts: 2
Liked: 3 times
Joined: Mar 23, 2015 3:43 pm
Full Name: Geoffroi Genot
Contact:

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

Post by Geoffroi » 3 people like this post

Hello,

Inspired by the script of tsighlter I've made a plugin for vCheck ( https://github.com/alanrenouf/vCheck-vSphere )

Code: Select all

#Verify if Virtual Machines have been backed up
#Writed by Geoffroi Genot based on the work of tsighlter on VEEAM Forum (http://forums.veeam.com/powershell-f26/script-to-verify-vm-s-are-backed-up-t12714.html)

# Start of Settings
# List of VM to exclude (by name) separated by a comma i.e: @("vm1","vm2")
$excludevms=@()
# Number of days to check, if a VM is not backed up within this interval it will be considered as not backed up         
$DaysToCheck= 7
# End of Settings

#Try to load VEEAM snapin, otherwise stop the process
asnp "VeeamPSSnapIn" -ErrorAction Stop 

# 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
$Result=@()
foreach ($vm in ($FullVM | Where { $_.Runtime.PowerState -eq "poweredOn" } | ForEach-Object {$_ | Select-object @{Name="VMname";Expression={$_.Name}}}))  {
   if (!$excludedvms.ContainsKey($vm.VMname)) {
        $vm | Add-Member -MemberType NoteProperty -Name "backed_up" -Value $False 
        $Result += $vm
    }
}

# 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"
foreach ($session in $vbrsessions) {
    foreach ($vm in ($session.gettasksessions() | Where-Object {$_.Status -ne "Failed"} | ForEach-Object { $_ | Select-object @{Name="VMname";Expression={$_.Name}}})) {
		$VMObj = $Result | where {$_.VMName -eq $vm.VMname }
		if ($VMObj){
			$VMObj.backed_up = $true
		}
    }
}

$Result | where {$_.backed_up -eq $false}
$Title = "Running VMs that were not backed up in the last $DaysToCheck day(s)"
$Header =  "Running VMs that were not backed up in the last $DaysToCheck day(s)"
$Comments = "The following VMs were not backed up in the last $DaysToCheck day(s). They are probably not in a backup job or were in a failure state"
$Display = "Table"
$Author = "Geoffroi Genot"
$PluginVersion = 1.0
$PluginCategory = "VEEAM"
I hope it will be useful :)
glamic26
Enthusiast
Posts: 27
Liked: 11 times
Joined: Apr 21, 2015 12:10 pm
Contact:

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

Post by glamic26 » 1 person likes this post

Thanks Geoffroi that could be really useful to me. Out of interest how do you get this to run as part of your vCheck without it running on you Veeam server? I only seem to be able to run the Veeam Powershell commands from the Veeam Backup Server. I tried to install the Veeam Powershell plugin on the server that runs our vCheck script but it won't install it without Veeam B&R installed.
Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests