Skip to content

Instantly share code, notes, and snippets.

@stephanlinke
Last active February 18, 2022 19:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephanlinke/90ff0b7cfaf77ff02efe2d88a648fcec to your computer and use it in GitHub Desktop.
Save stephanlinke/90ff0b7cfaf77ff02efe2d88a648fcec to your computer and use it in GitHub Desktop.
[Import Devices From CSV List] This script allows you to import devices from a CSV into PRTG, including their lat/lon location. #PRTG #management
#Requires -Version 4
# ___ ___ _____ ___
#| _ \ _ \_ _/ __|
#| _/ / | || (_ |
#|_| |_|_\ |_| \___|
# NETWORK MONITOR
# ------------------
# (c) 2014 Paessler AG
# this will create new devices including lat and lon. from a CSV.
# Make sure it looks like this and is named devices.csv within the same directory as the script:
<#
DeviceName;DeviceAddress;TargetGroup;LocationName;Longitude;Latitude
Server1;server2.network.com;1;Nuremberg;11.1296;49.4741
#>
$Server = "<insert-your-prtgserver>"
$Username = "<insert-administrative-prtg-user>"
$Passhash = "<insert-above-users-passhash-from-its-account-settings>"
$DeviceID = 2042 # Enter the id of a device with no sensors here, you can get it from the URL of the devices overview page
# The list of URLs you want to monitor. Please use the same syntax as the examples.
$DeviceList = (Get-Content("devices.csv") | ConvertFrom-Csv -Delimiter ';');
ForEach($Device in $DeviceList){
# create the sensor
$CreateURL = "http://{0}/api/duplicateobject.htm?id={1}&name={2}&targetid={3}&username={4}&passhash={5}" -f
$Server,
$DeviceID,
$Device.DeviceName,
$Device.TargetGroup,
$Username,
$Passhash
$response = [System.Net.WebRequest]::Create($CreateURL).GetResponse();
# extract the sensor id, every number with at least 4 digits counts as id
$newDeviceID = $response.ResponseUri.Query -match "(\d{4,})";
$newDeviceID = $matches[0];
# modify the device settings to feature latitude and longitude
$UrlLatLon = "http://{0}/api/setlonlat.htm?id={1}&location={2}&lonlat={3},{4}&username={5}&passhash={6}" -f
$Server,$newDeviceID,$Device.LocationName,$Device.Longitude,$Device.Latitude,$Username,$Passhash
Invoke-WebRequest -Uri $UrlLatLon | Out-Null
# Unpause the new sensor
Invoke-WebRequest "http://$Server/api/pause.htm?id=$newDeviceID&action=1&username=$username&passhash=$passhash" > $null
Write-Host "Device $($Device.DeviceName) created successfully. Should be up and running..."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment