I am not really sure where I should post this, as its not a question, its just sharing something that I have been working on:
I have put together a Powershell module so I can manage my PRTG setup.
It allows me to quickly do the following: 1, update massive groups of devices with additional sensors. 2, import devices from CSV and assigned to group 3, report on all sensors that are in a certain state (useful for a daily overall status email)
There are examples in the code.
I am certainly no Powershell Guru, In fact this is the first thing I have written in Powershell.
######ignore invalid SSL Certs########## 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 ######setup details, access related. #$auth = "username=prtgadmin&passhash=618951066" $auth = "username=api_access&passhash=1982551677" $PRTGHost = "10.254.199.20" ############################################## ############Get-prtgSensorInGroup########### ############################################## # will return ALL sensors listed in PRTG, for a given Group (via its ID) (limited to 2500) #default is the root (ID=0) #its important to remember that a "Group" is also a device, and probe etc. #eg #Get-prtgSensorInGroup 8011 function Get-prtgSensorInGroup([string]$StartingID=0) { $url = "https://$PRTGHost/api/table.xml?content=sensors&output=csvtable&columns=objid,probe,group,device,sensor,status,message,lastvalue,priority,favorite,tags&id=$StartingID&count=2500&$auth" $request = Invoke-WebRequest -Uri $url -MaximumRedirection 0 -ErrorAction Ignore convertFrom-csv $request } # end function ############################################## ############Get-prtgDevicesInGroup#################### ############################################## # will return ALL Devices listed in PRTG, for a given Group (via its ID) (limited to 2500) #default is the root (ID=0) #if you want to find 1 device(ID=8011), then use: Get-prtgDevicesInGroup | where {$_.ID -eq 8011} #anther example, seaching for an IP #Get-prtgDevicesInGroup | where {$_.host -eq "10.81.8.36"} #you may also want to return many devices, eg #Get-prtgDevicesInGroup | where {$_.host -like "10.81.8.*"} function Get-prtgDevicesInGroup ([string]$StartingID=0) { #if ($script:devicesCached -eq $false){ $url = "https://$PRTGHost/api/table.xml?content=devices&output=csvtable&columns=objid,probe,groupid,device,host,downsens,partialdownsens,downacksens,upsens,warnsens,pausedsens,unusualsens,undefinedsens,comments&id=$StartingID&count=2500&$auth" $request = Invoke-WebRequest -Uri $url -MaximumRedirection 0 -ErrorAction Ignore $getprtgDeviceRet = convertFrom-csv ($request) $getprtgDeviceRet # write-host "got devices from website..." # $script:devicesCached = $true #}else{ # write-host "got groups from cache..." # $getprtgDeviceRet #} } # end function ############################################## ############Get-prtgGroupsInGroup#################### ############################################## # will return ALL Groups listed in PRTG, for a given Group (via its ID) (limited to 2500) #default is the root (ID=0) #will will list all the group recurively under the listed group (ie, not JUST the children) #eg, return al lthe groups in the local probe # get-prtgGroupsInGroup 1 function get-prtgGroupsInGroup ([string]$StartingID=0) { $url = "https://$PRTGHost/api/table.xml?content=groups&output=csvtable&columns=objid,probe,group,name,downsens,partialdownsens,downacksens,upsens,warnsens,pausedsens,unusualsens,undefinedsens&count=2500&id=$StartingID&$auth" $request = Invoke-WebRequest -Uri $url -MaximumRedirection 0 -ErrorAction Ignore $getprtgGroupret = convertFrom-csv ($request) #write-host "got groups from website..." $getprtgGroupret } # end function ############################################## ############Set-prtgDeviceProperty############ ############################################## # Sets a property of a device function Set-prtgDeviceProperty ([string]$DeviceID,[string]$PropertyName,[string]$Value) { $url = "https://$PRTGHost/api/setobjectproperty.htm?id=$DeviceID&name=$PropertyName&value=$Value&$auth" $request = Invoke-WebRequest -Uri $url -MaximumRedirection 0 -ErrorAction Ignore } # end function ############################################## ############Add-prtgSensorToDevice############ ############################################## # add's a sensor to a device (good for updating templates- adding a new sensor to 100's of devices) #eg add one sensor to a group of devices #add-prtgSensorToDevice -SensorScriptBlock {Get-prtgSensorInGroup | where {$_.id -eq 8328}} -DevicesScriptBlock {Get-prtgDevicesInGroup 7633} #eg, add sensor 5520 to device 6409 #add-prtgSensorToDevice -SensorScriptBlock {Get-prtgSensorInGroup | where {$_.id -eq 5520}} -DevicesScriptBlock {Get-prtgDevicesInGroup | where {$_.id -eq 6409}} #eg add a pages_left sensor to all devices with a tag of "printer" #TODO function Add-prtgSensorToDevice { [CmdletBinding()] param( [Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$false)] [scriptblock]$DevicesScriptBlock, [Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$false)] [scriptblock]$SensorScriptBlock ) process { if ($SensorScriptBlock -ne $null -and $DevicesScriptBlock -ne $null) { ##loop thru all the devices. $Devices = &([scriptblock]::Create($DevicesScriptBlock)) foreach($Device in $Devices) { $DeviceID = $device.id "DeviceID=$DeviceID" ##Loop thru all the sensors. $Sensors = &([scriptblock]::Create($SensorScriptBlock)) foreach($Sensor in $Sensors) { $SensorID = $Sensor.id "SensorID=$SensorID" $NewName=$Sensor.Sensor $url = "https://$PRTGHost/api/duplicateobject.htm?id=$SensorID&Targetid=$DeviceID&name=$NewName&$auth" $url $request = Invoke-WebRequest -Uri $url -MaximumRedirection 0 -ErrorAction Ignore } # end foreach - Sensor } # end foreach - Device } # end if } # end process } # end function ############################################## ############Add-prtgDevice############ ############################################## # adds a new device in PRTG #example #Add-prtgDevice -NewIP "5.5.5.5" -TemplateID 5682 -DestGroupID 6408 -NewDeviceName "Hi_I_Am_New" #example, import from CSV. where the fields are newip, TemplateID, DestGroupID, NewDeviceName #Import-Csv -Delimiter "`t" -path c:\prtgimport.csv | Add-prtgDevice function Add-prtgDevice { [CmdletBinding()] param( [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)] [string]$NewIP, [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)] [string]$TemplateID, [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)] [string]$DestGroupID, [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)] [string]$NewDeviceName ) Process { #$NeWIP = "2.2.2.2" #$TemplateID = "5539" #$DestGroupID = "5538" #$NewDeviceName = "Server1" "" + "adding sensor$NewIP" #Duplicate the sensor (Server replies with a redirect to new objects webpage, e.g. /sensor.htm?id=10214, parse id 10214 from the URL): $url = "https://$PRTGHost/api/duplicateobject.htm?id=$TemplateID&name=$NewDeviceName&targetid=$DestGroupID&$auth" $request = Invoke-WebRequest -Uri $url -MaximumRedirection 0 -ErrorAction Ignore $newID = $request.Headers.Location.Split("=")[1] #add in the correct IP (host) the new sensor: $url = "https://$PRTGHost/api/setobjectproperty.htm?id=$newID&name=host&value=$newIP&$auth" $request = Invoke-WebRequest -Uri $url -MaximumRedirection 0 -ErrorAction Ignore #Resume monitoring for the new sensor: $url = "https://$PRTGHost/api/pause.htm?id=$newID&action=1&$auth" $request = Invoke-WebRequest -Uri $url -MaximumRedirection 0 -ErrorAction Ignore } #end process } # end function
Add comment