Combining Veeam and PowerCLI

Combining Veeam and PowerCLI

Veeam Backup & Replication is a great tool for backing up your virtual environment. U can use it to backup certain virtual machines or your whole environment by simply creating backup jobs in which u add virtual machines. Veeam has the option which sends an e-mail if a backup was successful or failed.

Sadly there is one thing I couldn’t find and it’s an option to check which virtual machines are being backed up and which are not in a virtual environment.

After some thinking I decided to combine Veeam and PowerCLI as a solution to my problem.

When a job is created there is an advanced option to add a note to a virtual machine which can be used for scripts.

When creating a job click “Advanced” when the following screen appears.

Once the advanced settings open, go to the notifications tab and select “Set successful backup details to this VM attribute”. Change the value from “Notes” to “Backup”.

Every time the job finishes successful Veeam will add an annotation called “Backup” to the virtual machine as seen on the picture below.

Below is a script which will collect all virtual machines in the defined vCenter and run a check on the backup annotation. This will perform a check if the backup was successful in the past 2 days. Finally it will send an overview in plain text via mail.

Please change the required settings on top of the script!

# Set the vCenter Server
$vcenter = "localhost"
# Set the SMTP Server address
$SMTPSRV = "<SMTP SERVER>"
# Set the Email address to receive from
$EmailFrom = "veeam@foonet.be"
# Set the Email address to send the email to
$EmailTo = "<RECEIVER>"
# Set the Email subject
$EmailSubject = "Veeam Backup Information"

# SMTPmail function
function Send-SMTPmail($to, $from, $subject, $smtpserver, $body) {
	$mailer = new-object Net.Mail.SMTPclient($smtpserver)
	$msg = new-object Net.Mail.MailMessage($from,$to,$subject,$body)
	$msg.IsBodyHTML = $false
	$mailer.send($msg)
}

# Get Backedup function
function Get-Backedup ($vm) {
	$val = $vm.CustomFields |where {$_.key -eq "Backup" } | select -Property Value
	$today = Get-Date -Format "d/MM/yyyy"
	$yesterday = get-date -format "d/MM/yyyy" -displayhint date ((get-date).adddays(-1))
	$backupDate = $val.Value
	if ( $backupDate -like "*$today*" -or $backupDate -like "*$yesterday*"  ) {
		$res = 1
	} else {
		$res = 0
	} 
	return ( $res )
}

Connect-VIServer $vcenter | Out-Null

$vms = Get-VM

$resultok=@()
$resultnotok=@()

foreach ($vm in $vms) {
	if (Get-Backedup($vm)) {
		$resultok += "$vm `r`n"
	} else {
		$resultnotok += "$vm `r`n"
	}
}

$body = "==================================================`r`n"
$body += "Backup report $vcenter:`r`n"
$body += "==================================================`r`n`r`n"
$body += "The following VMs have NOT been backed up:`r`n"
$body += "---------------------------------------------------------------`r`n"
$body += $resultnotok | Sort-Object
$body += "`r`n`r`nThe following VMs have been backed up:`r`n"
$body += "--------------------------------------------------------`r`n"
$body += $resultok | Sort-Object

send-SMTPmail -to $EmailTo -from $EmailFrom -subject $EmailSubject -smtpserver $SMTPSRV -body $body

That’s it. Feel free to improve the script or give hints and tips!

Comments are closed.