Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #requires -version 4.0
- # ___ ___ _____ ___
- #| _ \ _ \_ _/ __|
- #| _/ / | || (_ |
- #|_| |_|_\ |_| \___|
- # Pause Sensors that are paused by a license indefinitely
- # ================================
- # This script will scan your PRTG for sensors that are paused due to licensing and pause them indefinitely.
- # This way, users are able to create sensors beyond their actual group limit and don't mess up with the overall sensor count.
- #
- # Version History
- # ----------------------------
- # 1.0 Initial Release
- ##################################
- <#
- .SYNOPSIS
- This script will scan your PRTG for sensors that are paused due to licensing and pause them indefinitely.
- .DESCRIPTION
- Installation:
- 1. Save the script as PRTG-PauseUnlicensedSensors.ps1 under <PRTG Application folder>\Custom Sensors\EXE\
- 2. Create a new EXE/SCRIPT Sensor on the local probe device
- 3. Create a new EXE/Script sensor and select the PRTG-PauseUnlicensedSensors.ps1 script
- -prtgHostName <your prtg url, e.g. http://prtg.acme.com or https://prtg.acme.com>
- -prtgPort <the port your PRTG installation is using>
- -prtgUserName <a user with read/write permissions to the device, or a PRTG administrator>
- -prtgPasshash <the passhash of the above user>
- #>
- param(
- [string]$prtgHostName = "<your prtg url, e.g. http://prtg.acme.com or https://prtg.acme.com>",
- [string]$prtgPort = 80,
- [string]$prtgUsername = "prtgadmin",
- [string]$prtgPasshash = "12345678",
- [switch]$verbose = $true
- )
- [int]$global:counter = 0;
- # this will output debug messages to the console
- function Console-ShowMessage([string]$type,$message){
- if($verbose){
- Write-Host ("[{0}] " -f (Get-Date)) -NoNewline;
- switch ($type){
- "success" { Write-Host " success " -BackgroundColor Green -ForegroundColor White -NoNewline; }
- "information" { Write-Host " information " -BackgroundColor DarkCyan -ForegroundColor White -NoNewline; }
- "warning" { Write-Host " warning " -BackgroundColor DarkYellow -ForegroundColor White -NoNewline; }
- "error" { Write-Host " error " -BackgroundColor DarkRed -ForegroundColor White -NoNewline; }
- default { Write-Host " notes " -BackgroundColor DarkGray -ForegroundColor White -NoNewline; }
- }
- Write-Host (" {0}{1}" -f $message,$Global:blank)
- }
- }
- # This function will retrieve all sensors that are paused by license
- function This-RetrievePausedSensors(){
- $url = [string]::Format("{0}:{1}/api/table.json?content=sensors&output=json&columns=objid,sensor,status&filter_status=11&username={2}&passhash={3}",$prtgHostName, $prtgPort, $prtgUserName, $prtgPasshash);
- # call the URL and store the content, we need the actual ID of the new sensor to change the settings and unpause it
- $request = (Invoke-WebRequest -Uri $url)
- $sensors = ($request.Content | ConvertFrom-Json).sensors
- $global:counter = $sensors.count;
- Foreach ($sensor in $sensors)
- { This-PauseUnlicensedSensors -sensorName $sensor.sensor -sensorId $sensor.objid; }
- }
- # This function will generate the PRTG Metascan Items for the VMs in XML format
- function This-PauseUnlicensedSensors {
- param([string]$sensorName,[int]$sensorId)
- $message = "Sensor paused indefinitely by License Check sensor."
- try{
- Console-ShowMessage "information" ([string]::Format("Pausing Sensor {0} with ID {1}",$sensorName,$sensorId));
- # create the URL for duplicating the original sensor
- $url = [string]::Format("{0}:{1}/api/pause.htm?id={2}&pausemsg={3}&action=0&username={4}&passhash={5}",
- $prtgHostName, $prtgPort, $sensorId, $message, $prtgUserName, $prtgPasshash);
- # call the URL and store the content, we need the actual ID of the new sensor to change the settings and unpause it
- $request = [System.Net.WebRequest]::Create($url);
- $request.AllowAutoRedirect = $false
- $response=$request.GetResponse();
- If ($response.StatusCode -eq 200)
- {
- $response.GetResponseHeader("Location") -match '\d{3,}' | Out-Null;
- $newSensorID = $Matches[0];
- Console-ShowMessage -type "success" -message "Sensor paused succesfully!";
- }
- Else
- { Console-ShowMessage "error" "Pausing the sensor failed. PRTG returned: $($response.StatusCode)"; }
- }
- catch{ return $false; }
- }
- This-RetrievePausedSensors
- Write-Host ([string]::Format("{0}:{0} sensor(s) were paused in the last run.", $global:counter));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement