Standalone backup agent for Microsoft Windows servers and workstations (formerly Veeam Endpoint Backup FREE)
JaxIsland7575
Veteran
Posts: 391
Liked: 107 times
Joined: Apr 27, 2015 1:59 pm
Full Name: Ryan Jacksland
Location: NY, USA
Contact:

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

Post by JaxIsland7575 » 1 person likes this post

I have found the solution for me. When creating the task I used these options, any option not specifically listed was disabled or not selected:
Security Options:
-Run whether user is logged in or not
-Run with highest privileges

Triggers:
Basic Settings
-Log: Veeam Endpoint Backup
-Source: Veeam Endpoint Backup
-EventID: 190
-I used 190 because I didn't want email when it started, just an email on how it finished, by default you can leave this blank
-Stop task if it runs longer: 30 minutes
-Enabled

Actions:
Start a Program
Settings:
Program/Script: C:\windows\system32\windowspowershell\v1.0\powershell.exe
Add arugments (optional): C:\Veeam_Email\veeam_email.ps1 (this path is unique to where you put your file)

Conditions:
-Start the task only if the computer is on AC power
-Wake the computer to run this task

Settings:
-Allow task to be run on demand
-Stop the task if it runs longer than: 1 hour
-If the running task does not end when requested, force it to stop


Once I set it up with those selections it began to work.
VMCE v9
ominfo
Novice
Posts: 7
Liked: never
Joined: May 15, 2015 4:45 pm
Full Name: Olivier Martin
Contact:

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

Post by ominfo »

OK i will test it right now ! thank you
JaxIsland7575
Veteran
Posts: 391
Liked: 107 times
Joined: Apr 27, 2015 1:59 pm
Full Name: Ryan Jacksland
Location: NY, USA
Contact:

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

Post by JaxIsland7575 »

One thing to keep in mind is prior to setting up this task, I opened PowerShell as Administrator and ran: Set-ExecutionPolicy unrestricted

After that I setup the job and it worked.
VMCE v9
ominfo
Novice
Posts: 7
Liked: never
Joined: May 15, 2015 4:45 pm
Full Name: Olivier Martin
Contact:

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

Post by ominfo »

Yes thanks for the advice, already done :)
JaxIsland7575
Veteran
Posts: 391
Liked: 107 times
Joined: Apr 27, 2015 1:59 pm
Full Name: Ryan Jacksland
Location: NY, USA
Contact:

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

Post by JaxIsland7575 »

Were you able to get it running with the new settings?
VMCE v9
ominfo
Novice
Posts: 7
Liked: never
Joined: May 15, 2015 4:45 pm
Full Name: Olivier Martin
Contact:

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

Post by ominfo »

Case # 00917535

Work like a charm !! Thanks a lot !
Mike Resseler
Product Manager
Posts: 8191
Liked: 1322 times
Joined: Feb 08, 2013 3:08 pm
Full Name: Mike Resseler
Location: Belgium
Contact:

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

Post by Mike Resseler »

Oliver!

Sweet! Thanks for letting us know

Mike
starcom
Lurker
Posts: 1
Liked: never
Joined: May 31, 2015 4:40 pm
Full Name: Starcom IT
Contact:

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

Post by starcom »

Thanks for the script
if i want to send it via smtp.gmail.com where do i enter the email password?

TIA
Mike Resseler
Product Manager
Posts: 8191
Liked: 1322 times
Joined: Feb 08, 2013 3:08 pm
Full Name: Mike Resseler
Location: Belgium
Contact:

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

Post by Mike Resseler »

Starcom,

You can't. Task Scheduler does not support authentication for that job. However, you can use a program (for example SendEmail) and trigger a oneliner as a program with the authentication in

Mike
Dima P.
Product Manager
Posts: 14726
Liked: 1707 times
Joined: Feb 04, 2013 2:07 pm
Full Name: Dmitry Popov
Location: Prague
Contact:

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

Post by Dima P. »

if i want to send it via smtp.gmail.com where do i enter the email password?
I would say you don’t need to authenticate, since the SMTP server is used only to send email - no authentication is required (regularly, it’s just based on the domain name in the email address ). Of course some servers might block the unauthorized access but using the password would not solve it, only connecting from the proper IP address.
Allan20
Lurker
Posts: 1
Liked: never
Joined: Aug 24, 2015 7:29 am
Contact:

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

Post by Allan20 »

Hi

After the endpoint backup has run. The blue window for powershell pops up and then runs the script to send the email.. Is there anyway to make that window hidden?
SaschaH
Influencer
Posts: 12
Liked: 7 times
Joined: Aug 20, 2015 10:23 am
Full Name: Sascha Hoehne
Contact:

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

Post by SaschaH » 1 person likes this post

Hi Allan,

yes, but you have to use a little "VBScript-Wrapper". I found this a while ago:

Code: Select all

Dim objShell,objFSO,objFile
Set objShell=CreateObject("WScript.Shell")
Set objFSO=CreateObject("Scripting.FileSystemObject")

strPath="<path>\<yourscript>.ps1"

set objFile=objFSO.GetFile(strPath)
strCMD="powershell -nologo -command " & Chr(34) & "&{" &_
objFile.ShortPath & "}" & Chr(34)
objShell.Run strCMD,0
Be sure to edit strPath!

Sascha
chris.clark
Lurker
Posts: 1
Liked: never
Joined: Aug 26, 2015 11:21 am
Full Name: Chris Clark
Contact:

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

Post by chris.clark »

Hi
Thanks for script, however when it runs on my machine it bring up an error.

Line 2, Char 1 Error: Invalid Character Code: 800A0408 Source: Microsoft VBScript Compilation error

Sorry, I'm not a VB expert so not sure what to do.

Could you help

Thanks

Chris
UniSys
Influencer
Posts: 24
Liked: 5 times
Joined: Aug 12, 2015 6:34 am
Full Name: Nikolay
Contact:

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

Post by UniSys » 2 people like this post

Thanks for the script!

I use VEB to backup several computers so I prefer to have single mail showing the backup status for my all computers. I also use colors in my reports. Let me share my additions:

Code: Select all

###########################################################
# Edit this part:
$youremailserver=    "mail.my.lan"
$sender      =   "backup@my.lan"
$recipient   =   "admin@my.lan"
###########################################################

$Servernames = "srv01","srv02","srv03"
$Body =" <br> "

foreach ($Servername in $Servernames)

{
 
# init variables
$TimeGenerated = ""
$Source = ""
$EntryType = ""
$Message = ""
$InstanceID = ""

$TimeGenerated   =    get-eventlog -LogName "Veeam Endpoint Backup" -computername $Servername -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Format-List -property TimeGenerated | out-string
$Source      =   get-eventlog -LogName "Veeam Endpoint Backup" -computername $Servername -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Format-List -property Source | out-string
$EntryType   =   get-eventlog -LogName "Veeam Endpoint Backup" -computername $Servername  -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Format-List -property EntryType | out-string
$Message   =   get-eventlog -LogName "Veeam Endpoint Backup" -computername $Servername -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Format-List -property Message | out-string
$InstanceID   =   get-eventlog -LogName "Veeam Endpoint Backup" -computername $Servername -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Format-List -property InstanceID| out-string


# Add another line to message body according to the result of the backup:

if ($Message.contains("Success")) {
   $Body += "<font style = BACKGROUND-COLOR:green face=Arial color=white>$Message</font> <br>" 
   $Body += "<font face=Arial> $TimeGenerated   $InstanceID <br> <br> </font> "
} elseif ($InstanceID.contains("110")) {
   $Body += "<font style = BACKGROUND-COLOR:orange face=Arial color=white>$Message</font> <br>" 
   $Body += "<font face=Arial> $TimeGenerated $InstanceID <br> <br> </font> "
}   
   elseif ($InstanceID.contains("")) {
   $Body += "<font style = BACKGROUND-COLOR:red face=Arial color=white> $Servername : No messages logged </font> <br>" 
   $Body += "<font face=Arial> $TimeGenerated $InstanceID <br> <br> </font> "
}   
 else {   
   $Body += "<font style = BACKGROUND-COLOR:red face=Arial color=white>$Message</font> <br>" 
   $Body += "<font face=Arial> $TimeGenerated $InstanceID <br> <br> </font> " 
}
}


Send-MailMessage -To $recipient -Subject "Backup report" -From $sender -Body $Body -BodyAsHtml -SmtpServer $Youremailserver 

I am not Powershell scripting expert so my coding style is not perfect. :) But this script works fine for me.

We use VBR free on the same site and I would like to include VMs backup status in my report.
Dima P.
Product Manager
Posts: 14726
Liked: 1707 times
Joined: Feb 04, 2013 2:07 pm
Full Name: Dmitry Popov
Location: Prague
Contact:

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

Post by Dima P. »

Well done Nikolay! Thanks for sharing, may I call it a new edition of color coded email notification scrip for VEB :mrgreen:
UniSys
Influencer
Posts: 24
Liked: 5 times
Joined: Aug 12, 2015 6:34 am
Full Name: Nikolay
Contact:

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

Post by UniSys » 1 person likes this post

Dmitry,

The original idea and original script are not mine, Michael (feelgoodeule) posted it here. I have just shared my additions. So feel free to name it as you wish. :)
feelgoodeule
Influencer
Posts: 20
Liked: 19 times
Joined: Jul 28, 2009 12:28 pm
Full Name: Michael

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

