lordmilko / PrtgAPI

C#/PowerShell interface for PRTG Network Monitor
MIT License
301 stars 37 forks source link

Report about NotificationTrigger #340

Closed FSGermany closed 1 year ago

FSGermany commented 1 year ago

What's going on?

Hello,

we have problems in export specific sensor information with Notification Triggers from one server. The server has an sensor "SNMP HPE ProLiant system status" with several threshold triggers for different channels. All threshold triggers have Notification Trigges for NotificationTriggerOn and Off. When we execute our script we've got an error that [System.String] doesn't contain a method with the name "item".

We are using servers from the same manufacturer and the same generation but only one is making problems. We have already deleted the sensor in PRTG and add a new one, but the problem still exists.

Here is our script:

$_ONR = $_ONR = $env:computername.Substring(0,3)
$_Host = "test"
$_Server = $_ONR + "PRTG"
$_Datum = Get-Date -Format yyyyMMdd_HHmmss
$_Verzeichnis = "\\" + $_ONR + "\PRTG\Konfiguration"
$_Datei = $_Verzeichnis + "\" + $_ONR + "_PRTG_" + $_Datum + ".csv"

Connect-PrtgServer $_Server -IgnoreSSL -force

$_Sensors = New-Object PrtgAPI.Sensor
$_Sensors=get-Sensor

[System.Collections.ArrayList]$_mySensorData = @()

for ( $i = 0;$i -lt $_Sensors.Count ; $i++)
{
    $tmp1="error"
    $tmp2="error"

    $_myNotificationTrigger_1="NotificationTriggerOn"
    $_myNotificationTrigger_2="NotificationTriggerOff"

        try{$_myNotificationTrigger_1 = ($_Sensors.item($i) | Get-NotificationTrigger)}
        catch{$_myNotificationTrigger_1="Fehler beim auslesen" }
        try{$_myNotificationTrigger_2 = ($_Sensors.item($i) | Get-NotificationTrigger).OffNotificationAction }
        catch{$_myNotificationTrigger_2 ="Fehler beim auslesen"}

        <#

        #>

           $_myNotificationTrigger_1.OnNotificationAction | fl * | Out-Null
           $_myNotificationTrigger_2 | fl * | Out-Null

    $_mySensorDataElement = New-Object PSObject -Property @{
        Name   = $_Sensors.item($i).Name
        Id     = $_Sensors.item($i).Id
        Device = $_Sensors.item($i).Device
        Group  = $_Sensors.item($i).Group
        Dependency = $_Sensors.item($i).Dependency
        ParentID = $_Sensors.Item($i).ParentID
        OnNotificationAction = $_myNotificationTrigger_1.Name
        OnNotificationActionProgrammParameters = $_myNotificationTrigger_1.OnNotificationAction.Program.Parameters
        OffNotificationAction = $_myNotificationTrigger_2.Name
        OffNotificationActionProgrammParameters = $_myNotificationTrigger_2.Program.Parameters
    }
          if ($_myNotificationTrigger_1.count -gt 1 -or $_myNotificationTrigger_2.count -gt 1)
          {
                for ($j=0;$j -le  $_myNotificationTrigger_1.count-1 ; $j++)
                {
                    $_mySensorDataElement = New-Object PSObject -Property @{
                    Name   = $_Sensors.item($i).Name
                    Id     = $_Sensors.item($i).Id
                    Device = $_Sensors.item($i).Device
                    Group  = $_Sensors.item($i).Group
                    Dependency = $_Sensors.item($i).Dependency
                    ParentID = $_Sensors.item($i).ParentID
                    OnNotificationAction = $_myNotificationTrigger_1.item($j).Name
                    OnNotificationActionProgrammParameters = $_myNotificationTrigger_1.item($j).OnNotificationAction.Program.Parameters
                    OffNotificationAction = $_myNotificationTrigger_2.item($j).Name
                    OffNotificationActionProgrammParameters = $_myNotificationTrigger_2.item($j).Program.Parameters
                    }
                $_mySensorData.Add($_mySensorDataElement) | out-null
                }
          } 
          else
            {$_mySensorData.Add($_mySensorDataElement) | out-null}

}

$_mySensorData | Select-Object -Property Device,
Name,
OnNotificationActionProgrammParameters,
OffNotificationActionProgrammParameters,
OnNotificationAction,
OffNotificationAction,
Id,
Dependency,
ParentID | sort -Property Device,Name | Export-Csv -Delimiter ";" -Path $_Datei -NoTypeInformation -Encoding Unicode

Output of the script for the problem server:
"999DASI";"SNMP HPE ProLiant Systemzustand";"System.Object[]";;"System.Object[]";;"3510";;"2188"
"999DASI";"SNMP HPE ProLiant Systemzustand";"System.Object[]";;"System.Object[]";;"3510";;"2188"
"999DASI";"SNMP HPE ProLiant Systemzustand";"System.Object[]";;"System.Object[]";;"3510";;"2188"
"999DASI";"SNMP HPE ProLiant Systemzustand";"System.Object[]";;"System.Object[]";;"3510";;"2188"
"999DASI";"SNMP HPE ProLiant Systemzustand";"System.Object[]";;"System.Object[]";;"3510";;"2188"
"999DASI";"SNMP HPE ProLiant Systemzustand";"System.Object[]";;"System.Object[]";;"3510";;"2188"
"999DASI";"SNMP HPE ProLiant Systemzustand";"System.Object[]";;"System.Object[]";;"3510";;"2188"

Output of the script for an normal server:
"999HOSTADMIN";"SNMP HPE ProLiant Systemzustand";"-i 230  -d  ""Lüfter des %device ist defekt."" ""Sensor-ID (%sensorid)""";"-i 240  -d  ""Lüfter des %device ist in Ordnung."" ""Sensor-ID (%sensorid)""";"ILO Host Lüfter Fehler";"ILO Host Lüfter i.O.";"2733";;"2187"
"999HOSTADMIN";"SNMP HPE ProLiant Systemzustand";"-i 230  -d  ""Kühlung des %device nicht ordnungsgemäß, Temperatur des Arbeitsspeichers von %device ist außerhalb des Toleranzbereichs."" ""Sensor-ID (%sensorid)""";"-i 240  -d  ""Kühlung des %device in Ordnung, Temperatur des Arbeitsspeichers von %device ist innerhalb des Toleranzbereichs."" ""Sensor-ID (%sensorid)""";"ILO Host Memory-Temp Fehler";"ILO Host Memory-Temp i.O.";"2733";;"2187"
"999HOSTADMIN";"SNMP HPE ProLiant Systemzustand";"-i 230  -d  ""Lüfter des %device ist defekt."" ""Sensor-ID (%sensorid)""";"-i 240  -d  ""Lüfter des %device ist in Ordnung."" ""Sensor-ID (%sensorid)""";"ILO Host Lüfter Fehler";"ILO Host Lüfter i.O.";"2733";;"2187"
"999HOSTADMIN";"SNMP HPE ProLiant Systemzustand";"-i 230  -d  ""Kühlung des %device nicht ordnungsgemäß, Temperatur der CPU von %device ist außerhalb des Toleranzbereichs."" ""Sensor-ID (%sensorid)""";"-i 240  -d  ""Kühlung des %device in Ordnung, Temperatur der CPU von %device ist innerhalb des Toleranzbereichs."" ""Sensor-ID (%sensorid)""";"ILO Host CPU-Temp Fehler";"ILO Host CPU-Temp i.O.";"2733";;"2187"
"999HOSTADMIN";"SNMP HPE ProLiant Systemzustand";"-i 230  -d  ""Kühlung des %device nicht ordnungsgemäß, Temperatur der CPU von %device ist außerhalb des Toleranzbereichs."" ""Sensor-ID (%sensorid)""";"-i 240  -d  ""Kühlung des %device in Ordnung, Temperatur der CPU von %device ist innerhalb des Toleranzbereichs."" ""Sensor-ID (%sensorid)""";"ILO Host CPU-Temp Fehler";"ILO Host CPU-Temp i.O.";"2733";;"2187"
"999HOSTADMIN";"SNMP HPE ProLiant Systemzustand";"-i 230  -d  ""Ein Netzteil des %device ist defekt oder stromlos."" ""Sensor-ID (%sensorid)""";"-i 240  -d  ""Ein Netzteil des %device ist in Ordnung."" ""Sensor-ID (%sensorid)""";"ILO Host Netzteil Fehler";"ILO Host Netzteil i.O.";"2733";;"2187"
"999HOSTADMIN";"SNMP HPE ProLiant Systemzustand";"-i 230  -d  ""Ein Netzteil des %device ist defekt oder stromlos."" ""Sensor-ID (%sensorid)""";"-i 240  -d  ""Ein Netzteil des %device ist in Ordnung."" ""Sensor-ID (%sensorid)""";"ILO Host Netzteil Fehler";"ILO Host Netzteil i.O.";"2733";;"2187"

We are using the following versions:

PSVersion : 5.1.17763.2931 PSEdition : Desktop OS : Microsoft Windows Server 2019 Standard PrtgAPIVersion : 0.9.18 Culture : de-DE CLRVersion : .NET Framework 4.8 (528049) PrtgVersion : 22.4.80.1553 PrtgLanguage : german.lng

What are we doing wrong?

Due Dilligance

lordmilko commented 1 year ago

On one line you have

# The following sets the variable $_myNotificationTrigger_1 to a string
$_myNotificationTrigger_1="NotificationTriggerOn"

And then you have

# The following sets $_myNotificationTrigger_1 to either a collection of NotificationTrigger objects or another string

try{$_myNotificationTrigger_1 = ($_Sensors.item($i) | Get-NotificationTrigger)}
catch{$_myNotificationTrigger_1="Fehler beim auslesen" }

and then on another line you have

# The following calls the "item" method on $_myNotificationTrigger_1 - which is a string
$_myNotificationTrigger_1.item($j).Name

Sometimes $_myNotificationTrigger_1 is a string, sometimes it is an array. When it's a string, strings don't have an item method, hence you are getting this error.

I would also note you are making things harder on yourself by casting lists to ArrayList and using the item method. You can simply use lists as follows

# Declaring an empty list
$myList = @()

# Adding an item to our list
$myList += 3

# Accessing the item in our list
$myList[0]
FSGermany commented 1 year ago

Hello lordmilko,

thank you for your advice. I will rewrite the script and try again.