lordmilko / PrtgAPI

C#/PowerShell interface for PRTG Network Monitor
MIT License
301 stars 37 forks source link

bulk renaming device name #189

Closed lpelektronik closed 3 years ago

lpelektronik commented 3 years ago

Hi, I have bulk imported devices from .csv and started auto-discovery with template. As soon as it was finished, name changed to full DNS name which is confusing. Is there any way how to change these names back to names I have in csv using prtgapi? as an example: Imported name: MyMachine001 Name after auto-discovery: mymachine001.my.domain.com (MyMachine001)

is it possible to remove everrythink what is before and after MyMachine001? or is it simply possible to replace new auto discovered name with Imported name?

Thank you in advance. Ladi

lordmilko commented 3 years ago

Hi @lpelektronik,

Unfortunately this is simply an annoying side effect of PRTG's auto-discovery process. In order to work around this you'll either need to have a script re-set the device names to what they're meant to be, or use a regular expression to automatically rename all devices that appear to have auto-discovery names back to what their original names were before PRTG mangled them.

In your example above, you have a device that was called MyMachine001 that PRTG renamed to mymachine001.my.domain.com (MyMachine001). Assuming you don't have any devices that contain a pair of brackets in their names, you can automatically discover and rename all devices with auto-discovery names after the auto-discoveries have completed with the following one-liner

$expr = ".+\((.+)\)"; get-device|where { ($_.name -replace $expr,'$1') -ne $_.name }|foreach { $_ | rename-object ($_.name -replace $expr,'$1') -whatif}

By default the command above will simply preview the names it will modify. If it includes objects you don't want to mess with you might want to filter those out prior to piping into foreach. You can make it run for real by removing the -WhatIf near the end of the command

I have a special PowerShell script I've written to automatically go through and cleanup various auto-discovery artifacts, so I would recommend employing a similar technique