Post by feelgoodeule » 1 person likes this post

Hello UniSys,

thank you for leaving the credits to me ;-)

I read your addition to my script. My first thought was, wow, that is smart idea to read the event-log of remote computers from a central one!

But: I trigger my script on the computer which is backed up by VEB, the trigger is an event on that computer (backup started, finished etc).
These triggers don't exist on the central computer which runs your script! How do you trigger the script instead?

I don't understand get this part.

Thanks for sharing, Michael
UniSys
Influencer
Posts: 24
Liked: 5 times
Joined: Aug 12, 2015 6:34 am
Full Name: Nikolay
Contact:

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

Post by UniSys »

Michael,

To read event log event on remote computer I use 'get-eventlog' command together with '-computername' key. This way you can start your script on one PC and let it connect to event log on another PC.
feelgoodeule
Influencer
Posts: 20
Liked: 19 times
Joined: Jul 28, 2009 12:28 pm
Full Name: Michael

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

Post by feelgoodeule »

Hello Unisys

Yes, I know the way you read the event log of the remote computer.
But again: How do you trigger the execution of the script on the central server?

Michael
UniSys
Influencer
Posts: 24
Liked: 5 times
Joined: Aug 12, 2015 6:34 am
Full Name: Nikolay
Contact:

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

Post by UniSys »

Michael,

My backup has to be done before 06:00. I start my script on central server via Task Scheduler at 6:15.

So start of the script is not VEB event-driven. Is it what you mean?

Sorry for many obvious details. I was not sure which part is unclear.
Dima P.
Product Manager
Posts: 14726
Liked: 1707 times
Joined: Feb 04, 2013 2:07 pm
Full Name: Dmitry Popov
Location: Prague
Contact:

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

Post by Dima P. »

Guys,
Mind me jump in and ask some question regarding your SMTP configurations :wink:

- Do you use in-house smtp or public mail hosting?
- Does it require authentication?
- Do you use the same email for FROM and TO addresses?

Thanks!
sibinsam
Veeam ProPartner
Posts: 64
Liked: 6 times
Joined: Nov 03, 2014 5:31 am
Full Name: Sibin Sam
Contact:

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

Post by sibinsam »

Dima P. wrote:Guys,
Mind me jump in and ask some question regarding your SMTP configurations :wink:

- Do you use in-house smtp or public mail hosting?
- Does it require authentication?
- Do you use the same email for FROM and TO addresses?

Thanks!
I use smtp server with no authentication.
The from and to addresses are different.
Dima P.
Product Manager
Posts: 14726
Liked: 1707 times
Joined: Feb 04, 2013 2:07 pm
Full Name: Dmitry Popov
Location: Prague
Contact:

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

Post by Dima P. »

Hey sibinsam,
smtp server with no authentication
And is that a public smtp server or private one?
JaxIsland7575
Veteran
Posts: 391
Liked: 107 times
Joined: Apr 27, 2015 1:59 pm
Full Name: Ryan Jacksland
Location: NY, USA
Contact:

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

Post by JaxIsland7575 »

Dima P. wrote:Guys,
Mind me jump in and ask some question regarding your SMTP configurations :wink:

- Do you use in-house smtp or public mail hosting?
- Does it require authentication?
- Do you use the same email for FROM and TO addresses?

Thanks!
- In-house smtp on exchange
- No authentication - private server
- I use different From and To addresses (I created a Veeam AD account that I use for running the services as well as email)

Cheers!
VMCE v9
sibinsam
Veeam ProPartner
Posts: 64
Liked: 6 times
Joined: Nov 03, 2014 5:31 am
Full Name: Sibin Sam
Contact:

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

Post by sibinsam »

Dima P. wrote:Hey sibinsam, And is that a public smtp server or private one?
Private smtp server
Dima P.
Product Manager
Posts: 14726
Liked: 1707 times
Joined: Feb 04, 2013 2:07 pm
Full Name: Dmitry Popov
Location: Prague
Contact:

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

Post by Dima P. »

Got it! Thanks Sam.
abelliniSIBA
Enthusiast
Posts: 32
Liked: 10 times
Joined: Nov 19, 2014 2:01 pm
Full Name: Alessandro Bellini
Location: Italy, Milan
Contact:

[MERGED] Veeam Endpoint Mail HTML Notification script

Post by abelliniSIBA » 3 people like this post

Hi guys,

i wrote a small Powershell script (MailEndpoint.ps1) to notify by mail Endpoint backup, adding a grafic table on body results, and write an html file.
just copy and paste this one in an empty *.ps1 file
than 3 suggestions:

1. Open Powershell and run : PS C:\> set-executionpolicy unrestricted;
2. For my Endpoint installations i add this script on Scheduled Tasks and running on a specified EventID "150" or "190";
3. To schedule this script you need to personalize some attribbute (SMTPServer, SMTPsender, SMTPreceiveraddress) and run it with 2 options (as the following example):

