Last active
September 12, 2016 10:25
-
-
Save markwragg/18ef05965e2f6b2cf0ea150e7c52d487 to your computer and use it in GitHub Desktop.
Powershell script to delete multiple devices from PRTG where a name and/or message text is partially matched and where the device is paused and has been paused for longer than x days. The script uses -whatif and -confirm for use with the deletion step.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding(SupportsShouldProcess = $true,ConfirmImpact='High')] | |
Param( | |
$PRTGURL = "https://{your PRTG URL}/api", | |
$Auth = "username={your PRTG user}&passhash={your PRTG password hash}", | |
$Name = "", | |
$Message = "", | |
[int]$DaysPaused = 7 #Use -1 to filter for where date is unknown and 0 to return all | |
) | |
$Devices = $null | |
$Devices = (Invoke-RestMethod "$PRTGURL/table.json?content=devices&output=json&columns=objid,device,status_raw,message&count=99999&$Auth").devices | |
$Devices = $Devices | Where-Object {($_.status_raw -eq 7) -and ($_.device -notlike "*Template*")} | |
#Filtering list based on provided parameters | |
If ($Name -ne "") { $Devices = $Devices | Where-Object {$_.device -like "*$Name*"} } | |
If ($Message -ne "") { $Devices = $Devices | Where-Object {$_.message -like "*$Message*"} } | |
$Devices | ForEach-Object { | |
$DatePaused = ($_.message | Select-String -Pattern "(?<=Paused at )(.*?)(?= by)" -AllMatches | % { $_.Matches } | % { $_.Value }) | |
If ($DatePaused) { $DatePaused = [DateTime]::Parse($DatePaused,([Globalization.CultureInfo]::CreateSpecificCulture("$((get-culture).name)"))) } | |
$_ | Add-Member –MemberType NoteProperty –Name "paused" –Value $DatePaused -Force | |
} | |
If ($DaysPaused -gt 0) { $Devices = $Devices | Where-Object {($_.paused -lt $(get-date).adddays(-$DaysPaused)) -and ($_.paused -is [System.DateTime])} } | |
If ($DaysPaused -eq -1) { $Devices = $Devices | Where-Object {$_.paused -isnot [System.DateTime]} } | |
#Remove HTML from Messages | |
$Devices | ForEach-Object {$_.message = $_.message -replace '<[^>]+>',''} | |
#Output list of devices and total to console to assist confirmation | |
$Devices | Select device, paused, message | Sort paused | FT | |
Write-Warning "$($Devices.Count) devices will be deleted." | |
$i = 1 | |
$Devices | ForEach-Object { | |
If ($PSCmdlet.ShouldProcess("$($_.device)","PRTG: Delete Object")) | |
{ | |
Write-Progress -Activity "Deleting $($_.device)" -Status "$i of $($Devices.Count)" -PercentComplete ($i/($Devices.Count)*100) | |
$Delete = Invoke-RestMethod "$PRTGURL/deleteobject.htm?id=$($_.objid)&approve=1&$Auth" | |
} | |
$i++ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment