Home
Remove devices from PRTG automatically using PowerShell
PRTG (PowerShell) Friday, 19 June 2020 by paul

Script to delete devices from PRTG which have a ping sensor which has downtime over a specified number of days. This can be used to automatically remove old devices from a PRTG server.

############################################################################################################################################ 
# Script that deletes old devices on PRTG Remote server
############################################################################################################################################ 

add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
Write-host "Delete Old Devices on PRTG Server"

$username="username"
$passhash="passwordhash"
$server = "prtg.domain.com"
$days = 30
$logfile = $PSScriptRoot+"\prtg-delete-sensors.log"
$url = "https://$server/api/table.json?content=devices&columns=objid,device,group&output=json&login=$username&passhash=$passhash&count=9999"
$deletenow = $false
# Change line above to $deletenow = $true to delete the devices that have downtime according to ping sensors

Write-host "Server: "$server
Write-host "URL: "$url

# Get list of devices
try { $results = (Invoke-RestMethod -uri $url) }
catch { exit }

Add-Content $logfile -Value "##### Script started: $(Get-Date)  #####"

$body = "Devices which have downtime over $days days:" + [char]13
$count_devices=0
$count_sensors=0
$count_deleted_devices=0
foreach($device in $results.devices)
		{
			$count_devices=$count_devices+1
			$deviceid = $device.objid
			$devicename = $device.device
			$url = "https://$server/api/table.json?content=sensors&id=$deviceid&columns=objid,device,sensor,downtimesince,status_raw&output=json&login=$username&passhash=$passhash&count=100"
			Write-Host "Device:"$devicename" ("$deviceid")"
			#Write-Host "Url:" $url
			#$device
			# Get sensors for device
			try { $results2 = (Invoke-RestMethod -uri $url) }
			catch { exit }
			$deleted = $false
			foreach($sensor in $results2.sensors)
			{
				$count_sensors=$count_sensors+1
				#Write-Host " Sensor "$sensor.sensor" ("$sensor.objid")"
				$objid = $sensor.objid
				If ( $deleted -ne $true ) {
					#$sensor
					If ( $sensor.sensor -like "*Ping*") {
						$downtime=$sensor.downtimesince.Split(" ")
						$downtimei = $downtime[0] -as [int]
						$dt = $downtimei | out-string
						#Write-Host "" $sensor.group " Sensor="$sensor.sensor "downtime="$sensor.downtimesince " days="$downtimei " unit="$downtime[1]
						If ( $downtimei -gt $days -and $downtime[1] -eq "d") {
							$str = "Ping sensor down over threshold $device.device - $objdid days down: $dt"
							Write-Host $str -foreground red
							$parentid = ([xml]$results3.Content).prtg.result
							Add-Content $logfile -Value $str
							If ($deletenow -eq $true) {
								Write-Host "Deleting device "$devicename" id:"$deviceid -foreground green
								$url = "https://$server/api/deleteobject.htm?id=$deviceid&approve=1&login=$username&passhash=$passhash"
								$results5 = (Invoke-RestMethod -uri $url)
							}
							$deleted = $true
							$count_deleted_devices=$count_deleted_devices+1
						}
					}
					}
				else {
					Write-Host "Marked for deletion - " $objdid
				}
			}
        }

$str = "$count_devices devices checked. ($count_deleted_devices deleted)"
Write-Host $str 
Add-Content $logfile -Value "Devices checked: $count_devices"
Add-Content $logfile -Value "Devices deleted: $count_deleted_devices"
Add-Content $logfile -Value "Sensors checked: $count_sensors"
Add-Content $logfile -Value "##### Script finished: $(Get-Date)  #####"

 


Add Comment
No Comments.