PS C:\>.\MailEndpoint.ps1 -SourceServer SERVERNAME -EmailLog (you can create a batch file with these arguments)

where SERVERNAME is the name of your machine. Verify to have the access on your local relay or smtp server

Code: Select all

# +----------------------------------------------------------------------------+
# |NAME: MailEndpoint.ps1                                                            |
# |AUTHOR: Alessandro                                                                 |
# |DATE: 22/09/2015                                                                    |
# |VERSION: 1.0                                                                            |
# |                                                                                               |
# |KEYWORDS:  Veeam, notification, backup, Eventlog, Endpoint    |
# |                                                                                               |
# |COMMENTS: mail the event success\not from windows Event log   |
# |                                                                                               |
# |PARAMETER :                                                                           |
# |    smtpserver use this to change the default smtp server             |
# |    from: you can specify a dif sender email address                   |
# |    to: same like from                                                                |
# | emailLog:If you whan to send the repport by a email; you need |
# |                smtpserver, from and to varibale                               |
# |    outpufile: if you prefere have file for the repport                   |
# |                                                                                               |
# |NEED : none                                                                             |
# +----------------------------------------------------------------------------+
# +----------------------------------------------------------------------------+
# | Maintenance History                                                                |
# | -------------------                                                                      |
# | Name                   Date       Version     Description                    |
# | ---------------------------------------------------------------------------+
# | Alessandro Bellini 22/09/2015 1.0  Endpoint version Notification |
# +----------------------------------------------------------------------------+
 
#paramter of the script fonction
Param(
       $SourceServer,
       [string]$SmtpServer = "SMTPServer",
       [string]$From = "SMTPSender",
       [string]$To = "SMTPreceiveraddress",
       [switch]$EmailLog,
       [string]$Outpufile ="VeamEndpointBackupReport.html"
)
#start
Begin
{
       $script:CurrentErrorActionPreference = $ErrorActionPreference
       $script:Output = @()
       $script:ProcessedServers = @()
       $ErrorActionPreference = "SilentlyContinue"
    $script:Subject = @()
    $script:TimeGeneratedJob = @()
    $script:MessageJob = @()
    $script:Status=1
    # table style for the Email
    $Table = @{Name="Server Name";expression={$_.servername}},@{Name="Time Events (last)";expression={$_.TimeGenerated}},@{Name="EntryType";expression={$_.EntryType}},@{Name="Source";expression={$_.Source}},@{Name="InstanceID";expression={$_.InstanceID}},@{Name="Message";expression={$_.Message}}
   
 
 
       If($EmailLog)
       {     
            
            
           Function SendEmailStatus($From, $To, $Subject, $SmtpServer, $BodyAsHtml, $Body)
           {  $SmtpMessage = New-Object System.Net.Mail.MailMessage $From, $To, $Subject, $Body
                        $SmtpMessage.IsBodyHTML = $BodyAsHtml
                        $SmtpClient = New-Object System.Net.Mail.SmtpClient $SmtpServer
                        $SmtpClient.Send($SmtpMessage)
                       If($? -eq $False){Write-Warning "$($Error[0].Exception.Message) | $($Error[0].Exception.GetBaseException().Message)"}
                        $SmtpMessage.Dispose()
                        rv SmtpClient
                        rv SmtpMessage
           }
    }
  
    Function GetEventlog
    {
    Param( $SServer
                 )
             # Put most info into the body of the email:
        $TimeGeneratedJob=(get-eventlog "Veeam Endpoint Backup" -InstanceId 190 -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Format-List -property TimeGenerated | out-string).substring(20)
        $MessageJob=(get-eventlog "Veeam Endpoint Backup" -InstanceId 190 -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Format-List -property Message| out-string).substring(14)
        $TimeGenerated=(get-eventlog "Veeam Endpoint Backup" -InstanceId 150, 110 -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Where {$_.message -match $SServer}| Format-List -property TimeGenerated | out-string).substring(20)
        $Source=(get-eventlog "Veeam Endpoint Backup" -InstanceId 150, 110 -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Where {$_.message -match $SServer} | Format-List -property Source | out-string).substring(13)
        $EntryType=(get-eventlog "Veeam Endpoint Backup" -InstanceId 150, 110 -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Where {$_.message -match $SServer} | Format-List -property EntryType | out-string).substring(16)
        $Message=(get-eventlog "Veeam Endpoint Backup" -InstanceId 150, 110 -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Where {$_.message -match $SServer} | Format-List -property Message| out-string).substring(14)
        $InstanceID=(get-eventlog "Veeam Endpoint Backup" -InstanceId 150, 110 -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Where {$_.message -match $SServer} | Format-List -property InstanceID| out-string).substring(17)
        #check if the value is empty frome the Evente log
        if(!$TimeGenerated)
        {$TimeGenerated ="no info"}
           if(!$Source)
        {$Source ="no info"}
           if(!$EntryType)
        {$EntryType ="no info"}
           if(!$Message)
        {$Message ="no info"}
           if(!$InstanceID)
        {$InstanceID ="no info"}
      
 #Created the PS object for store date in table. and # result screen     
    $Object = New-Object psobject
    $Object | Add-Member NoteProperty Servername $SServer -PassThru |Add-Member NoteProperty TimeGenerated $TimeGenerated -PassThru | Add-Member NoteProperty Source $Source -PassThru |
       Add-Member NoteProperty EntryType $EntryType -PassThru | Add-Member NoteProperty Message $MessageJob -PassThru | Add-Member NoteProperty InstanceID $InstanceID -PassThru
     
# Determine the subject according to the result of the backup:
      if ($MessageJob.contains("Success")) {
           $subject = "Veeam Endpoint Backup finished with Success."
            $script:Status = 1
        } else {  
                    if ($MessageJob.contains("no info")) {
                                        $subject = "Veeam Endpoint Backup finished with Warning!! Check Body for details."
                         $script:Status = 2
                                  }
                           else
                           {
                           $subject = "Veeam Endpoint Backup failed!! Check Body for details."
            $script:Status = 0
                           }
             }     
 
    if ($MessageJob.contains("Failed")) {
$subject = "Veeam Endpoint Backup Job failed, Check Body for details."
$script:Status = 0 }
             elseif ($MessageJob.contains("Warning")) {
             $subject = "Veeam Endpoint Backup Job finished with Warning Check Body for details."
            $script:Status = 2 
             }
    #Export values
    $script:Output += $Object 
    $script:subject = $subject
    $script:TimeGeneratedJob = $TimeGeneratedJob
    $script:MessageJob = $Message
      
}
}
#process of the script
Process
{ 
GetEventlog $SourceServer

       #output message resulte
Write-Host "Script finished with : " $script:subject
write-host "Job Message : " $script:MessageJob
write-host "Finished Time Job at : "  $script:TimeGeneratedJob  
}
# the end process task
End
{     
# created the header og the email
 
#definir the color
if($Status -eq 1){
$colorTagTable = "background-color:#7CFC00"
$messageinfo = "Success"
}
elseif ($Status -eq 2) {
$colorTagTable = "background-color:#FFD700"
$messageinfo = "Warning"
}
else{
$colorTagTable = "background-color:#B22222"
$messageinfo = "Error"
}
 
# the header
$htmlbody = ""
$date= Get-Date
$header = @"
 
<table cellspacing="0" cellpadding="0" width="100%" border="0">
 
     <tr>
         <td style="width: 80%;border: none;$colorTagTable;" ><H2>Veeam Endpoint Backup</H2>
       <div>Finished Time Job at $script:TimeGeneratedJob</div></br>     
             <div>Message : $Messagejob</div>
             <div>Created by $env:username at $date.</div>
         </td>
          <td style="border: none;$colorTagTable;" >
             finished with: <b>$messageinfo</b>
        </td>
    </tr>
</table>
"@
#merge body
$htmlbody += $header
#Formating the result in HTML format converting for google mail
$Body = $Output | Select $Table | ConvertTo-HTML -Body $htmlbody
$Body = $Body | AlternatingRows
$Body = $Body -replace '<body>', '<body style = "font-size:12px;font-family:Arial;color:#000000;font-weight:normal">'
$Body = $Body -replace '<table>', '<table style = "border-width:1px;cellpadding=10;border-style:solid;border-collapse:collapse;width:100%">'
$Body = $Body -replace '<th>', '<th style = "font-size:12px;border-width:1px;padding:10px;border-style:solid;background-color:#96C5EC">'
$Body = $Body -replace '<td>', '<td style = "font-size:10px;border-width:1px;padding:10px;border-style:solid">'

Add-Type -AssemblyName System.Web
$Body = [System.Web.HttpUtility]::HtmlDecode($Body) # this line made the link clickable
 
#merge body
$htmlbody +=$header
#fonction to sending the email
If($EmailLog){
SendEmailStatus -From $From -To $To -Subject $script:Subject -SmtpServer $SmtpServer -BodyAsHtml $true -Body $Body
}

#or in a outpute file
if($Outpufile){
$Body | Out-File $OutpufileHtml

}
       $ErrorActionPreference = $script:CurrentErrorActionPreference
}
Hope it could helps you :wink:
Cheers from sunny Italy
Alessandro
Veolia
Dima P.
Product Manager
Posts: 14726
Liked: 1707 times
Joined: Feb 04, 2013 2:07 pm
Full Name: Dmitry Popov
Location: Prague
Contact:

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

