Here it is: Powershell script to add VEB-EMails

Backup agent for Microsoft Windows servers and workstations (formerly Veeam Endpoint Backup FREE)

Re: Here it is: Powershell script to add VEB-EMails

Veeam Logoby best74 » Mon Feb 01, 2016 9:06 am

Hmm, now i'm only receiving backup finished with warning, and not job detail (e.g. low disk space or whatever)
best74
Novice
 
Posts: 9
Liked: 1 time
Joined: Thu Jan 14, 2016 1:16 pm
Full Name: Brian Løfqvist Jensen

Re: Here it is: Powershell script to add VEB-EMails

Veeam Logoby JudgeFudge » Mon Feb 01, 2016 12:47 pm

How is your Environment? Did you check on VEB what the Backups are saying?
Not every Warningmessage writes in the Database. Please check in the VEB when the Backup finished with warning what warning it says.

@MattMN what exactly do you mean by showing the Time and Date? if you want to have the Date after the Time just delete the variable $start and enter $TimeBackupStarted on HTML part same with $end and $TimeGenerated
JudgeFudge
Influencer
 
Posts: 15
Liked: 8 times
Joined: Thu Jan 07, 2016 4:54 pm
Location: Germany
Full Name: Chris

Re: Here it is: Powershell script to add VEB-EMails

Veeam Logoby best74 » Wed Feb 03, 2016 7:28 am

Hi, my warning in VEB is the following:
Backup location [\\BACKUP\Backup\Support01] is getting low on free disk space (436,9 GB free of 8,0 TB).
best74
Novice
 
Posts: 9
Liked: 1 time
Joined: Thu Jan 14, 2016 1:16 pm
Full Name: Brian Løfqvist Jensen

Re: Here it is: Powershell script to add VEB-EMails

Veeam Logoby JudgeFudge » Wed Feb 03, 2016 12:47 pm 1 person likes this post

About the part why you don't get the reason.. fucking .NET Framework update -.- had the same problem while testing.

I had to update the code again, made another SQL query. Two weeks ago everything worked and now MS fucked it up again. I think this script will never be finished.
Problem is that the variable $reason will no longer be empty in the new .NET version so that's why you get the $Message instead.
I checked it with !$_.reason if its empty and it was but now it won't be recognized anymore. I work now with [System.DBNull]::Value which is the right query for SQL.
Tried it another hundred times with two Machines where I failed Backups on purpose and with the new query it gives me the right Information again.
Mayor update is the part where i connect to the veeam database and fill my variables.

Please Update your code and tell me if it's working again.

Code: Select all
###########################################################
#  #
#  Endpoint Notification    #
#  #
###########################################################

# Set information about sender and recipient
$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = "sender@sender.com"
$emailMessage.To.Add( "recipient@recipient.com" )
$hostname = "TestBackup"

# SMTP Server Informationen
$emailSmtpServer = "yourmailserver.com"
$emailSmtpServerPort = "25"

# Put most info into the body of the email:
$TimeGenerated   =get-eventlog "Veeam Endpoint Backup" -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Format-Wide -property TimeGenerated | out-string
$TimeBackupStarted   =get-eventlog "Veeam Endpoint Backup" -InstanceID 110 -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Format-Wide -property TimeGenerated | out-string
$Source      =get-eventlog "Veeam Endpoint Backup" -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Format-List -property Source | out-string
$EntryType   =get-eventlog "Veeam Endpoint Backup" -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Format-List -property EntryType | out-string
$Message   =get-eventlog "Veeam Endpoint Backup" -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Format-Wide -property Message -AutoSize | out-string
$InstanceID   =get-eventlog "Veeam Endpoint Backup" -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Format-List -property InstanceID| out-string

#Formatting Variables into readable data
$date = Get-Date -format F
$difference = (new-timespan -Start $TimeBackupStarted -End $TimeGenerated)
$duration = "{0:c}" -f $difference
$TimeGenerated = [datetime]::parse($TimeGenerated)
$TimeBackupStarted = [datetime]::parse($TimeBackupStarted)
$start = "{0:HH:mm:ss}" -f $TimeBackupStarted
$end = "{0:HH:mm:ss}" -f $TimeGenerated
$mb = " MB"
$gb = " GB"
$tb = " TB"

