Home ESX/ESXi PowerShell: CommVault report from vCenter

PowerShell: CommVault report from vCenter

by Kliment Andreev
0 comment
Reading Time: < 1 minute

Recently, we moved to CommVault for backing up VMs. We use a CommVault solution that interacts with NetApp for taking snaps. Once the backup runs, it time-stamps the VM with the last backup taken. If you don’t need an e-mail in HTML format with the report, remove the lines from 103 to 113. Finally, remove the comment line # from line 57 if you want the report on the screen.

cap01

Based on this info, I made a script that loops through all clusters and VMs and dumps the report in a mail. You can remove lines 5 to 14 if you don’t want the script scheduled.

# PowerCLI script that lists all VMs under clusters that start with USCORPROD*
# and prints the Last Backup attribute from CommVault
# If the difference between the run time of the script and the last backup is
# more than $HoursDifference, write the output with red color
Add-PSSnapin VMware.VimAutomation.Core
Add-PSSnapin VMware.VimAutomation.Vds
if(get-item HKLM:\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns\VMware.VimAutomation.Core){
	. ((get-item HKLM:\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns\VMware.VimAutomation.Core).GetValue("ApplicationBase")+"\Scripts\Initialize-PowerCLIEnvironment.ps1")
}
else
{
	write-warning "PowerCLI Path not found in registry, please set path to Initialize-PowerCLIEnvironment.ps1 manually. Is PowerCli aleady installed?"
	. "D:\Programs (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"
}

# Specify the vCenter server
$server = "vcenter"
# Specify the difference in hours to compare
$HoursDifferenceCompare = 24
# Cluster filter
$ClusterFilter = "*"
# Connect to vCenter
Connect-VIServer -Server $server -user domain\username -password vcpassword
# Initalize the main HTML content
$midHTML=""
# Get each cluster that starts with $ClusterFilter and sort them. 
# Make sure only unique values are returned
ForEach ($Cluster in Get-Cluster $ClusterFilter | Sort -unique)
{
	# Get all the VMs per cluster
	$VMs = Get-Cluster $Cluster | Get-VM 
	# Loop each VM
	ForEach ($VM in $VMs)
	{
		# Get the last backup attribute
		$LastBackup = Get-VM $VM | Get-Annotation -CustomAttribute "Last Backup"
		# Parse the output, trinm the string
		$LastBackupTimeStamp = $LastBackup -replace "Last Backup:"
		# Get the date/time when the script started
		$StartDate=(Get-Date)
		# The end date is the Last Backup time for the VM
		# If the VM was never backed up, assume 01/01/70
		if ($LastBackupTimeStamp -eq '') {
			$EndDate = [datetime]'January 1, 1970'
			$LastBackupTimeStamp = "NEVER"}
		else {
			$EndDate = [datetime]$LastBackupTimeStamp 
			}
		# Calculate the difference
		$TimeSpan = New-TimeSpan -Start $StartDate -End $EndDate
		# It's a negative value, so get the absolute
		$DaysDifference = [math]::abs($TimeSpan.Days)
		$HoursDifference = [math]::abs($TimeSpan.Hours) + ($DaysDifference * 24)
		# Print the output
		if ($HoursDifference -gt $HoursDifferenceCompare) {
			# Remove for debugging if needed
			#Write-Host $Cluster " " $VM " " $LastBackupTimeStamp
			# Build the HTML
			$midHTML = $midHTML + '<font color="red">'
			$midHTML = $midHTML + "<p>" + $Cluster + " " + $VM.Name.PadRight(30,"-") + " " + $LastBackupTimeStamp + "</p>"
			$midHTML = $midHTML + "</font>"
		} else {
			$midHTML = $midHTML + "<p>" + $Cluster + " " + $VM.Name.PadRight(30,"-") + " " + $LastBackupTimeStamp + "</p>"
		}
	}	
}

# Top HTML header
$topHTML = @"
<!DOCTYPE html>
<html>
<body>
<font face="Courier New, Courier, monospace">
<style>
p
{
  margin:0;
  padding:0;
  font-size:12px;
    line-height:12px;
}
div
{
  margin:0;
  padding:0;
}
</style>
<div>
<p>&#160;&#160;&#160;CLUSTER NAME&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SERVER NAME&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;DATE OF LAST BACKUP</p>
<p>-----------------------------------------------------------------------</p>
"@

# Bottom HTML footer
$bottomHTML = @"
<p>-----------------------------------------------------------------------</p>
<p>Script by K.Andreev - 20160630</p>
</div>
</font>
</body>
</html>
"@

# e-mail setup
$EmailFrom ="[email protected]"
$EmailTo ="[email protected]"
$msg = New-Object System.Net.Mail.MailMessage ($EmailFrom,$EmailTo)
$msg.subject = "CommVault Report " + $StartDate
$msg.IsBodyHtml = $true;
$msg.Body =$topHTML + $midHTML + $bottomHTML
$smtpClient = New-Object System.Net.Mail.SmtpClient
$smtpClient.Host = "smtp.server.com"
$smtpClient.EnableSsl = $false
$smtpClient.Send($msg)

# Disconnect from vCenter
Disconnect-VIServer -Server $Server -Confirm:$false
0 comment
0

Related Articles

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More