PowerShell Veeam backup status report

PowerShell script exchange

PowerShell Veeam backup status report

Veeam Logoby pizzim13 » Mon Oct 03, 2011 9:24 pm 1 person likes this post

I wrote the following script a while back to get a single email, once every 24 hours with the status of any job who's last state was in error or warning. For the amount of Veeam servers and backup jobs I have running, getting individual emails for every job (success, failure, or warning) and it's retries was just too much. And the email that can be sent from Enterprise Manager doesn't have enough detail. This script has a lot moving parts but hopefully it can help someone out.

The following is an example of the report's output:
Image
The "msgs" have been truncated for the screenshot.

The following code was written to support multiple Veeam servers, not all code is supported by Veeam, uses a config.xml file, uses Powershell SecureStrings to encrypt passwords (http://technet.microsoft.com/en-us/maga ... 14574.aspx), uses PowerShell Remoting (https://blogs.technet.com/b/heyscriptin ... mands.aspx), runs as a scheduled task, if I had to rewrite this today I would probably change some things but it works :) , and lastly use at your own risk:

Code: Select all
$ErrorActionPreference = "stop"

#html styling
$style = "<style>"
$style = $style + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$style = $style + "TH{border-width: 1px;padding: 1px;border-style:double;border-color: black;}"
$style = $style + "TD{border-width: 1px;padding: 5px;border-style:double;border-color: black;}"
$style = $style + "</style>"

#For debugging
#Get-PSSession | Remove-PSSession

#Reading config file
$ScriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition
$Config = [xml](Get-Content "$ScriptPath\config.xml")

###Create a new PSSession to each server in $VeeamSrvs and assign to a variable
$VeeamCreds = New-Object System.Management.Automation.PSCredential $Config.AppSettings.Creds.veeam.username, ($Config.AppSettings.Creds.veeam.EncryptedPass | ConvertTo-SecureString)

$VeeamSrvSessions = New-Object System.Collections.ArrayList
foreach ($VeeamSrv in $Config.AppSettings.VeeamSrvs.HostName)
   {
   $VeeamSrvSessions.add((New-PSSession -ComputerName $VeeamSrv -Credential $VeeamCreds)) | out-null
   }
###

###Connect to each Veeam Server and add the Veeam Snapin
foreach ($VeeamSrvSession in $VeeamSrvSessions)
   {
   Invoke-Command -session $VeeamSrvSession -ScriptBlock `
      {
      #Add Veeam snapin if needed
      if ( (Get-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue) -eq $null )
         {
         Add-PsSnapin -Name VeeamPSSnapIn
         }
      } -AsJob
   }
Get-Job | Wait-Job
###

###Connect to all Veeam servers and return status on jobs that have warnings or failures and have completed within the last day
foreach ($VeeamSrvSession in $VeeamSrvSessions)
   {
    Invoke-Command -session $VeeamSrvSession -ScriptBlock `
      {$JobStatus = (Get-VBRJob | Where-Object {$_.GetLastResult() -ne "Success" -and $_.GetLastResult() -ne "None" -and $_.findlastsession().progress.stoptime -ge (Get-Date).addhours(-24)}) |
      ForEach-Object {$_ | Select-object  @{Name="Job";Expression={$_.Name}}, @{Name="Status";Expression={$_.GetLastResult()}}, `
         @{Name="ID";Expression={$_.id}}, @{Name="Job Msg";Expression={$_.FindLastSession().info.description}}, @{Name="Session";Expression={$_.FindLastSession()}}}
      
      if ($JobStatus -ne $null)
         {
         #Adding new properties to array
         $JobStatus | ForEach-Object {$_ | Add-Member -MemberType NoteProperty "Server" -Value $Env:COMPUTERNAME}
         $JobStatus | ForEach-Object {$_ | Add-Member -MemberType NoteProperty "VM Msg" -Value $null}
         
         #Collecting VM failure info
         foreach ($JobStat in $JobStatus)
            {
            #Not supported by Veeam
            $Info = [Veeam.Backup.Core.CBackupTaskSession]::GetByJobSession(($JobStat.Session | foreach-object {$_.info}).id)
            $JobStat."VM Msg" = ($info | ForEach-Object {$_} | Where-Object {$_.status -ne "Success"}) |
            Foreach-object {$_ | Select-Object @{Name="VM";Expression={$_.ObjectName}}, @{Name="Reason";Expression={$_.Reason}} | Format-List | Out-String}
            }
         $JobStatus
         }
      } -AsJob
   }
   
Get-Job | Wait-Job
$JobStatus = Get-Job | Receive-Job
###

#Closing open PS sessions
Get-PSSession | Remove-PSSession

###Sending report
#Mail settings
$MailSrv = $Config.AppSettings.Email.SRV
$MailFrom = $Config.AppSettings.Email.From
$MailTo = $Config.AppSettings.Email.To
$MailSbjt = "Veeam Job Status Report $(get-date)"
$MailBody = ($JobStatus | Select-Object Job, Status, "Job Msg", "VM Msg", Server | Sort-Object Status -Descending |   ConvertTo-Html -Head $style | Out-String) `
         -replace ("VM     :","<p></p><b>VM:</b>") `
         -replace ("Reason :","<p></p><b>Reason:</b>") `
         -replace ("<td>Failed","<td bgcolor=red>Failed")

Send-MailMessage -SmtpServer $MailSrv -From $MailFrom -To $MailTo -Subject $MailSbjt -Body $MailBody -BodyAsHtml
###


The following is the config file. Fill in the blanks with your info and save the file as config.xml in the same directory as your powershell script. The account you use the authenticate to your Veeam servers must be admins and be a dbo to your Veeam database.
Code: Select all
<!-- file.xml -->
<AppSettings>
   <VeeamSrvs>
      <HostName>veeam-srv1.domain.com</HostName>
      <HostName>veeam-srv2.domain.com</HostName>
      <HostName>veeam-srv3.domain.com</HostName>
   </VeeamSrvs>
   <Creds>
      <Veeam>
         <Username>user@domain.com</Username>
         <EncryptedPass></EncryptedPass>
      </Veeam>
   </Creds>
   <Email>
      <SRV>veeam.domain.com</SRV>
      <From>veeam-report@domain.com</From>
      <To>user@domain.com</To>
   </Email>   
</AppSettings>
pizzim13
Enthusiast
 
Posts: 86
Liked: 6 times
Joined: Thu Apr 21, 2011 7:37 pm

Re: PowerShell Veeam backup status report

Veeam Logoby Gostev » Mon Oct 03, 2011 9:43 pm

Nice!
Gostev
Veeam Software
 
Posts: 21390
Liked: 2349 times
Joined: Sun Jan 01, 2006 1:01 am
Location: Baar, Switzerland

Re: PowerShell Veeam backup status report

Veeam Logoby Sethbartlett » Fri Oct 07, 2011 2:35 pm

NICE PETER!!!
Skype: Sethbartlett88 - Make sure to label who you are and why you want to add me ;)
Twitter: @sethbartlett
If my post was helpful, please like it. Sometimes twitter is quicker to hit me up if you need me.
Sethbartlett
Expert
 
Posts: 282
Liked: 25 times
Joined: Wed Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett

Re: PowerShell Veeam backup status report

Veeam Logoby FkB|Stan » Wed Nov 02, 2011 4:59 pm

Hello,

Thx for your script. It seems pretty good.

I still receive an issue when I start it.
http://imageshack.us/photo/my-images/577/unledghwu.png
(Sorry it's in french. To summarize : Method call failed because [Veeam.Backup.DBManager.CDBJob] doenst have a method named : GetLastResult)

Do you have any idea why ?
I saw in some forum they use GetLastResult -nq. What's the difference between -ne and -nq?

Thx
FkB|Stan
Novice
 
Posts: 4
Liked: never
Joined: Wed Nov 02, 2011 4:47 pm
Full Name: FkB|Stan

Re: PowerShell Veeam backup status report

Veeam Logoby FkB|Stan » Thu Nov 03, 2011 10:39 am

I think I found my issue.
I wasnt using Veeam Backup Version 5.0.

Actually :
- Windows Server 2003 SP2 with Powershell v2.0 installed
- Veeam Backup & Replication 5.0 with Powershell KIT
- Powershell Remoting Works (Invoke command working)
- PSSession Works
- PSSnapin Works.


But when I start the script I receive this message :
AVERTISSEMENT : You should update your PowerShell to PowerShell 2.0 version.
(I checked Powershell version on each server, and it's 2.0)

I receive a mail, but it's empty.

I really need help to make this wokring.

Waiting for an answer.
FkB|Stan
Novice
 
Posts: 4
Liked: never
Joined: Wed Nov 02, 2011 4:47 pm
Full Name: FkB|Stan

Re: PowerShell Veeam backup status report

Veeam Logoby pizzim13 » Sat Nov 05, 2011 4:20 am

The path of least resistance would be to test the individual parts of the script on your local veeam server. This way you won't have the extra variable of powershell remoting to troubleshoot. One thing to check is the permissions on the account you are using for ps remoting has on your veeam database. It needs to have administrative rights.

"You should update your PowerShell to PowerShell 2.0 version." pay that warning no mind. It comes up in my environment also.

Do you have multiple veeam servers? If not, rip out the ps remoting bits and run the script locally on your veeam server.
pizzim13
Enthusiast
 
Posts: 86
Liked: 6 times
Joined: Thu Apr 21, 2011 7:37 pm

Re: PowerShell Veeam backup status report

Veeam Logoby ThomasMc » Sun Nov 06, 2011 2:35 pm

You should update your PowerShell to PowerShell 2.0 version.

I wonder if Veeam are checking the PS version with $host.version somewhere and throwing this warning.
ThomasMc
Expert
 
Posts: 293
Liked: 17 times
Joined: Wed Apr 13, 2011 12:45 pm
Full Name: Thomas McConnell

Re: PowerShell Veeam backup status report

Veeam Logoby FkB|Stan » Mon Nov 07, 2011 9:35 am

pizzim13 wrote:The path of least resistance would be to test the individual parts of the script on your local veeam server. This way you won't have the extra variable of powershell remoting to troubleshoot. One thing to check is the permissions on the account you are using for ps remoting has on your veeam database. It needs to have administrative rights.

"You should update your PowerShell to PowerShell 2.0 version." pay that warning no mind. It comes up in my environment also.

Do you have multiple veeam servers? If not, rip out the ps remoting bits and run the script locally on your veeam server.


Ok i'll check Administrative rights.

I tried his scripts on local, remote, different kind of server.
It looks working only on Veeam 5.0, because there are different command btw 4.0 and 5.0 and I still receive an empty email.
I have too much server with different installation (Powershell V1/V2, Veeam 4.0/5.0, PowerShell toolkit), so this script isnt great for me.

However, I started my own script to check Veeam Backup which should working on all kind of Veeam.
Actually, it's working well.

On Veeam 4.0 i'm using FindLastJobSession method to get information.
How Can I get Reason when a Backup Failed?! Or Vm Message?
Which Method should I call?

Thx for your help and answer btw.
FkB|Stan
Novice
 
Posts: 4
Liked: never
Joined: Wed Nov 02, 2011 4:47 pm
Full Name: FkB|Stan

Re: PowerShell Veeam backup status report

Veeam Logoby Sethbartlett » Mon Nov 07, 2011 1:26 pm

I am not sure what the "Reason" call is for version 4. The method Peter is calling is technically unsupported and had to be grabbed through the DLLs/Code directly. It is a .NET call more-so than powershell, so the difference between 4 and 5 I am not sure about.
Skype: Sethbartlett88 - Make sure to label who you are and why you want to add me ;)
Twitter: @sethbartlett
If my post was helpful, please like it. Sometimes twitter is quicker to hit me up if you need me.
Sethbartlett
Expert
 
Posts: 282
Liked: 25 times
Joined: Wed Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett

Re: PowerShell Veeam backup status report

Veeam Logoby ThomasMc » Mon Nov 07, 2011 4:00 pm

You can't use remoting on PS v1
ThomasMc
Expert
 
Posts: 293
Liked: 17 times
Joined: Wed Apr 13, 2011 12:45 pm
Full Name: Thomas McConnell

Re: PowerShell Veeam backup status report

Veeam Logoby FkB|Stan » Thu Nov 17, 2011 2:42 pm

Thx for your answers.

I create my own script to test. And I finaly succeeded.
Method between version 4.x and version 5.x are totally different.
So I made a script for version 4 and another one for version 5.

Actually, I can report Veeam Backup Job on a nagios/centreon server + I can receive a mail.

It took me alot of time, but finaly, it's working :d
I'm so happy.


VERSION 4 Exemple for STATUS :
- For a specific job : (Get-VBRJOB $JOBNAME).FindLastJobSession().Status
- For All Job : Get-VBRJob | Select-Object @{Name="Job";Expression={$_.Name}} , @{Name="Status";Expression={$_.LatestStatus}}

VERSION 5 Exemple for STATUS :
- For All Job : Get-VBRJob | Select-Object @{Name="Status";Expression={$_.GetLastResult()}}
FkB|Stan
Novice
 
Posts: 4
Liked: never
Joined: Wed Nov 02, 2011 4:47 pm
Full Name: FkB|Stan

Re: PowerShell Veeam backup status report

Veeam Logoby ekranz » Mon Jan 23, 2012 10:29 pm

Any chance this has been updated to work with V6?
ekranz
Novice
 
Posts: 7
Liked: never
Joined: Thu Jun 23, 2011 1:47 pm
Full Name: Ed Kranz

Re: PowerShell Veeam backup status report

Veeam Logoby Sethbartlett » Tue Jan 24, 2012 5:47 am

At a quick glance, this looks like it would work with v6. Are you having issues running it in v6?
Skype: Sethbartlett88 - Make sure to label who you are and why you want to add me ;)
Twitter: @sethbartlett
If my post was helpful, please like it. Sometimes twitter is quicker to hit me up if you need me.
Sethbartlett
Expert
 
Posts: 282
Liked: 25 times
Joined: Wed Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett

Re: PowerShell Veeam backup status report

Veeam Logoby pizzim13 » Tue Jan 24, 2012 1:40 pm

Nice to hear that people are using it. I have only tinkered with v6. Once we have decided the best way to setup v6 for our environment, I will make revisions to this script and post them when finished. As Seth asked, what issues are you running into?
pizzim13
Enthusiast
 
Posts: 86
Liked: 6 times
Joined: Thu Apr 21, 2011 7:37 pm

Re: PowerShell Veeam backup status report

Veeam Logoby ekranz » Tue Feb 28, 2012 5:26 pm

I'm actually having the same problem that FkB|Stan had, except I'm using V6.
Code: Select all
PS C:\ps> .\veeamreport.ps1

Id              Name            State      HasMoreData     Location             Command
--              ----            -----      -----------     --------             -------
1               Job1            Running    True            bu1.core.dakota.m... ...
1               Job1            Completed  False           bu1.core.dakota.m... ...
3               Job3            Running    True            bu1.core.dakota.m... $JobStatus = (Get-VBRJ...
1               Job1            Completed  False           bu1.core.dakota.m... ...
3               Job3            Completed  True            bu1.core.dakota.m... $JobStatus = (Get-VBRJ...
WARNING: You should update your PowerShell to PowerShell 2.0 version.
Cannot convert argument "0", with value: "", for "GetByJobSession" to type "System.Guid": "Cannot convert null to type
"System.Guid"."
At C:\ps\veeamreport.ps1:70 char:13
+ $JobStatus = <<<<  Get-Job | Receive-Job
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

PS C:\ps> get-host

Name             : ConsoleHost
Version          : 2.0
InstanceId       : f3d80527-bb3a-4c3f-9e58-710b5b4b1443
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

The Veeam server is running Windows 2008 R2 and the client I'm on is a Windows 7 machine, so I've got PowerShell Version 2 on both.
ekranz
Novice
 
Posts: 7
Liked: never
Joined: Thu Jun 23, 2011 1:47 pm
Full Name: Ed Kranz

Next

Return to PowerShell



Who is online

Users browsing this forum: No registered users and 7 guests