Last active
September 12, 2016 10:25
-
-
Save markwragg/cd9fd02ad539fb5dd4adfa6add0909bc to your computer and use it in GitHub Desktop.
Powershell module for managing retrieving and removing paused devices from PRTG : http://wragg.io/clean-up-paused-devices-from-prtg-with-powershell/
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
Function Get-PRTGPausedDevice{ | |
[CmdletBinding()] | |
Param( | |
$PRTGURL = "prtg.yourcompany.com", | |
$Username = "youruser", | |
$Passhash = "yourpasshash", | |
$Name, | |
$Message, | |
[int]$DaysPaused #Use -1 to filter for where date is unknown and 0 to return all | |
) | |
Begin{ | |
$Devices = $null | |
} | |
Process{ | |
$Devices = (Invoke-RestMethod "https://$PRTGURL/api/table.json?content=devices&output=json&columns=objid,device,status_raw,message&count=99999&username=$Username&passhash=$Passhash").devices | |
$Devices = $Devices | Where-Object {($_.status_raw -eq 7) -and ($_.device -notlike "*Template*")} | Select objid, device, message, status | |
#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 | |
$_ | Add-Member –MemberType NoteProperty –Name "PRTGURL" –Value $PRTGURL -Force | |
$_ | Add-Member –MemberType NoteProperty –Name "username" –Value $Username -Force | |
$_ | Add-Member –MemberType NoteProperty –Name "passhash" –Value $Passhash -Force | |
$_.message = $_.message -replace '<[^>]+>','' | |
} | |
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]} } | |
} | |
End{ | |
$Devices.PSObject.TypeNames.Insert(0,'PRTG.PausedDevice') | |
$defaultDisplaySet = 'device','message','paused' | |
$defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet(‘DefaultDisplayPropertySet’,[string[]]$defaultDisplaySet) | |
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet) | |
$Devices | Add-Member MemberSet PSStandardMembers $PSStandardMembers | |
Return $Devices | |
} | |
} | |
Function Remove-PRTGPausedDevice{ | |
[CmdletBinding(SupportsShouldProcess = $true,ConfirmImpact='High')] | |
Param( | |
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$True)]$PRTGURL, | |
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$True)]$Username, | |
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$True)]$Passhash, | |
$Name, | |
$Message, | |
$DaysPaused, #Use -1 to filter for where date is unknown and 0 to return all | |
[int][Parameter(ValueFromPipelineByPropertyName=$True)]$objid | |
) | |
Begin{ | |
$Devices = $null | |
$i = 0 | |
If ($Name -or $Message -or $DaysPaused){ | |
$Devices = (Invoke-RestMethod "https://$PRTGURL/api/table.json?content=devices&output=json&columns=objid,device,status_raw,message&count=99999&username=$Username&passhash=$Passhash").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." | |
} | |
} | |
Process{ | |
If ($Devices){$Input = $Devices} | |
$Input | ForEach-Object { | |
If ($PSCmdlet.ShouldProcess("$($_.device)","PRTG: Delete Object")) | |
{ | |
$i++ | |
Write-Progress -Activity "Removing $($_.device) from $PRTGURL" | |
Invoke-RestMethod "https://$PRTGURL/api/deleteobject.htm?id=$($_.objid)&approve=1&username=$Username&passhash=$Passhash" | out-null | |
} | |
} | |
} | |
End{ | |
Write-Output "$i devices removed." | |
} | |
} | |
Function Remove-PRTGDevice{ | |
[CmdletBinding(SupportsShouldProcess = $true,ConfirmImpact='High')] | |
Param( | |
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$True)]$PRTGURL, | |
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$True)]$Username, | |
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$True)]$Passhash, | |
[Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$True)]$objid | |
) | |
$Count = $input.Count | |
$i = 0 | |
$input | ForEach-Object { | |
If ($PSCmdlet.ShouldProcess("$($_.device)","PRTG: Delete Object")) | |
{ | |
$i ++ | |
Write-Progress -Activity "Removing $($_.objid) : $($_.device) frin $PRTGURL" -Status "$i of $Count" -PercentComplete (($i/$Count)*100) | |
Invoke-RestMethod "https://$PRTGURL/api/deleteobject.htm?id=$($_.objid)&approve=1&username=$Username&passhash=$Passhash" | Out-Null | |
} | |
} | |
Write-Output "$i devices removed." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment