# Altaro Backup Monitoring
#-------------------
# ------------------
param(
    [string]$ComputerName
    )

$ret = Invoke-Command -ComputerName $ComputerName -ScriptBlock {
$Yesterday = ((Get-Date) - (New-TimeSpan -Day 1))
[int]$retvalue = 0
try { 
    $Geteventlog = (Get-WinEvent -ErrorAction Stop  -FilterHashtable @{LogName='Application';StartTime=$Yesterday;Id=5000,5005,5007,5002}) 
} catch {
   $_.Exception.GetType().FullName
   $Geteventlog_Success = "0"
}
for($i = 0 ; $i -le $Geteventlog_Success.Length; $i++){
    $Id = $Geteventlog_Success[$i].Id
    $Message = $Geteventlog_Success[$i].Message

    Switch ($Id) {
       "5007" { Write-Host "2:Backups NOT OK"
                $retvalue =  2;
                Exit 2}
       "5000" { Write-Host "0:Backups OK"
                $retvalue = 0;
                }
        
       "5001" { 
                Write-Host "1:Backup Completed With Warnings"
            $retvalue =  1
            Exit 1
            }
       "5002" { Write-Host "2:Backups NOT OK"
                $retvalue = 2;
                Exit 2 }
       "5005" { Write-Host "0:Backups OK"
                $retvalue =  0;}

       "0" { Write-Host "2:No Backups Last 24 hours"; $retvalue =  2; Exit 2}
       }
    }
    return $retvalue
}

This Always gives me back "Backups not OK" but the state stays on up and not DOWN of the sensor. When a backup fails i need the sensor to go down.


Article Comments

Hi BartTechne,

Did you also assign corresponding limits in the Sensor?


Mar, 2020 - Permalink

Hi,

I changed this towards and Advanced sensor. And then it works. Only one small issue now.. When it goes in to result 0 i get the error channel is not defined...

# Altaro Backup Monitoring
#-------------------
# ------------------
param(
    [string]$ComputerName
    )

$ret = Invoke-Command -ComputerName $ComputerName -ScriptBlock {
$Yesterday = ((Get-Date) - (New-TimeSpan -Day 1))

try { 
    $Geteventlog_Success = (Get-WinEvent -ErrorAction Stop  -FilterHashtable @{LogName='Application';StartTime=$Yesterday;Id=5000,5005,5007,5002}) 
} catch {
   $_.Exception.GetType().FullName
   $Geteventlog_Success = "0"
}
for($i = 0 ; $i -le $Geteventlog_Success.Length; $i++){
    $Id = $Geteventlog_Success[$i].Id
    $Message = $Geteventlog_Success[$i].Message

    Switch ($Id) {
       "5007" { 
                write-host "<prtg>"
                write-host "<channel>Backup</channel>"
                write-host "<error>1</error>"
                write-host "<text>$Message</text>"
                write-host "</prtg>"
                Exit 2
                }

       "5002" { 
                write-host "<prtg>"
                write-host "<channel>Backup</channel>"
                write-host "<error>1</error>"
                write-host "<text>$Message</text>"
                write-host "</prtg>"
                Exit 2 }

       "5001" { 
                write-host "<prtg>"
                write-host "<channel>Backup</channel>"
                write-host "<error>2</error>"
                write-host "<text>$Message</text>"
                write-host "</prtg>"
                Exit 1
                }


       "5000" { 
                write-host "<prtg>"
                write-host "<channel>Backup</channel>"
                write-host "<result>0</result>"
                write-host "<text>$Message</text>"
                write-host "</prtg>"
                Exit 0
                }
        

       "5005" { 
                write-host "<prtg>"
                write-host "<channel>Backup</channel>"
                write-host "<error>1</error>"
                write-host "<text>$Message</text>"
                write-host "</prtg>"
                Exit 0
                }


       "0" {    
                write-host "<prtg>"
                write-host "<channel>Backup</channel>"
                write-host "<error>1</error>"
                write-host "<text>No Backups last 24 hours</text>"
                write-host "</prtg>"
           }
       }
    }
    
}

Mar, 2020 - Permalink

Hi Bart,

Please note that you defined a channel but no result. However, if you want to transmit only a error state, you don't need to enter a certain channel since the error state is always for the entire Sensor.
In addition, I mentioned that you entered "2" in "5001". Please note this is no valid error state. For more detailed information, please take a look at this article.


Mar, 2020 - Permalink

I tried to change this towards an simple EXE sensor.

# Altaro Backup Monitoring
#-------------------
# ------------------
param(
    [string]$ComputerName
    )

$ret = Invoke-Command -ComputerName "S-TEC-MGMT" -ScriptBlock {
$Yesterday = ((Get-Date) - (New-TimeSpan -Day 1))

try { 
    $Geteventlog_Success = (Get-WinEvent -ErrorAction Stop  -FilterHashtable @{LogName='Application';StartTime=$Yesterday;Id=5000,5001,5005,5007,5002}) 
} catch {
   $_.Exception.GetType().FullName
   $Geteventlog_Success = "0"
}
for($i = 0 ; $i -le $Geteventlog_Success.Length; $i++){
    $Id = $Geteventlog_Success[$i].Id
    $Message = $Geteventlog_Success[$i].Message

    Switch ($Id) {
       "5007" { 
                write-host 2:$Message
                Exit 2
                }

       "5002" { 
                write-host 2:$Message
                Exit 2
               }

       "5001" { 
                write-host 1:$Message
                Exit 1
               }

       "0" {    
                write-host 2:$Message
                Exit 2
           }
       default { 
                write-host 0:$Message
                Exit 0
        }
       }
    }
}

But now it does not Exit with 1 ? and PRTG says the sensor is up ? And i want to display a warning. Error 5001 is triggerd.


Mar, 2020 - Permalink

Hi Bart,

Please note that you set the IDs in the Switch clause in quotes. As a result, powershell interprets it as a string and not as a integer. Therefore, the default clause is always triggered.


Mar, 2020 - Permalink

So i Decided to do a complete overhaul of the script and the functionality is OK. But i have 1 question with baffles me at this time because i can not seem to understand the way PRTG handels EXIT 1 and error values.

Situation. Lets say the backup has a warning ( Event log ID: 5001) -> Exit 1 -> But The sensor in PRTG says Down. Is theire a way that i can tell PRTG when it exits with 1 it only displays a warning and not the value "Down" ?

# Altaro Backup Monitoring
#-------------------
# ------------------

param(
    [string]$ComputerName
    )


$ret = Invoke-Command -ComputerName S-TEC-MGMT -ScriptBlock {

$Yesterday = ((Get-Date) - (New-TimeSpan -Day 1))



try { 
    $Geteventlog_Success = (Get-WinEvent -ErrorAction Stop  -FilterHashtable @{LogName='Application';StartTime=$Yesterday;Id=5000,5001,5005,5007,5002}) 
} 
catch {
   $_.Exception.GetType().FullName
   $Geteventlog_Success = 0
}

$array_Successfull = @()
$array_Failed = @()
$array_FailedOffsite = @()
$array_Warning = @()
$array_SuccessfullOffsite = @()

for($i = 0 ; $i -le $Geteventlog_Success.Length; $i++){

    $Id = $Geteventlog_Success[$i].Id

    Switch ($Id) {
       
       5007 { 
                $array_FailedOffsite += $Geteventlog_Success[$i]
            }

       5002 { 
                $array_Failed += $Geteventlog_Success[$i]
            }

       5001 { 
                $array_Warning += $Geteventlog_Success[$i]   
            }

       5005 { 
                $array_SuccessfullOffsite += $Geteventlog_Success[$i]
            }

       5000 {
                $array_Successfull += $Geteventlog_Success[$i]
            }

        0   {

                write-host "<prtg>"
                write-host "<error>1</error>"
                write-host "<text>No Backups for the last 24 hours</text>"
                write-host "</prtg>"
                Exit 2
        }
    }
}


if($array_FailedOffsite -ne " "){

    for ($j = 0; $j -le $array_FailedOffsite.Length; $j++){
        
        $Id2 = $array_FailedOffsite[$j].Id
        $Message = $array_FailedOffsite[$j].Message

        if ($Id2 = 5007) {
            write-host "<prtg>"
            write-host "<error>1</error>"
            write-host "<text>$Message</text>"
            write-host "</prtg>"
            Exit 2
        }
    }
}
if($array_Failed -ne " "){
    for ($k = 0; $k -le $array_Failed.Length; $k++){
        
        $Id3 = $array_Failed[$k].Id
        $Message = $array_Failed[$k].Message


        if ($Id3 = 5002) {

            write-host "<prtg>"
            write-host "<error>1</error>"
            write-host "<text>$Message</text>"
            write-host "</prtg>"
            Exit 2
        }
    }
}

if($array_Warning -ne " "){
    for ($l = 0; $l -le $array_Warning.Length; $l++){
        
        $Id4 = $array_Warning[$l].Id
        $Message = $array_Warning[$l].Message

        if ($Id4 = 5001) {
            write-host "<prtg>"
            write-host "<error>1</error>"
            write-host "<text>$Message</text>"
            write-host "</prtg>"
            Exit 1
        }
    }
}

if($array_Successfull -ne " "){
    for ($m = 0; $m -le $array_SuccessfullOffsite.Length; $m++){
        
        $Id5 = $array_SuccessfullOffsite[$m].Id
        $Message = $array_SuccessfullOffsite[$m].Message
        
        if ($Id5 = 5005) {
            write-host "<prtg>"
            write-host "<error>1</error>"
            write-host "<text>$Message</text>"
            write-host "</prtg>"
            Exit 0
        }
    }
}

if($array_Successfull -ne " " ) {
    for ($n = 0; $n -le $array_Successfull.Length; $n++){
        
            $Id6 = $array_Successfull[$n].Id
            $Message = $array_Successfull[$n].Message

            if ($Id6 = 5000) {

            write-host "<prtg>"
            write-host "<error>0</error>"
            write-host "<text>$Message</text>"
            write-host "</prtg>"
            Exit 0
            }
        }
    }
}

Mar, 2020 - Permalink

Hi Bart,

The exit codes are only for the normal EXE/Script Sensor and not for the EXE/Script Advanced Sensor. If you want to trigger a warning in the advanced Sensor, you can use the "<Warning>" option. For example
Write-host "<prtg>" Write-Host "<result>" Write-Host "<channel>First channel</channel>" Write-Host "<value>10</value>" write-host "<Warning>1</Warning>" Write-Host "</result>" write-host "</prtg>"


Mar, 2020 - Permalink

is it also possible to pass through variables ?

So we can still see what failed in the sensor details ?

Like:

write-host "<prtg>"
            Write-Host "<result>"
            Write-Host "<channel>Backuo</channel>"
            Write-Host "<value>10</value>"
            write-host "<Warning>1</Warning>"
            Write-Host "</result>"
            Write-Host "<text>$Message</text>"
            write-host "</prtg>"

Mar, 2020 - Permalink

Hi Bart,

Yes this is possible. I added this option to my example here:

            $message="Place your text here"

            write-host "<prtg>"
            Write-Host "<result>"
            Write-Host "<channel>First channel</channel>"
            Write-Host "<value>10</value>"
            write-host "<Warning>1</Warning>"
            Write-Host "</result>"
            write-host "<Text>$message</Text>"
            write-host "</prtg>"

Mar, 2020 - Permalink