# Connecting to the Veeam SQL Database
$key = "hklm:\SOFTWARE\Veeam\Veeam Endpoint Backup"
$User = (get-Item $key).GetValue("SqlLogin")
$Pass = (get-Item $key).GetValue("SqlPassword")
$dbServer = (get-Item $key).GetValue("SqlInstancePipeName")
$db = (get-Item $key).GetValue("SqlDatabaseName")
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$dbServer;Database=$db;uid=$User;password=$Pass;"
$SQLConnection.Open()
$SQLCommand = $SQLConnection.CreateCommand()
$sql = "SELECT TOP 1 stored_size AS size, job_name AS job FROM [VeeamBackup].[dbo].[ReportSessionView] ORDER BY [creation_time] DESC;
SELECT [free_space] AS [free] FROM [VeeamBackup].[dbo].[BackupRepositories] WHERE (name != 'Default Backup Repository')
SELECT  SUM(backup_size) AS backupsize FROM [VeeamBackup].[dbo].[WmiServer.RestorePointsView]
SELECT TOP 1 reason AS Reason, stop_details AS Detail FROM [VeeamBackup].[dbo].[Backup.Model.JobSessions] ORDER BY creation_time DESC"
$SQLCommand.CommandText = $sql
$readAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$readSet = New-Object System.Data.DataSet
$readAdapter.SelectCommand = $SQLCommand
$readAdapter.Fill($readSet) |out-null
Foreach ($row in $readSet.Tables[0].rows) {$space_backup = $row[0]; $job = $row[1]}
Foreach ($row2 in $readSet.Tables[1].rows) {$space_free = $row2[0]}
Foreach ($row3 in $readSet.Tables[2].rows) {$space_all = $row3[0]}
Foreach ($row4 in $readSet.Tables[3].rows) {$reason = $row4[0]; $detail = $row4[1]}
$SQLConnection.Close()

if($reason -eq [System.DBNull]::Value)
{$reason = $Message}

if($space_backup -ge 1048576 -and $space_backup -lt 1073741824)
{$spacebackup = "{0:n2}" -f ($space_backup /1048576) + $mb}
elseif($space_backup -ge 1073741824 -and $space_backup -lt 1099511627776)
{$spacebackup = "{0:n2}" -f ($space_backup /1073741824) + $gb}
elseif($space_backup -ge 1099511627776)
{$spacebackup = "{0:n2}" -f ($space_backup /1099511627776) + $tb}
else
{$spacebackup = ("0" + $mb)}

if($space_free -ge 1048576 -and $space_free -lt 1073741824)
{$spacefree = "{0:n2}" -f ($space_free /1048576) + $mb}
elseif($space_free -ge 1073741824 -and $space_free -lt 1099511627776)
{$spacefree = "{0:n2}" -f ($space_free /1073741824) + $gb}
elseif($space_free -ge 1099511627776)
{$spacefree = "{0:n2}" -f ($space_free /1099511627776) + $tb}
else
{$spacefree = ("0" + $mb)}

if($space_all -ge 1048576 -and $space_all -lt 1073741824)
{$spaceall = "{0:n2}" -f ($space_all /1048576) + $mb}
elseif($space_all -ge 1073741824 -and $space_all -lt 1099511627776)
{$spaceall = "{0:n2}" -f ($space_all /1073741824) + $gb}
elseif($space_all -ge 1099511627776)
{$spaceall = "{0:n2}" -f ($space_all /1099511627776) + $tb}
else
{$spaceall = ("0" + $mb)}

# Determine the subject and background according to the result of the backup

if($Message.contains("Success"))
{$subject = "[Success] Endpoint Backup $hostname (VEB)"
   $Message2 = "EndpointBackup job '$job' finished with success."
   $bgcolor = "#00B050"}
elseif($Message.contains("Failed"))
{$subject = "[Failed] Endpoint Backup $hostname failed (VEB)"
   $Message2 = "EndpointBackup job '$job' finished with failed."
   $bgcolor = "#fb9895"}
elseif($Message.contains("Warning"))
{$subject = "[Warning] Endpoint Backup $hostname finished with Warning. (VEB)"   
   $Message2 = "EndpointBackup job '$job' finished with warning."
   $bgcolor = "#ffd96c"}

# Giving the typical Veeam look
$Body =

"<meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />
<table cellspacing=""0"" cellpadding=""0"" width=""50%"" border=""0"" style=""border-collapse: collapse;"">
<tr>
<td style=""padding: 0px;font-family: Tahoma;font-size: 12px;"">
<table cellspacing=""0"" cellpadding=""0"" width=""100%"" border=""0"" bordercolor=""#a7a9ac"" style=""border-collapse: collapse;"">
<tr>
<td colspan=""5"" style=""border: 1px solid #a7a9ac;background-color: $bgcolor;color: White;font-family: Tahoma;font-weight: bold;font-size: 16px;height: 70px;vertical-align: bottom;padding: 0 0 15px 15px;"">Backup job: $Hostname
<div style=""margin-top: 5px;font-size: 12px;"">$Message2</div>
</td></tr><tr>
<td colspan=""5"" style=""height: 35px;border: 1px solid #a7a9ac; background-color: #f3f4f4;font-size: 16px;vertical-align: middle;padding: 5px 0 0 15px;color: #626365; font-family: Tahoma;"">
<span>$date</span></td></tr>
<tr>
<td nowrap="""" style=""width:100px;padding: 2px 3px 2px 3px;vertical-align: top;border: 1px solid #a7a9ac;font-family: Tahoma;font-size: 12px;""><b>Start time </b></td>
<td nowrap="""" style=""width:150px;padding: 2px 3px 2px 3px;vertical-align: top;border: 1px solid #a7a9ac;font-family: Tahoma;font-size: 12px;"">$start</td>
<td nowrap="""" style=""width:150px;padding: 2px 3px 2px 3px;vertical-align: top;border: 1px solid #a7a9ac;font-family: Tahoma;font-size: 12px;""><b>Data size</b></td>
<td nowrap="""" style=""width:150px;padding: 2px 3px 2px 3px;vertical-align: top;border: 1px solid #a7a9ac;font-family: Tahoma;font-size: 12px;"">$spacebackup</td>
<td rowspan=""3"" style=""border: 1px solid #a7a9ac;""><span style=""font-size: 10px;"">&nbsp;</span></td>
</tr><tr>
<td nowrap="""" style=""width:100px;padding: 2px 3px 2px 3px;vertical-align: top;border: 1px solid #a7a9ac;font-family: Tahoma;font-size: 12px;""><b>End time</b></td>
<td nowrap="""" style=""width:150px;padding: 2px 3px 2px 3px;vertical-align: top;border: 1px solid #a7a9ac;font-family: Tahoma;font-size: 12px;"">$end</td>
<td nowrap="""" style=""width:150px;padding: 2px 3px 2px 3px;vertical-align: top;border: 1px solid #a7a9ac;font-family: Tahoma;font-size: 12px;""><b>Total backup size</b></td>
<td nowrap="""" style=""width:150px;padding: 2px 3px 2px 3px;vertical-align: top;border: 1px solid #a7a9ac;font-family: Tahoma;font-size: 12px;"">$spaceall</td>
</tr><tr>
<td nowrap="""" style=""width:100px;padding: 2px 3px 2px 3px;vertical-align: top;border: 1px solid #a7a9ac;font-family: Tahoma;font-size: 12px;""><b>Duration</b></td>
<td nowrap="""" style=""width:150px;padding: 2px 3px 2px 3px;vertical-align: top;border: 1px solid #a7a9ac;font-family: Tahoma;font-size: 12px;"">$duration</td>
<td nowrap="""" style=""width:150px;padding: 2px 3px 2px 3px;vertical-align: top;border: 1px solid #a7a9ac;font-family: Tahoma;font-size: 12px;""><b>Free disk space</b></td>
<td nowrap="""" style=""width:150px;padding: 2px 3px 2px 3px;vertical-align: top;border: 1px solid #a7a9ac;font-family: Tahoma;font-size: 12px;"">$spacefree</td>
</tr>
<tr><td colspan=""5"" style=""font-size:10px;padding: 2px 3px 2px 3px;border: 1px solid #a7a9ac;background-color:#f3f4f4;"">&nbsp;</td></tr>
<tr><td colspan=""5"" style=""font-size:12px;padding: 2px 3px 2px 3px;vertical-align: top;border: 1px solid #a7a9ac;font-family: Tahoma;"">$reason<br>$detail</td></tr>
</table></td></tr>
<tr><td >&nbsp;</td></tr><tr>
<td style=""font-size:12px;color:#626365;padding: 2px 3px 2px 3px;vertical-align: top;border: 1px solid #a7a9ac;font-family: Tahoma;"">Veeam Endpoint Backup</td>
</tr>
</table>"

$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $true
$emailMessage.Subject = $subject
$emailMessage.IsBodyHtml = $true
$emailMessage.Body = $Body

# Send the email
if ($InstanceID.contains("190")) {
   $SMTPClient.Send( $emailMessage )
} else {
   write-host "I don't want messages on 10010 and 10050 Restorepoint-creation or -remove Emails, skip those"
}


write-host "Script finished with -$instanceID- as the last event-ID"
JudgeFudge
Influencer
 
Posts: 15
Liked: 8 times
Joined: Thu Jan 07, 2016 4:54 pm
Location: Germany
Full Name: Chris

Re: Here it is: Powershell script to add VEB-EMails

Veeam Logoby best74 » Fri Feb 05, 2016 12:03 pm

Many thanks, i'll give it shot, and return with my results!
best74
Novice
 
Posts: 9
Liked: 1 time
Joined: Thu Jan 14, 2016 1:16 pm
Full Name: Brian Løfqvist Jensen

Re: Here it is: Powershell script to add VEB-EMails

Veeam Logoby saintdle » Mon Feb 29, 2016 9:57 am

Cheers for the time taken to do this and al your modifications, just set this up for a customer who has a single windows server, and use the script to alert on the backup status, as they wont be touching the server via a login and such to check the status themselves
Senior Engineer
VMCE
Various other stuff
Personal Technical Blog - www.educationalcentre.co.uk
saintdle
Veeam Vanguard
 
Posts: 65
Liked: 13 times
Joined: Tue Aug 05, 2014 1:13 pm
Full Name: Dean lewis

Re: Here it is: Powershell script to add VEB-EMails

Veeam Logoby zigzag » Wed Mar 02, 2016 10:04 am

Thanks for good working script. I used it on different machines and it works perfectly.

But now I have two machines, where I have trouble. If I start the script manually in the PS, i will get a lot of errors.
I checked the registry and I found the object... Any Ideas?

Thanks a lot

Example:
Code: Select all
Get-Item : Der Pfad "HKLM:\SOFTWARE\Veeam\Veeam Endpoint Backup" kann nicht gefunden werden, da er nicht vorhanden ist.
Bei C:\scripts\veeam.ps1:40 Zeichen:22
+     $User = (get-Item <<<<  $key).GetValue("SqlLogin")
    + CategoryInfo          : ObjectNotFound: (HKLM:\SOFTWARE\...Endpoint Backup:String) [Get-Item], ItemNotFoundExcep
   tion
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand

Sie können keine Methode für einen Ausdruck mit dem Wert NULL aufrufen.
Bei C:\scripts\veeam.ps1:40 Zeichen:37
+     $User = (get-Item $key).GetValue <<<< ("SqlLogin")
    + CategoryInfo          : InvalidOperation: (GetValue:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
zigzag
Novice
 
Posts: 4
Liked: never
Joined: Wed Mar 02, 2016 9:50 am

Re: Here it is: Powershell script to add VEB-EMails

Veeam Logoby JudgeFudge » Sun Mar 06, 2016 9:20 pm

Send you a PM looks more lika problem with VEB or .NET
JudgeFudge
Influencer
 
Posts: 15
Liked: 8 times
Joined: Thu Jan 07, 2016 4:54 pm
Location: Germany
Full Name: Chris

Re: Here it is: Powershell script to add VEB-EMails

Veeam Logoby Dima P. » Thu Mar 17, 2016 2:50 pm 2 people like this post

Hi guys,

Thanks to all of you for amazing workarounds! The wait is over: we have added email notifications in version 1.5. You can get the new version from our website. Once you test built-in notifications, don't forget to disable your scripts - otherwise you may be getting too many notifications regarding successful backups :mrgreen:
Dima P.
Veeam Software
 
Posts: 6497
Liked: 454 times
Joined: Mon Feb 04, 2013 2:07 pm
Location: SPb
Full Name: Dmitry Popov

Re: Here it is: Powershell script to add VEB-EMails

Veeam Logoby ftcnet » Fri Mar 18, 2016 5:09 am

Thanks for including this very helpful email result feature.

Image

.. where can we find info on these additional subject variables ? .. or are there only the three subject variables that are shown in the example?

It would be helpful if the test email actually included the resulting Subject line (using the variables) instead of just "Veeam Endpoint Backup".
ftcnet
Enthusiast
 
Posts: 25
Liked: 3 times
Joined: Fri May 15, 2015 1:59 am
Full Name: Bernard Klatt

Re: Here it is: Powershell script to add VEB-EMails

Veeam Logoby lxk3il » Fri Mar 18, 2016 9:01 am 1 person likes this post

ftcnet wrote:.. where can we find info on these additional subject variables ? .. or are there only the three subject variables that are shown in the example?

As you can see on your screenshot, pressing F1 opens the corresponding helppage, where you can find the currently available parameters:
  • %ComputerName%
  • %JobResult%
  • %CompletionTime%

If Veeam adds some more parameters, they will most liekly update this list.
lxk3il
Veeam ProPartner
 
Posts: 26
Liked: 6 times
Joined: Thu Sep 10, 2015 11:23 am
Full Name: Lukas

Re: Here it is: Powershell script to add VEB-EMails

Veeam Logoby Dima P. » Fri Mar 18, 2016 12:00 pm

Hi guys,

It seems the word ‘additional’ is a bit confusing – VEB has only three variables available (all described in the Help Center article). Let us know if you need some extra subject variables added but do not forget to share your personal use case :wink:
Dima P.
Veeam Software
 
Posts: 6497
Liked: 454 times
Joined: Mon Feb 04, 2013 2:07 pm
Location: SPb
Full Name: Dmitry Popov

Re: Here it is: Powershell script to add VEB-EMails

Veeam Logoby feelgoodeule » Thu Mar 24, 2016 10:06 pm 3 people like this post

Hello together,

first of all: thank you Veeam developers for making my script obsolete. It was very interesting what was developed out of the first idea and how many people contributed great thoughts. Thanks to everybody.

While it was pretty easy to get everything running at the office having an exchangeserver, I still had some difficulties to get it going at home without.

Here for those who search in short:

- create a googlemail or gmail-Account
- Open your Google account > Sign-in & security and ensure that "Allow less secure apps" is set to ON (as said in the VEB-help)
- in VEB-Email-settings: SMTP server settings: smtp.googlemail.com Port 587
- tick "use secure connection"
This worked for me. Hope for you too.

Michael
feelgoodeule
Influencer
 
Posts: 18
Liked: 14 times
Joined: Tue Jul 28, 2009 12:28 pm
Full Name: Michael

Share Backupscript

Veeam Logoby Invidia » Fri Apr 08, 2016 8:07 am

Hello together,

I wrote a script in the past that runs a "veeam endpoint backup free" job and sends me a e-mail if the backup was successful or not.
In the old version was no possibility to run the job just an week-days. So I used the windows integraded scheduled task programm to let it run from monday till friday.

So here is the script:
Code: Select all
### params ###
$from = "your.email@gmail.com"               
$to = "recipient@gail.com"
$smtpserver = "smtp.gmail.com"
$smtpport = 587
$user = "your.email@gmail.com"
$passwort = "yourpassword"
$date = get-date -format g
$hddname = [System.IO.DriveInfo]::GetDrives() | where {$_.Name -eq "D:\" } | select -ExpandProperty VolumeLabel
$LogFile = "C:\Server\backup.log"                  # phath + filename
### starts the backupjob ###
& "C:\Program Files\Veeam\Endpoint Backup\Veeam.EndPoint.Manager.exe" /backup
### evaluate returncode and set email text ###
if ($LASTEXITCODE -eq 0)
{
    $subject = $date + " Backup was successfull on Server XYZ"
    $body = $date + " Backup alert on Server XYZ. Backup was accomplished on disk " + $hddname + "."
}
elseif ($LASTEXITCODE -ne 0)
{
    $subject = $date + " Backup was unsuccessfull on Server XYZ"
    $body = $date + " Backup alert on Server XYZ. Backup was not accomplished."
}
### send e-mail ###
$smtp = New-Object System.Net.Mail.SmtpClient($smtpserver, $smtpport);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential  ($user,$passwort);
$smtp.Send($from, $to, $subject, $body);
### create logfile ###
### source: http://hope-this-helps.de/serendipity/archives/399-Funktion-um-Logfile-zu-schreiben-fuer-die-Powershell.html ###
function write_log ($content)
{
    $FileExists = Test-Path $LogFile
    $DateNow = Get-Date -Format "dd.MM.yyyy HH:mm"                      # evaluates the date with following syntax 01.10.2013 10:00
    $FileInp = $content                                               
    If ($FileExists -eq $True){                                       
        Add-Content $LogFile -value $FileInp                           
    } else {                       
       New-Item $Logfile -type file                                     
       Add-Content $LogFile -value $FileInp                             
    }
}
$LogInp = $date + " Backup was accomplished on disk " + $hddname + "."
write_log ($LogInp)                                                   

Invidia
Lurker
 
Posts: 1
Liked: never
Joined: Fri Apr 08, 2016 7:50 am
Full Name: Andre Coleman

Re: Share Backupscript

Veeam Logoby Mike Resseler » Fri Apr 08, 2016 10:36 am

Hi Andre,

Thanks for the script however, in version 1.5 you can let VEB send email and we implemented the functionality to backup on weekdays only, and even on the days of your choice so if you upgrade you won't need the script anymore ;-)
Mike Resseler
Veeam Software
 
Posts: 3345
Liked: 380 times
Joined: Fri Feb 08, 2013 3:08 pm
Location: Belgium, the land of the fries, the beer, the chocolate and the diamonds...
Full Name: Mike Resseler

PreviousNext

Return to Veeam Agent for Windows



Who is online

Users browsing this forum: No registered users and 25 guests