Post by Dima P. » 1 person likes this post

Hello Alessandro,
Some folks have already proposed their own scripts, so I merged yours to the existing discussions. Thanks for sharing your approach with the community – it’s highly appreciated!

Cheers from cold and rainy Russia :mrgreen:
abelliniSIBA
Enthusiast
Posts: 32
Liked: 10 times
Joined: Nov 19, 2014 2:01 pm
Full Name: Alessandro Bellini
Location: Italy, Milan
Contact:

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

Post by abelliniSIBA »

Thanks Dima!
That's why i like that interesting community!
Veolia
vpjax
Lurker
Posts: 1
Liked: 1 time
Joined: Oct 23, 2015 2:12 pm
Full Name: Josh Lee
Contact:

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

Post by vpjax » 1 person likes this post

I have expanded on your powershell script to actually grab data from the Veeam DB itself. It is nice to know a little more than the backup completed.

Also note you can use an application such as DeltaCopy to sync these backups to another server or in the cloud. You can hang this script off the event log in task scheduler and then start the rsync sync at the end of this script based on failed or success.


The 2 important added pieces of code are here:

It goes in and queries Veeam's Local DB. It grabs the correct username and password from the Registry and Querys the Report View.

Code: Select all

 
$key = "hklm:\SOFTWARE\Veeam\Veeam Endpoint Backup"
    $TableDetail = Invoke-Sqlcmd -Username (Get-ItemProperty -Path $key -Name SqlLogin).SqlLogin -Password (Get-ItemProperty -Path $key -Name SqlPassword).SqlPassword -ServerInstance (Get-ItemProperty -Path $key -Name SqlInstancePipeName).SqlInstancePipeName -Query "SELECT Top 10 [job_name] as [Job Name],[creation_time] as [Started],[end_time] as [Ended],[is_full] as [Full Backup],[total_size] / 1073741824 as [Total Size GB], [total_used_size] / 1073741824 as [Total Used Size GB], [processed_used_size] / 1073741824 as [Total Processed Used GB],[stored_size] / 1048576 as [Total Stored Size MB],[progress] as [Progress],[is_retry] as [Retry?],[avg_speed] / 1048576 as [Speed MB/min]  FROM [VeeamBackup].[dbo].[ReportSessionView] ORDER BY [creation_time] DESC" |  Select-Object -Property * -ExcludeProperty ItemArray, Table, HasErrors, RowState, RowError  | ConvertTo-HTML
Full Scripts with other additions and changes below:

Code: Select all

# +----------------------------------------------------------------------------+
# |NAME: MailEndpoint.ps1                                                      |
# |AUTHOR: Alessandro                                                          |
# |DATE: 22/09/2015                                                            |
# |VERSION: 1.1                                                                |
# |                                                                            |
# |KEYWORDS:  Veeam, notification, backup, Eventlog, Endpoint    	       |
# |                                                                            |
# |COMMENTS: mail the event success\not from windows Event log   	       |
# |                                                                            |
# |PARAMETER :                                                                 |
# |    smtpserver use this to change the default smtp server                   |
# |    from: you can specify a dif sender email address                        |
# |    to: same like from                                                      |
# |    emailLog:If you whan to send the repport by a email; you need 	       |
# |                smtpserver, from and to varibale                            |
# |    outpufile: if you prefere have file for the repport                     |
# |    SourceClient: Shows in email report                                     |
# +----------------------------------------------------------------------------+
# +----------------------------------------------------------------------------+
# | Maintenance History                                                        |
# | -------------------                                                        |
# | Name                   Date       Version     Description                  |
# | ---------------------------------------------------------------------------+
# | Alessandro Bellini 22/09/2015 1.0  Endpoint version Notification           |
# | Joshua Lee         23/10/2015 1.1  Added Direct DB QUERY for Backup Stats  |
# | 				        Added Option SourceClient which adds   |
# |					client name to emailed report. Also    |
# |					changed email subject and added Server |
# |					Name.				       |
# +----------------------------------------------------------------------------+


 
#paramter of the script fonction
Param(
       $SourceServer,
       [string]$SmtpServer = "SMTPServer",
       [string]$From = "SMTPSender",
       [string]$To = "SMTPreceiveraddress",
       [switch]$EmailLog,
       $SourceClient,
       [string]$Outpufile ="VeamEndpointBackupReport.html"
)
#start
Begin
{
       $script:CurrentErrorActionPreference = $ErrorActionPreference
       $script:Output = @()
       $script:ProcessedServers = @()
       $ErrorActionPreference = "SilentlyContinue"
    $script:Subject = @()
    $script:TimeGeneratedJob = @()
    $script:MessageJob = @()
    $script:Status=1
    # table style for the Email
    $Table = @{Name="Server Name";expression={$_.servername}},@{Name="Time Events (last)";expression={$_.TimeGenerated}},@{Name="EntryType";expression={$_.EntryType}},@{Name="Source";expression={$_.Source}},@{Name="InstanceID";expression={$_.InstanceID}},@{Name="Message";expression={$_.Message}}
	
    $key = "hklm:\SOFTWARE\Veeam\Veeam Endpoint Backup"
    $TableDetail = Invoke-Sqlcmd -Username (Get-ItemProperty -Path $key -Name SqlLogin).SqlLogin -Password (Get-ItemProperty -Path $key -Name SqlPassword).SqlPassword -ServerInstance (Get-ItemProperty -Path $key -Name SqlInstancePipeName).SqlInstancePipeName -Query "SELECT Top 10 [job_name] as [Job Name],[creation_time] as [Started],[end_time] as [Ended],[is_full] as [Full Backup],[total_size] / 1073741824 as [Total Size GB], [total_used_size] / 1073741824 as [Total Used Size GB], [processed_used_size] / 1073741824 as [Total Processed Used GB],[stored_size] / 1048576 as [Total Stored Size MB],[progress] as [Progress],[is_retry] as [Retry?],[avg_speed] / 1048576 as [Speed MB/min]  FROM [VeeamBackup].[dbo].[ReportSessionView] ORDER BY [creation_time] DESC" |  Select-Object -Property * -ExcludeProperty ItemArray, Table, HasErrors, RowState, RowError  | ConvertTo-HTML

     If($EmailLog)
       {     
            
            
           Function SendEmailStatus($From, $To, $Subject, $SmtpServer, $BodyAsHtml, $Body)
           {  $SmtpMessage = New-Object System.Net.Mail.MailMessage $From, $To, $Subject, $Body
                        $SmtpMessage.IsBodyHTML = $BodyAsHtml
                        $SmtpClient = New-Object System.Net.Mail.SmtpClient $SmtpServer
                        $SmtpClient.Send($SmtpMessage)
                       If($? -eq $False){Write-Warning "$($Error[0].Exception.Message) | $($Error[0].Exception.GetBaseException().Message)"}
                        $SmtpMessage.Dispose()
                        rv SmtpClient
                        rv SmtpMessage
           }
    }
  
    Function GetEventlog
    {
    Param( $SServer
                 )
             # Put most info into the body of the email:
        $TimeGeneratedJob=(get-eventlog "Veeam Endpoint Backup" -InstanceId 190 -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Format-List -property TimeGenerated | out-string).substring(20)
        $MessageJob=(get-eventlog "Veeam Endpoint Backup" -InstanceId 190 -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Format-List -property Message| out-string).substring(14)
        $TimeGenerated=(get-eventlog "Veeam Endpoint Backup" -InstanceId 150, 110 -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Where {$_.message -match $SServer}| Format-List -property TimeGenerated | out-string).substring(20)
        $Source=(get-eventlog "Veeam Endpoint Backup" -InstanceId 150, 110 -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Where {$_.message -match $SServer} | Format-List -property Source | out-string).substring(13)
        $EntryType=(get-eventlog "Veeam Endpoint Backup" -InstanceId 150, 110 -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Where {$_.message -match $SServer} | Format-List -property EntryType | out-string).substring(16)
        $Message=(get-eventlog "Veeam Endpoint Backup" -InstanceId 150, 110 -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Where {$_.message -match $SServer} | Format-List -property Message| out-string).substring(14)
        $InstanceID=(get-eventlog "Veeam Endpoint Backup" -InstanceId 150, 110 -newest 1 -entrytype Information, Warning, Error -source "Veeam Endpoint Backup" | Where {$_.message -match $SServer} | Format-List -property InstanceID| out-string).substring(17)
        #check if the value is empty frome the Evente log
        if(!$TimeGenerated)
        {$TimeGenerated ="no info"}
           if(!$Source)
        {$Source ="no info"}
           if(!$EntryType)
        {$EntryType ="no info"}
           if(!$Message)
        {$Message ="no info"}
           if(!$InstanceID)
        {$InstanceID ="no info"}
      
 #Created the PS object for store date in table. and # result screen     
    $Object = New-Object psobject
    $Object | Add-Member NoteProperty Servername $SServer -PassThru |Add-Member NoteProperty TimeGenerated $TimeGenerated -PassThru | Add-Member NoteProperty Source $Source -PassThru |
       Add-Member NoteProperty EntryType $EntryType -PassThru | Add-Member NoteProperty Message $MessageJob -PassThru | Add-Member NoteProperty InstanceID $InstanceID -PassThru
     
# Determine the subject according to the result of the backup:
      if ($MessageJob.contains("Success")) {
           $subject = "Veeam Endpoint Backup finished with Success on " + $SServer
            $script:Status = 1
        } else {  
                    if ($MessageJob.contains("no info")) {
                                        $subject = "Veeam Endpoint Backup finished with Warning on " + $SServer + " Check Body for details."
                         $script:Status = 2
                                  }
                           else
                           {
                           $subject = "Veeam Endpoint Backup failed on " + $SServer + "!! Check Body for details."
            $script:Status = 0
                           }
             }     
 
    if ($MessageJob.contains("Failed")) {
$subject = "Veeam Endpoint Backup Job failed on " + $SServer + ", Check Body for details."
$script:Status = 0 }
             elseif ($MessageJob.contains("Warning")) {
             $subject = "Veeam Endpoint Backup Job finished with Warning on " + $SServer + " Check Body for details."
            $script:Status = 2 
             }
    #Export values
    $script:Output += $Object 
    $script:subject = $subject
    $script:TimeGeneratedJob = $TimeGeneratedJob
    $script:MessageJob = $Message
      
}
}
#process of the script
Process
{ 
GetEventlog $SourceServer

       #output message resulte
Write-Host "Script finished with : " $script:subject
write-host "Job Message : " $script:MessageJob
write-host "Finished Time Job at : "  $script:TimeGeneratedJob  
}
# the end process task
End
{     
# created the header og the email
 
#definir the color
if($Status -eq 1){
$colorTagTable = "background-color:#7CFC00"
$messageinfo = "Success"
}
elseif ($Status -eq 2) {
$colorTagTable = "background-color:#FFD700"
$messageinfo = "Warning"
}
else{
$colorTagTable = "background-color:#B22222"
$messageinfo = "Error"
}
 
# the header
$htmlbody = ""
$date= Get-Date
$header = @"
 
<table cellspacing="0" cellpadding="0" width="100%" border="0">
 
     <tr>
         <td style="width: 80%;border: none;$colorTagTable;padding:10px;" ><H2>Veeam Endpoint Backup - $SourceClient</H2>
       <div><b>Job Finished at:</b> $script:TimeGeneratedJob</div></br>     
             <div><b>Message:</b> $Messagejob</div>
             <div><b>Client Site:</b> $SourceClient 
		  <b>Emailed at:</b> $date. <BR />
		  <b>Note: </b> ! Check SYNC LOG ! Additional email to Follow.</div>
         </td>
          <td style="border: none;$colorTagTable;padding:10px;" >
             Finished with: <b>$messageinfo</b>
        </td>
    </tr>
</table>
"@

#merge body
$htmlbody += $header
#Formating the result in HTML format converting for mail
$Body = $Output | Select $Table | ConvertTo-HTML -Body $htmlbody
$Body = $Body | AlternatingRows
$Body = $Body -replace '<body>', '<body style = "font-size:12px;font-family:Arial;color:#000000;font-weight:normal">'
$Body = $Body -replace '<table>', '<table style = "border-width:1px;cellpadding=10;border-style:solid;border-collapse:collapse;width:100%">'
$Body = $Body -replace '<th>', '<th style = "font-size:12px;border-width:1px;padding:10px;border-style:solid;background-color:#96C5EC">'
$Body = $Body -replace '<td>', '<td style = "font-size:10px;border-width:1px;padding:10px;border-style:solid">'



#Formating the result in HTML format converting for mail
$Body2 = $TableDetail
$Body2 = $Body2 | AlternatingRows
$Body2 = $Body2 -replace '<body>', '<body style = "font-size:12px;font-family:Arial;color:#000000;font-weight:normal">'
$Body2 = $Body2 -replace '<table>', '<table style = "border-width:1px;cellpadding=10;border-style:solid;border-collapse:collapse;width:100%">'
$Body2 = $Body2 -replace '<th>', '<th style = "font-size:12px;border-width:1px;padding:10px;border-style:solid;background-color:#96C5EC">'
$Body2 = $Body2 -replace '<td>', '<td style = "font-size:10px;border-width:1px;padding:10px;border-style:solid">'

$Body += "<div style=''><BR /> <b>Backup Details:</b><BR></div>" + $Body2

Add-Type -AssemblyName System.Web
$Body += "<div style='font-size:10px;'>Managed by: Venture Pointe, Inc.</div>"


$Body = [System.Web.HttpUtility]::HtmlDecode($Body) # this line made the link clickable
 
#merge body
$htmlbody +=$header

#function to sending the email
If($EmailLog){
SendEmailStatus -From $From -To $To -Subject $script:Subject -SmtpServer $SmtpServer -BodyAsHtml $true -Body $Body
}

#or in a outpute file
if($Outpufile){
$Body | Out-File $OutpufileHtml

}
       $ErrorActionPreference = $script:CurrentErrorActionPreference
}
Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests