Skip to content

Instantly share code, notes, and snippets.

@theagreeablecow
Last active May 11, 2023 22:05
Show Gist options
  • Save theagreeablecow/3682559 to your computer and use it in GitHub Desktop.
Save theagreeablecow/3682559 to your computer and use it in GitHub Desktop.
SAMReport for Veeam
############################################################################################################
# Module Variables - Veeam #
#-------------------------------#
# Customise report Variables
$EmailTo = $EmailTo
$EmailSubject = $EmailSubject
$ReportTitle = $ReportTitle
$ReportSubTitle = $ReportSubTitle
# Load required plug-ins
if ( (Get-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue) -eq $null ) {
Add-PsSnapin -Name VeeamPSSnapIn
}
if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null ) {
Add-PsSnapin -Name VMware.VimAutomation.Core
}
# Load server and array information
$Servers = ($env:COMPUTERNAME)
$VMware_Server = "vcenter.mydomain.com.au"
$Proxies = @("vbrproxy1","vbrproxy2","vbrproxy7","vbrproxy8")
$ServiceArray = @("VeeamBackupService.exe","Veeam Backup Catalog Data Service","VeeamTransportSvc","Veeam Backup and Replication Service","VeeamDeploymentService","VeeamNFSSvc")
$ProxyServiceArray = @("VeeamTransportSvc","VeeamDeploymentService")
$Jobs = Get-VBRJob
$BackupList = Get-VBRBackupRepository
<#
$OU = "OU=MyServers,DC=mydomain,DC=com,DC=au"
$Servers = Get-ADComputer -Filter {OperatingSystem -Like "Windows *Server*"} -SearchBase $OU | Select-Object –ExpandProperty Name
$Servers = @("Server1","Server2","Server3")
$Servers = Get-Content .\<path>\servers.txt
$Servers = ($env:COMPUTERNAME)
#>
#Miscellaneous
############################################################################################################
# Info #
#------------------------
$Title = "Job Status Summary"
$Comment = "The following is a summary of the most recent session for all jobs."
$Author = "The Agreeable Cow"
$PluginDate = "August 2012"
$Version = "v1.0"
# 1.0 16/08/2012 The Agreeable Cow Original Build
############################################################################################################
# Main Script #
#------------------------
# Get Job Status
foreach($Job in $Jobs)
{
$Session = $Job.FindLastSession()
if(!$Session){continue;}
$Info = $Session.GetTaskSessions()
foreach ($VM in $info) {
$JobsTotal += $JobsTotal.count + 1
$Status = $VM.status
if ($Status -eq "Success"){
$JobsSuccess += $JobsSuccess.count + 1
}
if ($Status -eq "InProgress" -or $Status -eq "Pending"){
$JobsPending += $JobsPending.count + 1
}
if ($Status -eq "Warning"){
$JobsWarning += $JobsWarning.count + 1
}
if ($Status -eq "Failed"){
$JobsFailed += $JobsFailed.count + 1
}
}
}
# Status Count and formatting
$JobsCount = $JobsSuccess + $JobsPending + $JobsWarning + $JobsFailed
$JobsUnknown = $JobsTotal - $JobsCount
if ($JobsSuccess -eq $NULL){$JobsSuccess = "0"}
if ($JobsPending -eq $NULL){$JobsPending = "0"}
if ($JobsWarning -eq $NULL){$JobsWarning = "0"}
if ($JobsFailed -eq $NULL){$JobsFailed = "0"}
if ($JobsUnknown -eq $NULL){$JobsUnknown = "0"}
$ResultsText = "Total Jobs: " + $JobsTotal + "</br>" + "Success: " + $JobsSuccess + "</br>" + "Pending: " + $JobsPending + "</br>" + "Warning: " + $JobsWarning + "</br>" + "Failed: " + $JobsFailed + "</br>" + "Unknown: " + $JobsUnknown
# Results Alert
if ($JobsFailed -ge 1 -OR $JobsUnknown -ge 1){
$ResultsAlert = "Alert"
}
elseif ($JobsPending -ge 1 -OR $JobsPending -ge 1){
$ResultsAlert = "Warning"
}
else{
$ResultsAlert = "Good"
}
############################################################################################################
# Output #
#------------------------
$OutText = $ResultsText # $OutText MUST be either $ResultsText or "" Valid $ResultsText is any text string
$OutData = "" # $OutData MUST be either $ResultsData or "" Valid $ResultsData is any data array
$OutAlert = $ResultsAlert # $OutAlert MUST be either $ResultsAlert or "" Valid $ResultsAlert are 'Good', 'Warning' or 'Alert'
$Attachment = "" # $Attachment MUST be either UNC path or ""
#Export-ModuleMember
############################################################################################################
# Info #
#------------------------
$Title = "Job Status Details"
$Comment = "The following is a detailed report of the most recent session for all jobs."
$Author = "The Agreeable Cow"
$PluginDate = "August 2012"
$Version = "v1.0"
# 1.0 16/08/2012 The Agreeable Cow Original Build
############################################################################################################
# Main Script #
#------------------------
$WarningAge = 1 # warning highlight for days since last backup
$MaxAge = 7 # error highlight for days since last backup
$ResultsData = @()
foreach($Job in $Jobs) {
$Session = $Job.FindLastSession()
if(!$Session){continue;}
$Info = $Session.GetTaskSessions()
foreach ($VM in $info) {
# Get Array Data
$JobName = $job.Name
$Name = $VM.Name
$Type = $job.jobtype
$Status = $VM.status
$Progress = $VM.Progress.DisplayName
$Size = [Math]::Round([Decimal]$VM.Progress.ProcessedSize/1GB,2)
$Start = $VM.progress.starttime
$Finish = $VM.progress.stoptime
$Duration = '{0:00}:{1:00}:{2:00}' -f ($VM.progress.duration | % {$_.Hours, $_.Minutes, $_.Seconds})
$schedule = $job.GetScheduleOptions()
$NextRun = $schedule.NextRun
$Age = ((Get-Date) - $Finish).Days
if ($Status -eq "Success"){
if ($age -gt $MaxAge) {
$age = "!RED!" + $age
$AgeAlert += $AgeAlert.count + 1
}
if ($age -ge $WarningAge) {
$age = "!ORANGE!" + $age
$AgeWarning += $AgeWarning.count + 1
}
if ($age -lt $WarningAge) {
$age = "!GREEN!" + $age
$AgeGood += $AgeGood.count + 1
}
}
else{
$age = ""
}
$Message = $VM.GetDetails()
# Status Count
if ($Status -eq "Success"){
$JobsSuccess += $JobsSuccess.count + 1
}
if ($Status -eq "InProgress" -or $Status -eq "Pending"){
$JobsPending += $JobsPending.count + 1
}
if ($Status -eq "Warning"){
$JobsWarning += $JobsWarning.count + 1
}
if ($Status -eq "Failed"){
$JobsFailed += $JobsFailed.count + 1
}
#Load Array
$obj = New-Object PSobject
$obj | Add-Member -MemberType NoteProperty -name "Job Name" -value $JobName
$obj | Add-Member -MemberType NoteProperty -name Type -value $Type
$obj | Add-Member -MemberType NoteProperty -name Name -value $Name
$obj | Add-Member -MemberType NoteProperty -name Status -value $Status
$obj | Add-Member -MemberType NoteProperty -name "Size Gb" -value $Size
$obj | Add-Member -MemberType NoteProperty -name Start -value $Start
$obj | Add-Member -MemberType NoteProperty -name Finish -value $Finish
$obj | Add-Member -MemberType NoteProperty -name Duration -value $Duration
$obj | Add-Member -MemberType NoteProperty -name Age -value $Age
$obj | Add-Member -MemberType NoteProperty -name NextRun -value $NextRun
$obj | Add-Member -MemberType NoteProperty -name Message -value $Message
$ResultsData += $obj
}
}
$ResultsData = $ResultsData | sort -Property "Job Name"
# Results Alert
if ($JobsFailed -ge 1 -OR $JobsUnknown -ge 1 -OR $AgeAlert -ge 1){
$ResultsAlert = "Alert"
}
elseif ($JobsPending -ge 1 -OR $JobsPending -ge 1 -OR $AgeWarning){
$ResultsAlert = "Warning"
}
else{
$ResultsAlert = "Good"
}
############################################################################################################
# Output #
#------------------------
$OutText = "" # $OutText MUST be either $ResultsText or "" Valid $ResultsText is any text string
$OutData = $ResultsData # $OutData MUST be either $ResultsData or "" Valid $ResultsData is any data array
$OutAlert = $ResultsAlert # $OutAlert MUST be either $ResultsAlert or "" Valid $ResultsAlert are 'Good', 'Warning' or 'Alert'
$Attachment = "" # $Attachment MUST be either UNC path or ""
############################################################################################################
# Info #
#------------------------
$Title = "Backup Storage Status"
$Comment = "The following shows storage data and any alerts for Veeam Backup repositories"
$Author = "The Agreeable Cow"
$PluginDate = "August 2012"
$Version = "v1.0"
# 1.0 16/08/2012 The Agreeable Cow Original Build
############################################################################################################
# Main Script #
#------------------------
$BackupDiskWarning = 10 # Warning highlight for % free disk space
$BackupDiskAlert = 5 # Alert highlight for % free disk space
# Get Backup data
$ResultsData = $BackupList | Get-vPCRepoInfo | Select @{Name="Repository Name"; Expression = {$_.Target}}, @{Name="Path"; Expression = {$_.Storepath}},
@{Name="Free (GB)"; Expression = {$_.StorageFree}}, @{Name="Total (GB)"; Expression = {$_.StorageTotal}}, @{Name="Free (%)"; Expression = {$_.FreePercentage}}
# Get Low Disk Space Alerts
foreach ($r in $BackupList) {
if ($r.Type -eq "WinLocal") {
$Server = $r.GetHost()
$FileCommander = [Veeam.Backup.Core.CRemoteWinFileCommander]::Create($Server.Info)
$storage = $FileCommander.GetDrives([ref]$null) | ?{$_.Name -eq $r.Path.Substring(0,3)}
$RepoFree = $storage.FreeSpace
$RepoTotal = $storage.TotalSpace
}
elseif ($r.Type -eq "LinuxLocal") {
$Server = $r.GetHost()
$FileCommander = new-object Veeam.Backup.Core.CSshFileCommander $server.info
$storage = $FileCommander.FindDirInfo($r.Path)
$RepoFree = $storage.FreeSpace
$RepoTotal = $storage.TotalSize
}
elseif ($r.Type -eq "CifsShare") {
$fso = New-Object -Com Scripting.FileSystemObject
$storage = $fso.GetDrive($r.Path)
$RepoFree = $storage.AvailableSpace
$RepoTotal = $storage.TotalSize
}
$RepoType = $r.Type
$RepoName = $r.Name
$RepoPath = $r.Path
$RepoFree = [Math]::Round([Decimal]$RepoFree/1GB,2)
$RepoTotal = [Math]::Round([Decimal]$RepoTotal/1GB,2)
$RepoFreePercent = [Math]::Round(($RepoFree/$RepoTotal)*100)
if ($RepoFreePercent -le $BackupDiskAlert) {
$ResultsText += "<span style='color:red'>Alert! Extremely low disk space for " + $RepoPath + " (" + $RepoFree + "Gb left of " + $RepoTotal + "Gb) </br></span>"
$BackupAlert += $BackupAlert.count + 1
}
elseif ($RepoFreePercent -le $BackupDiskWarning) {
$ResultsText += "<span style='color:orange'>Warning! Low disk space for " + $RepoPath + " (" + $RepoFree + "Gb left of " + $RepoTotal + "Gb) </br></span>"
$BackupWarning += $BackupWarning.count + 1
}
}
# Results Alert
if ($BackupAlert -ge 1){
$ResultsAlert = "Alert"
}
elseif ($BackupWarning -ge 1){
$ResultsAlert = "Warning"
}
else{
$ResultsAlert = "Good"
}
############################################################################################################
# Output #
#------------------------
$OutText = $ResultsText # $OutText MUST be either $ResultsText or "" Valid $ResultsText is any text string
$OutData = $ResultsData # $OutData MUST be either $ResultsData or "" Valid $ResultsData is any data array
$OutAlert = $ResultsAlert # $OutAlert MUST be either $ResultsAlert or "" Valid $ResultsAlert are 'Good', 'Warning' or 'Alert'
$Attachment = "" # $Attachment MUST be either UNC path or ""
############################################################################################################
# Info #
#------------------------
$Title = "Replica Storage Status"
$Comment = "The following shows storage data and any alerts for Veeam Replica repositories"
$Author = "The Agreeable Cow"
$PluginDate = "August 2012"
$Version = "v1.0"
# 1.0 16/08/2012 The Agreeable Cow Original Build
############################################################################################################
# Main Script #
#------------------------
$ReplicaDiskWarning = 10 # Warning highlight for % free disk space
$ReplicaDiskAlert = 5 # Alert highlight for % free disk space
#Replica Storage Info
$repList = Get-VBRJob | ?{$_.IsReplica}
#Low Disk Space Alerts
foreach ($replica in $repList) {
$repHost = $replica.GetTargetHost()
$ds = $repHost | Find-VBRDatastore -Name $replica.ViReplicaTargetOptions.DatastoreName
$Target = $repHost.Name
$Datastore = $replica.ViReplicaTargetOptions.DatastoreName
$StorageFree = [Math]::Round([Decimal]$ds.FreeSpace/1GB,2)
$StorageTotal = [Math]::Round([Decimal]$ds.Capacity/1GB,2)
$FreePercent = [Math]::Round(($ds.FreeSpace/$ds.Capacity)*100)
if ($FreePercent -le $ReplicaDiskAlert) {
$ResultsText += "<span style='color:red'>Alert! Extremely low disk space for " + $Datastore + " (" + $StorageFree + "Gb left of " + $StorageTotal + "Gb) </br></span>"
$ReplicaAlert += $ReplicaAlert.count + 1
}
elseif ($FreePercent -le $ReplicaDiskWarning) {
$ResultsText += "<span style='color:orange'>Warning! Low disk space for " + $Datastore + " (" + $StorageFree + "Gb left of " + $StorageTotal + "Gb) </br></span>"
$ReplicaWarning += $ReplicaWarning.count + 1
}
}
#Replica Data
$ResultsData = $repList | Get-vPCReplicaTarget | Select @{Name="Replica Target"; Expression = {$_.Target}}, Datastore, @{Name="Free (GB)"; Expression = {$_.StorageFree}},
@{Name="Total (GB)"; Expression = {$_.StorageTotal}}, @{Name="Free (%)"; Expression = {$_.FreePercentage}}
# Results Alert
if ($ReplicaAlert -ge 1){
$ResultsAlert = "Alert"
}
elseif ($ReplicaWarning -ge 1){
$ResultsAlert = "Warning"
}
else{
$ResultsAlert = "Good"
}
############################################################################################################
# Output #
#------------------------
$OutText = $ResultsText # $OutText MUST be either $ResultsText or "" Valid $ResultsText is any text string
$OutData = $ResultsData # $OutData MUST be either $ResultsData or "" Valid $ResultsData is any data array
$OutAlert = $ResultsAlert # $OutAlert MUST be either $ResultsAlert or "" Valid $ResultsAlert are 'Good', 'Warning' or 'Alert'
$Attachment = "" # $Attachment MUST be either UNC path or ""
############################################################################################################
# Info #
#------------------------
$Title = "Unprotected Machines"
$Comment = "Backup status of VMs in vCenter"
$Author = "The Agreeable Cow"
$PluginDate = "August 2012"
$Version = "v1.0"
# 1.0 20/08/2012 The Agreeable Cow Original Build
# 0.0 3/7/2012 tSightler Original scripts (http://sightunseen.org/blog/?p=1)
############################################################################################################
# Main Script #
#------------------------
$PoweredOnVMsOnly = "Y" # Change to "N" to include powered off VMs (eg Replicas and Templates)
$ExcludeVMs=@("Server1","Server2") # Specifically exclude more VMs eg ("vm1","vm2")
$AgeAlert = 7 # Alert on machines older than this many days
$ResultsData=@()
# Connect to vCenter
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | out-null
Connect-ViServer $VMware_Server | out-null
# Build hash table with excluded VMs
$excludedvms=@{}
foreach ($vm in $excludevms) {
$excludedvms.Add($vm, "Excluded")
}
# Get a list of all VMs from vCenter and add to hash table, assume Unprotected
$vms=@{}
foreach ($vm in (Get-VM | Where-Object {$_.PowerState -eq "PoweredOn"} | ForEach-Object {$_ | Select-object @{Name="VMname";Expression={$_.Name}}})) {
if (!$excludedvms.ContainsKey($vm.VMname)) {
$vms.Add($vm.VMname, "Unprotected")
}
}
# Find all backup job sessions that have ended in the last week
$vbrsessions = Get-VBRBackupSession | Where-Object {$_.JobType -eq "Backup" -or $_.JobType -eq "Replica" -and $_.EndTime -ge (Get-Date).adddays(-$AgeAlert)}
# Find all successfully backed up VMs in selected sessions (i.e. VMs not ending in failure) and update status to "Protected"
$backedupvms=@{}
foreach ($session in $vbrsessions) {
foreach ($vm in ($session.gettasksessions() | Where-Object {$_.Status -ne "Failed"} | ForEach-Object { $_ | Select-object @{Name="VMname";Expression={$_.Name}}})) {
if($vms.ContainsKey($vm.VMname)) {
$vms[$vm.VMname]="Protected"
}
}
}
# Output VMs in color coded format based on status.
$ResultsText = "All VMs matching your criteria have been backed up in the past " + $AgeAlert + " Day(s)"
$ResultsAlert = "Good"
foreach ($vm in $vms.Keys){
if ($vms[$vm] -ne "Protected") {
$obj = New-Object PSobject
$obj | Add-Member -MemberType NoteProperty -name "Server" -value $vm
$ResultsData += $obj
$ResultsText = "The following machines have not been backed up in the past " + $AgeAlert + " Day(s)"
$ResultsAlert = "Alert"
}
}
############################################################################################################
# Output #
#------------------------
$OutText = $ResultsText # $OutText MUST be either $ResultsText or "" Valid $ResultsText is any text string
$OutData = $ResultsData # $OutData MUST be either $ResultsData or "" Valid $ResultsData is any data array
$OutAlert = $ResultsAlert # $OutAlert MUST be either $ResultsAlert or "" Valid $ResultsAlert are 'Good', 'Warning' or 'Alert'
$Attachment = "" # $Attachment MUST be either UNC path or ""
############################################################################################################
# Info #
#------------------------
$Title = "Services Status"
$Comment = "Current Status of all Veeam Services"
$Author = "The Agreeable Cow"
$PluginDate = "August 2012"
$Version = "v1.0"
# 1.0 17/08/2012 The Agreeable Cow Original Build
############################################################################################################
# Main Script #
#------------------------
$ServerData = @()
$ProxyData = @()
$ResultsData = @()
foreach ($Server in $Servers){
foreach ($Service in $ServiceArray){
#CheckService $Server $Service
$GetService = Get-Service -computername $Server -Name $Service
if ($GetService.Status -ne “Running”){
$ServerText += "<span style='color:red'>" + $GetService.displayname + " is not running on " + $Server + ". Please check Veeam Services! </br></span>"
$ServiceAlert += $ServiceAlert.count + 1
}
$obj = New-Object PSobject
$obj | Add-Member -MemberType NoteProperty -name "Server" -value $Server
$obj | Add-Member -MemberType NoteProperty -name "Status" -value $GetService.status
$obj | Add-Member -MemberType NoteProperty -name "Service" -value $GetService.displayname
$ServerData += $obj
}
}
foreach ($Server in $Proxies){
foreach ($Service in $ProxyServiceArray){
$GetService = Get-Service -computername $Server -Name $Service
if ($GetService.Status -ne “Running”){
$ProxyText += "<span style='color:red'>" + $GetService.displayname + " is not running on " + $Server + ". Please check Veeam Services! </br></span>"
$ServiceAlert += $ServiceAlert.count + 1
}
$obj = New-Object PSobject
$obj | Add-Member -MemberType NoteProperty -name "Server" -value $Server
$obj | Add-Member -MemberType NoteProperty -name "Status" -value $GetService.status
$obj | Add-Member -MemberType NoteProperty -name "Service" -value $GetService.displayname
$ProxyData += $obj
}
}
# Collate Results
$ResultsText = $ServerText + $ProxyText
$ResultsData = $ServerData + $ProxyData
$ResultsData = $ResultsData | sort -Property "Server"
# Results Alert
if ($ServiceAlert -ge 1){
$ResultsAlert = "Alert"
}
else{
$ResultsAlert = "Good"
}
############################################################################################################
# Output #
#------------------------
$OutText = $ResultsText # $OutText MUST be either $ResultsText or "" Valid $ResultsText is any text string
$OutData = $ResultsData # $OutData MUST be either $ResultsData or "" Valid $ResultsData is any data array
$OutAlert = $ResultsAlert # $OutAlert MUST be either $ResultsAlert or "" Valid $ResultsAlert are 'Good', 'Warning' or 'Alert'
$Attachment = "" # $Attachment MUST be either UNC path or ""
############################################################################################################
# Info #
#------------------------
$Title = "License Check"
$Comment = "Information regarding Veeam Licensing"
$Author = "The Agreeable Cow"
$PluginDate = "27/08/2012"
$Version = "v1.0"
# 1.0 01/01/2013 The Agreeable Cow Original Build
# 0.0 29/12/2011 Arne Fokkema Script Source http://ict-freak.nl/2011/12/29/powershell-veeam-br-get-total-days-before-the-license-expires/
############################################################################################################
# Main Script #
#------------------------
$WarningDays = 60 # Number of days before license expires to flag a warning
$AlertDays = 30 # Number of days before license expires to flag an alert
#Get version and Licenses Info
$VeeamVersion = Get-VeeamVersion
$regBinary = (Get-Item 'HKLM:\SOFTWARE\VeeaM\Veeam Backup and Replication\license').GetValue('Lic1')
$veeamLicInfo = [string]::Join($null, ($regBinary | % { [char][int]$_; }))
if($VeeamVersion -like "6*"){
$pattern = "Expiration date\=\d{1,2}\/\d{1,2}\/\d{1,4}"
}
elseif($VeeamVersion -like "5*"){
$pattern = "EXPIRATION DATE\=\d{1,2}\/\d{1,2}\/\d{1,4}"
}
# Convert Binary key
if($VeeamVersion -like "5*" -OR $VeeamVersion -like "6*"){
$expirationDate = [regex]::matches($VeeamLicInfo, $pattern)[0].Value.Split("=")[1]
$totalDaysLeft = ((Get-Date $expirationDate) - (get-date)).Totaldays.toString().split(",")[0]
$totalDaysLeft = [int]$totalDaysLeft
if($totalDaysLeft -lt $AlertDays){
$ResultsText = "Alert: The Veeam License will expire in $($totalDaysLeft) days"
$ResultsAlert = "Alert"
}
elseif($totalDaysLeft -lt $WarningDays){
$ResultsText = "Warning: The Veeam License will expire in $($totalDaysLeft) days"
$ResultsAlert = "Warning"
}
else{
$ResultsText = "The Veeam License will expire in $($totalDaysLeft) days"
$ResultsAlert = "Good"
}
}
else{
$ResultsText = "Warning: Unable to process Veeam version"
$ResultsAlert = "Warning"
}
############################################################################################################
# Output #
#------------------------
$OutText = $ResultsText # $OutText MUST be either $ResultsText or "" Valid $ResultsText is any text string
$OutData = "" # $OutData MUST be either $ResultsData or "" Valid $ResultsData is any data array
$OutAlert = $ResultsAlert # $OutAlert MUST be either $ResultsAlert or "" Valid $ResultsAlert are 'Good', 'Warning' or 'Alert'
$Attachment = "" # $Attachment MUST be either UNC path or ""
############################################################################################################
# Info #
#------------------------
$Title = "Event Logs"
$Comment = "Event Log entries that match your criteria"
$Author = "The Agreeable Cow"
$PluginDate = "1/9/2012"
$Version = "v1.0"
# 1.0 01/9/2012 The Agreeable Cow Original Build
############################################################################################################
# Main Script #
#------------------------
#Create an Array and run query
$ResultsData = @()
$AlertData = @()
$WarningData = @()
$AllServers = $Servers + $Proxies
foreach ($Server in $AllServers){
#Customise an ALERT query via Event Viewer > Create Custom View > Copy query string from XML tab
$AlertXML = '<QueryList>
<Query Id="0" Path="System">
<Select Path="System">*[System[(Level=1 or Level=2) and TimeCreated[timediff(@SystemTime) &lt;= 86400000]]]</Select>
<Select Path="HardwareEvents">*[System[(Level=1 or Level=2) and TimeCreated[timediff(@SystemTime) &lt;= 86400000]]]</Select>
<Select Path="Veeam Backup">*[System[(Level=1 or Level=2) and TimeCreated[timediff(@SystemTime) &lt;= 86400000]]]</Select>
<Suppress Path="System">*[System[(EventID=4)]]</Suppress>
<Suppress Path="HardwareEvents">*[System[(EventID=4)]]</Suppress>
<Suppress Path="Veeam Backup">*[System[(EventID=4)]]</Suppress>
</Query>
</QueryList>'
$AlertEvents = Get-WinEvent -ea SilentlyContinue -ComputerName $Server -Filterxml $AlertXML
ForEach ($LogEntry in $AlertEvents) {
if ($LogEntry.Level -eq 1){$LevelTxt = "!RED!Critical"}
elseif ($LogEntry.Level -eq 2){$LevelTxt = "!RED!Error"}
elseif ($LogEntry.Level -eq 3){$LevelTxt = "!ORANGE!Warning"}
else {$LevelTxt = "Info"}
if ($LogEntry.Message.Length -ge 75){$Msg = $LogEntry.Message.substring(0,75)}
else {$Msg = $LogEntry.Message}
$obj = New-Object PSobject
$obj | Add-Member -MemberType NoteProperty -name "Level" -value $LevelTxt
$obj | Add-Member -MemberType NoteProperty -name "Logged" -value $LogEntry.TimeCreated
$obj | Add-Member -MemberType NoteProperty -name "Source" -value $LogEntry.ProviderName
$obj | Add-Member -MemberType NoteProperty -name "ID" -value $LogEntry.ID
$obj | Add-Member -MemberType NoteProperty -name "Computer" -value $Server
$obj | Add-Member -MemberType NoteProperty -name "Event Data" -value $Msg
$AlertData += $obj
$AlertCount += $AlertCount.count + 1
}
#Customise a WARNING query via Event Viewer > Create Custom View > Copy query string from XML tab
$WarningXML = '<QueryList>
<Query Id="0" Path="System">
<Select Path="System">*[System[(Level=3) and TimeCreated[timediff(@SystemTime) &lt;= 86400000]]]</Select>
<Select Path="HardwareEvents">*[System[(Level=3) and TimeCreated[timediff(@SystemTime) &lt;= 86400000]]]</Select>
<Select Path="Veeam Backup">*[System[(Level=3) and TimeCreated[timediff(@SystemTime) &lt;= 86400000]]]</Select>
<Suppress Path="System">*[System[(EventID=4)]]</Suppress>
<Suppress Path="HardwareEvents">*[System[(EventID=4)]]</Suppress>
<Suppress Path="Veeam Backup">*[System[(EventID=4)]]</Suppress>
</Query>
</QueryList>'
$WarningEvents = Get-WinEvent -ea SilentlyContinue -ComputerName $Server -Filterxml $WarningXML
ForEach ($LogEntry in $WarningEvents) {
if ($LogEntry.Level -eq 1){$LevelTxt = "!RED!Critical"}
elseif ($LogEntry.Level -eq 2){$LevelTxt = "!RED!Error"}
elseif ($LogEntry.Level -eq 3){$LevelTxt = "!ORANGE!Warning"}
else {$LevelTxt = "Info"}
if ($LogEntry.Message.Length -ge 75){$Msg = $LogEntry.Message.substring(0,75)}
else {$Msg = $LogEntry.Message}
$obj = New-Object PSobject
$obj | Add-Member -MemberType NoteProperty -name "Level" -value $LevelTxt
$obj | Add-Member -MemberType NoteProperty -name "Logged" -value $LogEntry.TimeCreated
$obj | Add-Member -MemberType NoteProperty -name "Source" -value $LogEntry.ProviderName
$obj | Add-Member -MemberType NoteProperty -name "ID" -value $LogEntry.ID
$obj | Add-Member -MemberType NoteProperty -name "Computer" -value $Server
$obj | Add-Member -MemberType NoteProperty -name "Event Data" -value $Msg
$AlertData += $obj
$WarningCount += $WarningCount.count + 1
}
}
# Results Data
$ResultsData = $AlertData + $WarningData | sort -Property "Logged"
# Results Alert
if ($AlertCount -ge 1){
$ResultsAlert = "Alert"
}
elseif ($WarningCount -ge 1){
$ResultsAlert = "Warning"
}
else{
$ResultsAlert = "Good"
}
############################################################################################################
# Output #
#------------------------
$OutText = "" # $OutText MUST be either $ResultsText or "" Valid $ResultsText is any text string
$OutData = $ResultsData # $OutData MUST be either $ResultsData or "" Valid $ResultsData is any data array
$OutAlert = $ResultsAlert # $OutAlert MUST be either $ResultsAlert or "" Valid $ResultsAlert are 'Good', 'Warning' or 'Alert'
$Attachment = "" # $Attachment MUST be either UNC path or ""
@theagreeablecow
Copy link
Author

theagreeablecow commented May 11, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment