For anyone looking for an example of how to change the name of hundreds of devices using powershell:
param(
[Parameter(Mandatory=$false)][string]$Uri = 'https://prtg.example.org',
[Parameter(Mandatory=$false)][string]$UserName = 'llingnau',
[Parameter(Mandatory=$false)][string]$Passhash = '123456789',
[int]$Timeout = "10"
)
$b = Invoke-WebRequest -Uri "$($Uri)/api/table.xml?content=devices&output=xml&columns=objid,name,host&id=0&username=$($UserName)&passhash=$($Passhash)" -TimeoutSec $Timeout -UseBasicParsing -ErrorAction SilentlyContinue
$xml = [xml]$b.Content
$nodes = $xml.SelectNodes("//item")
foreach ($node in $nodes) {
Write-Output "Parsing: $($node.name)"
$a = Invoke-WebRequest -Uri "$($Uri)/api/rename.htm?id=$($node.objid)&value=$($node.name.Replace('olddomain.tld','newdomain.tld'))&username=$($UserName)&passhash=$($Passhash)" -TimeoutSec $Timeout -UseBasicParsing -ErrorAction SilentlyContinue
}
This is the minimum example to make this work and obviously comes with no support whatsoever.
The use-case for this script was renaming devices to a different tld, so from "device.olddomain.tld" to "newdomain.tld" (for demonstrations purposes). Obviously doing only this is a bad idea because it would mean you have device names that no longer match the configured addresses which would lead to some confusion, but the point of the script is showing a very simple way of updating device names, one could now extend the script to adjust any other property available to devices.
Line 8 will simply query all devices (up to 500, which can be increased with the limit parameter)
And the loop in lines 13-17 will set a new name based on the content of $($node.name) - In this particular case, doing the Replace('olddomain.tld','newdomain.tld') to change part of the name.
There's no error validation and the script will also try to update the name of names that haven't changed, which isn't ideal but works for a proof of concept of interacting with the API. Ideally one would only do the /api/rename call for object's who's names actually changed.
Best Regards,
Luciano Lingnau [Paessler]
Add comment