Powershell script use output for monitoring purposes

Hi everyone,

We downloaded a script for Nagios that allows us to monitor Hyper-V replication between hosts. The script is written in Powershell and is triggered by the NSClient. This works perfectly. Now we want to use that script as a base for writing our own PowerShell checks for Nagios.

$server=get-vmreplication

$returnStateOK = 0
$returnStateCritical = 2
$errormassage = "Replication works fine!"

foreach($r in $server)
{
    if($r.Health -cnotlike "Normal")
    {
        $f=1
        $errormassage = "Replication-Error - " +$r.Name + "state: " + $r.Health
    }
}
 
if($f -eq 1)
{
    Write-Host $errormassage
    exit $returnStateCritical
}
else
{
    Write-Host $errormassage
    exit $returnStateOK
}


Import-Module "C:\Program Files\NSClient++\scripts\ADFSDiagnostics.psm1" 

$returnStateOK = 0
$returnStateWarning = 1
$returnStateCritical = 2
$returnStateUnknown = 3

if($server=Test-AdfsServerHealth | where {$_.Result -eq "Fail"})
{
    $report = "warning"
}
else
{
    $report="ok"
}

    if($report -eq "warning")
    {
        $errormessage = "ADFS Health state: Warning," +$Errordetail
        Write-Host $errormessage
        exit $returnStateCritical
    }
    elseif($report -eq "ok")
    {
        $Okmessage = "ADFS Health state: OK, no problems have been found." 
        Write-Host $Okmessage
        exit $returnStateOK
    }
    else
    {
    exit $returnStateUnknown
    }


Above is the script that we created by copy pasting anything that looked right. It results in displaying the "warning" part, I just want to use the output from

Test-AdfsServerHealth | where {$_.Result -eq "Fail"}

to display the name and/or Detail of the found problem. If we execute the above command without the rest of the script we get this result:

Does anyone know how I can capture the displayed Name and Detail and output that for my Nagios check? I tried $r.Name only because it was written in the other script aswell. Sorry for the stupid questions ;-)

Kind regards,
Dennis

August 21st, 2015 8:06am

Hi,

You can pipe through Select-Object to return only the properties you're interested in.

http://ss64.com/ps/select-object.html

Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 8:10am

Hi Mike,

Thank you for your suggestion, this worked perfectly. I added     $ErrorDetail = $server | Select-Object Detail into the PS script. It didn't show the text in Nagios at first, I had to change PowerShell to Unrestricted for the user that the NSclient was using.

Kind regards,
Dennis Lans

August 21st, 2015 9:00am

Cheers Dennis, you're very welcome. Glad it worked out for you.
Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 9:04am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics