Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- param(
- [string]$prtgHostName = "", # your-prtg-host, e.g. http://prtg.astrata.eu
- [string]$prtgPort = 80, # the port of your prtg webinterface
- [string]$prtgUserName = 'prtgadmin', # the prtg user you'd like to use
- [string]$prtgPasshash = 12345678, # the users passhash, can be obtained from his prtg account page
- [int]$prtgDeviceId = 8216, # the device you want to add the sensors to
- [int]$prtgTemplateSensor = 8240, # the ID of a http sensor you already created
- [string]$urlFile = "C:\temp\url.txt" # the path to your url list file
- )
- # this will output debug messages to the console
- function Console-ShowMessage([string]$type,$message){
- 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)
- }
- # @Output: Create HTTP Push (Advanced) sensors
- #
- # This function will generate the PRTG Metascan Items for the VMs in XML format
- function This-AddHTTPPushSensors {
- param([string]$sensorName)
- Console-ShowMessage "information" "Starting sensor creation for $($sensorName)";
- # create the URL for duplicating the original sensor
- $url = [string]::Format("{0}:{1}/api/duplicateobject.htm?id={2}&name={3}&targetid={4}&username={5}&passhash={6}",
- $prtgHostName, $prtgPort, $prtgTemplateSensor, $sensorName, $prtgDeviceId, $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 "Redirect")
- {
- $response.GetResponseHeader("Location") -match '\d{3,}' | Out-Null;
- $newSensorID = $Matches[0];
- Console-ShowMessage -type "success" -message "Sensor created successfully. New sensor ID: $($newSensorID)";
- }
- Else
- { Console-ShowMessage "error" "Sensor creation failed. PRTG returned: $($response.StatusCode)"; }
- # modify the token to resemble the URL of the device
- $url = [string]::Format("{0}:{1}/api/setobjectproperty.htm?id={2}&name={3}&value={4}&username={5}&passhash={6}",
- $prtgHostName, $prtgPort, $newSensorID, "name", $sensorname, $prtgUserName, $prtgPasshash);
- $UpdateSensorResult = (Invoke-WebRequest $url)
- if($UpdateSensorResult.StatusCode -eq "200")
- { Console-ShowMessage -type "success" -message "Sensor has been renamed succesfully"; }
- # modify the token to resemble the URL of the device
- $url = [string]::Format("{0}:{1}/api/setobjectproperty.htm?id={2}&name={3}&value={4}&username={5}&passhash={6}",
- $prtgHostName, $prtgPort, $newSensorID, "httpurl", $sensorname, $prtgUserName, $prtgPasshash);
- $UpdateSensorURL = (Invoke-WebRequest $url)
- # resume the new sensors
- $url = [string]::Format("{0}:{1}/api/pause.htm?id={2}&action=1&username={3}&passhash={4}",
- $prtgHostName, $prtgPort, $newSensorID, $prtgUserName, $prtgPasshash);
- $StartSensorURL = (Invoke-WebRequest $url)
- if($UpdateSensorURL.StatusCode -eq "200")
- { Console-ShowMessage -type "success" -message "Sensor has been renamed succesfully"; }
- if($UpdateSensorResult.StatusCode -eq "200")
- { Console-ShowMessage -type "success" -message "Changed sensor name to URL."; }
- if($UpdateSensorURL.StatusCode -eq "200")
- { Console-ShowMessage -type "success" -message "Sensor URL has been changed succesfully."; }
- }
- $lines = Get-Content $urlFile
- foreach ($line in $lines) {
- This-AddHTTPPushSensors -sensorName $line
- }
- Console-ShowMessage -type information -message "Created $($lines.Count) sensors!"
Advertisement
Add Comment
Please, Sign In to add comment