PowerShell script exchange
Post Reply
PatrickBerger
Lurker
Posts: 2
Liked: never
Joined: Dec 12, 2016 2:21 pm
Full Name: Patrick Berger
Contact:

Request: script for last run alerts

Post by PatrickBerger »

Dear Veeam community,

So far ive been using a script to give out alerts (by either giving a certain text in the powershell output or write it in the event logs) when backups have failed in the last 24 hours.
However now a colleague of mine made a smart remark .. what if for some weird reason or error, the backup hasnt run in the last 24 hours. And i honestly came to the conclusion that we wouldnt notice if that was the case.

Now hereby the question. Does someone have a script or a start up to a script that takes the date from the PC/Server and looks if there are jobs that have NOT ran in the last 24 hours (or x hours/days for that matter) so that the script may give me the option to output a certain code or write into an eventlog when this is the case.
I kinda would prefer it to be totally seperate from the already used script but just to be sure i will simply post the script we run atm here to.

Code: Select all

#Datamex Remote Monitoring als source aanmaken in application eventlog:
New-EventLog -LogName Application -Source "Datamex Remote Monitoring" > $null

#requires -Version 1
Function Get-VeeamHistory
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true, Position = 0)]
        [System.Int32]
        $Days
    )

    Add-PSSnapin -Name VeeamPSSnapin
    
    $colJobHistory = @()

    $JobHistory = Get-VBRBackupSession |
    Where-Object -FilterScript {
        $_.CreationTime -ge (Get-Date).AddDays(-$Days)
    } |
    Sort-Object -Property CreationTime -Descending

    ForEach ($Job In $JobHistory)
    {
        $objJobHistory = New-Object -TypeName System.Object
        $objJobHistory | Add-Member -MemberType NoteProperty -Name Name -Value $Job.JobName 
        $objJobHistory | Add-Member -MemberType NoteProperty -Name StartTime -Value ($Job.CreationTime -f [datetime])
        $objJobHistory | Add-Member -MemberType NoteProperty -Name EndTime -Value ($Job.EndTime -f [datetime])

        $Duration = New-TimeSpan -Start $Job.CreationTime -End $Job.EndTime
        $Duration = '{0:00}:{1:00}:{2:00}' -f $Duration.Hours, $Duration.Minutes, $Duration.Seconds

        $objJobHistory | Add-Member -MemberType NoteProperty -Name Duration -Value $Duration
        
        $AuxData = [xml] $Job.AuxData
        $DataSizeB = $AuxData.AuxData.CBackupStats.BackupSize
        $DataSizeGB = [math]::round($DataSizeB / 1GB,2)

        $objJobHistory | Add-Member -MemberType NoteProperty -Name DataSizeGB -Value $DataSizeGB        
        $objJobHistory | Add-Member -MemberType NoteProperty -Name Result -Value $Job.Result
        #$objJobHistory | Add-Member -MemberType NoteProperty -Name ErrorMessage -Value $Job.ErrorMessage

        Switch ($Job.Result)
        {
            'Success' 
            {
                $AEMResult = 0
                Break
            }
            'Warning' 
            {
                $AEMResult = 1
                Break
            }
            'Failed' 
            {
                $AEMResult = 2
                Break
            }
            default 
            {
                $AEMResult = 1
                Break
            }
        }

        $objJobHistory | Add-Member -MemberType NoteProperty -Name AEMResult -Value $AEMResult

        $colJobHistory += $objJobHistory
    }

    Return $colJobHistory
}

Function Exit-AEMResult
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [System.Object]
        $AEMResult
    )

    Begin
    {
        $AEMResultCode = 0
    }

    Process
    {
        Write-Output -InputObject $AEMResult | Select-Object -Property * -ExcludeProperty AEMResult

        If ($AEMResult.AEMResult -gt $AEMResultCode) 
        {
            $AEMResultCode = $AEMResult.AEMResult
        }
    }

    End
    {

        Write-Host -NoNewline -Object 'Backup Check Result: '
        Switch ( $AEMResultCode )
        { 
            0 
            {
                Write-Host -Object 'Success'
Write-EventLog -logname Application -Source "Datamex Remote Monitoring" -EntryType Information -EventId 7777 -Message "Veeam backups afgelopen 24 uur gelukt." 
                Exit 0
            } 
            1 
            {
                Write-Host -Object 'Warning'
Write-EventLog -logname Application -Source "Datamex Remote Monitoring" -EntryType Error -EventId 6666 -Message "Veeam warnings gedetecteerd!" 
                Exit 0
            } 
            2 
            {
                Write-Host -Object 'Failure'
Write-EventLog -logname Application -Source "Datamex Remote Monitoring" -EntryType Error -EventId 6666 -Message "Veeam Failure(s) gedetecteerd!" 
                Exit 1
            } 
        }
    }
}

Get-VeeamHistory -Days $env:Days | Exit-AEMResult
Vitaliy S.
VP, Product Management
Posts: 27055
Liked: 2710 times
Joined: Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov
Contact:

Re: Request: script for last run alerts

Post by Vitaliy S. » 1 person likes this post

Hi Patrick,

I cannot comment on the PowerShell piece, but you can try to use these solutions (available in Veeam ONE) to address your scenario:

1. Run Protected VMs, which does show VMs which didn't have a backup within the defined RPO
2. Use "VM with no backups" alert, that would can trigger a backup job, once it identifies that there were no backups for the given VM. Check out this topic for more info on the alert > ONEbelievable tricks: Veeam ONE and Automation Services

Thanks!
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Request: script for last run alerts

Post by veremin » 1 person likes this post

The following script should list all jobs that haven't been executed within last 24 hours:

Code: Select all

asnp VeeamPSSnapin
$Day=(Get-Date).adddays(-1)
Get-VBRJob | where {$_.FindLastSession().CreationTime -lt $Day} | select name
Thanks.
PatrickBerger
Lurker
Posts: 2
Liked: never
Joined: Dec 12, 2016 2:21 pm
Full Name: Patrick Berger
Contact:

Re: Request: script for last run alerts

Post by PatrickBerger »

Thanks alot for both replies! Will keep those features in mind.
Also i am sure i can make something happen using the powershell command.
efd121
Enthusiast
Posts: 46
Liked: 2 times
Joined: Aug 07, 2015 8:45 pm
Full Name: David Engler
Contact:

Re: Request: script for last run alerts

Post by efd121 »

Take a look at the script I posted powershell-f26/get-vbrrestorepoint-extr ... 39613.html.

The script looks at a VMware tag to determine if the RPO time has been met for each VM. I have some jobs that only run once a week and this script takes that into account before reporting a failure. With some help from tsightler it now runs in about 10 minutes checking almost 600 VMs.

Dave
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Request: script for last run alerts

Post by veremin »

Nice script, indeed. And agree with Tom that usage of hash table should result in even better performance. Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 27 guests