lordmilko / PrtgAPI

C#/PowerShell interface for PRTG Network Monitor
MIT License
305 stars 38 forks source link

Bulk rename of http sensor urls #126

Closed synsof closed 4 years ago

synsof commented 4 years ago

What's going on?

Our organization got sold to a new company and as part of this we need to rename all of our http sensors in prtg. Playing with some powershell variables, I was able to do essentially this with the name property, with no problem.

$params` = get-sensor -id 2877
$params.Name
HTTP ccccc.whatever.com
$rename = $params.Name
$rename
HTTP ccccc.whatever.com
$rename = $rename -replace 'ccccc', `'yyyyy'`
$rename
HTTP yyyyy.whatever.com
$params.Name = $rename
$params.Name
HTTP yyyyy.whatever.com
get-sensor -id 2877 | Set-ObjectProperty Name ``$params.Name

However, when trying to do the exact same thing with the httpurl property, it doesn't work. So, when I do

$params = get-sensor -id 2877
$params.httpurl

Nothing returns, and when I do

$rename = $rename -replace 'ccccc'.'yyyyy'
$rename

All i get is 'https://'

Then finally, when I $params.httpurl = $rename

I get this error:

The property 'httpurl' cannot be found on this object. Verify that the property exists and can be set.
At line:1 char:1
+ $params.httpurl = $rename
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

Please help, I want to be able to run this in bulk instead of going into each http sensor and replacing the characters manually.

Thanks

Due Dilligance Please enter an 'x' between the brackets to indicate you have done these

lordmilko commented 4 years ago

Hi @synsof,

Sensor objects do not have a httpurl property, and their properties also do not exactly represent "parameters" that you'd then specify to the Set-ObjectProperty cmdlet.

Rather, the Overview section on the wiki lists all of the known properties you can modify with Set-ObjectProperty. In this case, the name of the property we want to modify is the Url.

As such, in order to modify both the Name and Url of a sensor in one go we can do the following

foreach($sensor in $sensors)
{
    $properties = $sensor | Get-ObjectProperty
    $newName = $proeprties.Name -replace 'ccccc', 'yyyyy'
    $newUrl = $properties.Url -replace 'ccccc', 'yyyyy'

    Write-Host "Changing sensor $($sensor.Name) (ID: $($sensor.Id)) Name/Url from $($properties.Name)/$($properties.Url) to $newName/$newUrl"
    $sensor | Set-ObjectProperty -Name $newName -Url $newUrl
}

Regards, lordmilko