lordmilko / PrtgAPI

C#/PowerShell interface for PRTG Network Monitor
MIT License
305 stars 38 forks source link

Unable to get sensors based on tags #331

Closed MPrica12 closed 1 year ago

MPrica12 commented 1 year ago

Describe the bug

Hello,

So I currently am trying to use the PrtgAPI to gather all of the sensors present in our system that do not have the following tags: "monitoring-event" or "incident-p".

The issue is that whenever I try to do it using a simple where check the variable always stores ALL of the sensors in Prtg, so no filtering is applied.

I have tried 2 different instances for comparison, -like and -notlike, what is weird is the fact that using -like works but -notlike always retrieves every sensor, below is the command I am using:

Steps to reproduce

#Get all of the sensors that do not have any of these two tags
$Sensors = Get-Sensor | Where {$_.Tags -notlike "monitoring-event"-or $_.Tags -notlike "incident-p*"}

#Get all of the sensors that have these tags
$Sensors = Get-Sensor | Where {$_.Tags -like "monitoring-event"-or $_.Tags -like "incident-p*"}

What is the output of 'Get-PrtgClient -Diagnostic'?

PSVersion      : 7.3.3
PSEdition      : Core
OS             : Microsoft Windows 10.0.19041
PrtgAPIVersion : 0.9.18
Culture        : ro-RO
CLRVersion     : .NETCoreApp,Version=v7.0
PrtgVersion    : 23.1.82.2175
PrtgLanguage   : english.lng

Additional context

No response

lordmilko commented 1 year ago

The Tags property is an array. The -like and -notlike operators apply to strings. To analyze an array, consider using the -in and -notin filters

# Get all sensors that DON'T have the "monitoring-event" tag
$sensors = Get-Sensor | where { "monitoring-event" -notin $_.Tags }

If you need to filter for sensors containing a tag that matches a wildcard expression, this is a bit more complicated

# Get all sensors that DON'T have any tags like "incident-p*"
$sensors = Get-Sensor | where { !($_.Tags|where { $_ -like "incident-p*" }) }

Combining these two techniques, you can do the following

# Get all sensors that don't contain the tag "monitoring-event" or a tag like "incident-p*"
Get-Sensor | where { ("monitoring-event" -notin $_.Tags) -and !($_.Tags|where { $_ -like "incident-p*" }) }
# Get all sensors that do contain the tag "monitoring-event" or a tag like "incident-p*"
Get-Sensor | where { ("monitoring-event" -in $_.Tags) -or ($_.Tags|where { $_ -like "incident-p*" }) }
MPrica12 commented 1 year ago

The Tags property is an array. The -like and -notlike operators apply to strings. To analyze an array, consider using the -in and -notin filters

# Get all sensors that DON'T have the "monitoring-event" tag
$sensors = Get-Sensor | where { "monitoring-event" -notin $_.Tags }

If you need to filter for sensors containing a tag that matches a wildcard expression, this is a bit more complicated

# Get all sensors that DON'T have any tags like "incident-p*"
$sensors = Get-Sensor | where { !($_.Tags|where { $_ -like "incident-p*" }) }

Combining these two techniques, you can do the following

# Get all sensors that don't contain the tag "monitoring-event" or a tag like "incident-p*"
Get-Sensor | where { ("monitoring-event" -notin $_.Tags) -and !($_.Tags|where { $_ -like "incident-p*" }) }
# Get all sensors that do contain the tag "monitoring-event" or a tag like "incident-p*"
Get-Sensor | where { ("monitoring-event" -in $_.Tags) -or ($_.Tags|where { $_ -like "incident-p*" }) }

Thank you so much for this!! It worked as expected