What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

Mass Delete Sensors

Votes:

0

Hi,

Over the weekend I added a bunch of auto-discovery groups on /24 subnets. This is a lot of devices and a lot of sensors. eg. I need to remove 4300 sensors.

If I know all the sensor ID's that I don't want is there an API call that will delete all the ID's provided? (Similar to if I selected all sensors using the multi edit check box and pressing delete) Currently I am running the delete call on each ID but this could take up to 12 hours and is freezing my PRTG.

Thanks,

Matt

api delete sensors

Created on Apr 3, 2018 8:36:06 AM



5 Replies

Votes:

0

Hi Matthew,

If you added a bunch of new groups, can't you just delete those groups which will in turn delete all the sensors under them? Or did you potentially convert some existing groups into auto-discovery groups and now want to purge everything new that was added?

Since you mentioned you use PrtgAPI, you can do it like this

$ids = 1001,1002,1003,1004
Get-Sensor -Id $ids | Remove-Object -Force

If you didn't know the IDs of the sensors you wanted to delete, but knew you hadn't added any new sensors since then, you could also query for all sensor IDs greater than a certain number

flt id greaterthan 4000 | Get-Sensor | Remove-Object -Force

By default Remove-Object will attempt to remove all 4300 sensors at once. If PRTG doesn't like this however you can either pipe in a smaller number of items at once, or instruct Remove-Object to remove each sensor one at a time

Get-Sensor -Id $ids | Remove-Object -Force -Batch:$false

Of course, you will also want to remove the new device and group objects that were added as well

Regards,

lordmilko

Created on Apr 3, 2018 9:37:05 AM



Votes:

0

Hi lordmilko,

I want the groups and I want the devices in the groups. I only want the ping sensors though. My groups are set up like this:

  • Site Group
    • Gateway Device
    • Routers Group
    • Switches Group
    • VLAN1 Group
    • VLAN2 Group

I have about 400 site groups and each has a VLAN 1 and A VLAN 2 group. My VLAN 1 and 2 groups have auto discovered 1000's of sensors over the weekend and I want to remove all sensors in those groups except for ping sensors.

Thank for your suggestions. I was getting errors trying to pass a variable through to remove-object so I rewrote my code as a one liner and it worked perfectly if I run it for each site individually (Now trying to find out if I can iterate it over each site group instead of over the main group). If I run it on the group all of my sites are in it hangs.

Get-Group -Id 26292 | Get-Sensor | where{($_.Name -ne "ping") -and ($_.Tags -match "vlan")} | Remove-Object -WhatIf

What didn't work was:

$sensors = Get-Sensor -Tags "vlan170"
foreach ($s in $sensors)
{
    if($s.Name -ne "PING")
    {
        $SensorRemovalList += $s
    }
}
$SensorRemovalList | Remove-object -WhatIf

Any ideas what I'm doing wrong here?

Created on Apr 3, 2018 10:34:12 AM



Votes:

0

Here is the code that worked for me:

$groups = Get-Group -Id 26292 | Get-Group | where{($_.Name -notmatch "Routers") -and ($_.Name -notmatch "Switches") -and ($_.Name -notmatch "vlan")}

foreach($g in $groups)
{
    Write-host -ForegroundColor DarkYellow $g.Name
    $ID = $g.id
    Get-Group -Id $ID | Get-Sensor | where{($_.Name -ne "ping") -and ($_.Tags -match "vlan")} | Remove-Object -Force -Verbose
}

Created on Apr 3, 2018 11:22:12 AM



Votes:

0

If you specify -Verbose after each cmdlet you will probably find it hasn't hung, and will be able to see the requests that are being executed

Potentially your issue is that you haven't declared your $SensorRemovalList variable as an array.

I imagine you would have gotten an error similar to the following?

Method invocation failed because [PrtgAPI.Sensor] does not contain a method named 'op_Addition'.
At line:5 char:5
+     $sensors += $group | Get-Sensor -Tags vlan170 | where name -ne "P ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Here are the results of some testing I did

PS C:\> get-sensor -tags vlan170

Name                         Id     Device               Group                     Probe                 Status        
----                         --     ------               -----                     -----                 ------        
Ping                         7810   NY-BRONX             NY1                       New York              Warning       
CPU Load                     7811   NY-BRONX             NY1                       New York              Unknown     

When I rewrote your script how I would write it, I got the following

$groups = Get-Group

$sensors = @()

foreach($group in $groups)
{
    $sensors += $group | Get-Sensor -Tags vlan170 | where name -ne "Ping"
}

$sensors | Remove-Object -WhatIf
What if: Performing the operation "Remove-Object" on target "'CPU Load' (ID: 7811)".
PS C:\> $sensors

Name                         Id     Device               Group                     Probe                 Status        
----                         --     ------               -----                     -----                 ------        
CPU Load                     7811   NY-BRONX             NY1                       New York              Unknown  

If you are getting an error passing something to Remove-Object, you are passing the wrong type of something. General PowerShell troubleshooting steps to take in this scenario include

1. Dump the $SensorRemovalList variable. Does it even contain anything?

2. Get the type of the $SensorRemovalList object. Was it the type you expected?

3. If $SemsorRemovalList is an array, get the type of an element of the array. In this scenario, it should be a Sensor

PS C:\> $sensors.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                                                                                              
-------- -------- ----                                     --------                                                                                                                                                                                              
True     True     Object[]                                 System.Array                                                                                                                                                                                          



PS C:\> $sensors[0].GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                                                                                              
-------- -------- ----                                     --------                                                                                                                                                                                              
True     False    Sensor                                   PrtgAPI.Objects.Shared.SensorOrDeviceOrGroupOrProbe    

Created on Apr 3, 2018 11:25:06 AM



Votes:

0

Lordmilko you are right. I did not declare my $SensorRemovalList variable as an array. I have to say sir you are a genius.

Created on Apr 3, 2018 1:09:33 PM




Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.