I tried to create a simple PowerShell script to monitor my Hyper-V cluster. The script works if I run it manually on the PRTG server but for some reason the sensor status is always Up. What could be the problem?

$Nodes = Get-ClusterNode -Cluster HVCluster01
$Interfaces = Get-ClusterNetworkInterface -Cluster HVCluster01
$NodeStatus = $null
$InterfaceStatus = $null

ForEach ($Node in $Nodes) {
    $NodeState = $Node.State
    If ($NodeState -ne "Up") {
        $NodeStatus = "Down"
    }
    Else {}
}

ForEach ($Interface in $Interfaces) {
    $InterfaceState = $Interface.State
    If ($InterfaceState -ne "Up") {
        $InterfaceStatus = "Down"
    }
    Else {}
}

If ($NodeStatus -eq "Down" -or $InterfaceStatus -eq "Down") {
    Write-Host "Status:Down"
    exit 2
}
Else {
    Write-Host "Status:Ok"
    exit 0
}
}

Article Comments

Hello Mika,

Thank you for your message.

Regarding your script, can you please tell me what you would like to achieve exactly? Do you want to have a channel for each node / interface with their corresponding state?

If that's the case, then you have to return a JSON to PRTG, which follows the structure indicated this manual: https://www.paessler.com/manuals/prtg/custom_sensors#advanced_sensors

For each channel, you must add a result object such as the following:

<result>
           <channel>Node name</channel>
           <value>Node status (must be converted to 0 or 1)</value>
           <valuelookup>YourCustomLookup</valuelookup>
</result>
<result>
           <channel>Interface name</channel>
           <value>Interface status (must be converted to 0 or 1)</value>
           <valuelookup>YourCustomLookup</valuelookup>
</result>

Then, in PRTG you can use a custom lookup to display the status UP or DOWN based on the value returned.

If you have questions, let us know.

Regards.


Mar, 2021 - Permalink

The plan was that the script will simply just give output OK (exit code 0) if every ClusterNode and ClusterNetworkInterface are in Up state. And if any of those are in different state then the script will return exit code 2.


Mar, 2021 - Permalink

Thank you for the clarification.

In this case, I would recommend to use the variables $NodeStatus and $InterfaceStatus as arrays, and add the value of each loop in the lists by using += instead of assigning the latest value.

For example, you can use this piece of code to add the value in the array, and by the same occasion, remove the variable $NodeState (same thing for the interface status):

$NodeStatus += $Node.State

Then, at the end of the script, simply check if the list contains the state DOWN an return the according result in PRTG.

Regards.


Mar, 2021 - Permalink

Hello Mika,

Thank you for your feedback.

Regarding PowerShell 64bit, I invite you to have a look to the application PSx64 from PRTG Tools Family as well as the Knowledge base article regarding this matter, such as https://helpdesk.paessler.com/en/support/solutions/articles/76000065205-force-64-bit-powershell

Regards.


Apr, 2021 - Permalink