I need to query all objects from a starting point in a for loop and take action on specific type of device To make it simple I show the logic [xml] foreach (object in AllObjectsFromStartGroupID) { if type=Group => set some group property if type=device ==> set host=newIP if type=sensor ==> set name Sitecode + old name (ie: PING becomes "99-PING" I thought of using a "type" property but it sounds like it does not exist Did I miss something?


Article Comments

There's no way to retrieve all types of objects in a single request; instead, you can either iterate over the children of an object, splitting the processing of each object type up via functions, or execute a series of highly specified queries to target the actual objects you're interested in.

If you really wish to loop over all objects, the following code snippet shows an example of how to do this using PrtgAPI, adapted from a script originally designed to generate a "tree" of PRTG's object hierarchy

function ProcessRootChildren()
{
    $probes = Get-Probe

    foreach($probe in $probes)
    {
        ProcessProbeChildren $probe
    }
}

function ProcessProbeChildren($probe)
{
    ProcessContainerChildren $probe
}

function ProcessContainerChildren($parentObj)
{
    $filter = flt parentid eq $parentObj.Id

    $groups = $filter|Get-Group

    foreach($group in $groups)
    {
        ProcessGroupChildren $group
    }

    $devices = $filter|Get-Device

    foreach($device in $devices)
    {
        ProcessDeviceChildren $device
    }
}

function ProcessGroupChildren($parentObj)
{
    ProcessContainerChildren $parentObj
}

function ProcessDeviceChildren($device)
{
    $sensors = $device | Get-Sensor

    foreach($sensor in $sensors)
    {
        # Do stuff to sensors
    }
}

Most likely though you don't really need to loop over every object, and would be better served executing more specified queries

$groups = Get-Group -Tags newyork

foreach($group in $groups)
{
    # Suppose the site code is encoded in the first few letters of the group name

    $siteCode = $group.Name.Substring(0, 2)

    # etc
}

If you have a CSV of your device names (as displayed in PRTG) that's even better

$csv = Import-Csv C:\servers.csv

foreach($server in $csv)
{
    Get-Device $csv.Device | Set-ObjectProperty Host $csv.NewHost
}

Regards,

lordmilko


Mar, 2018 - Permalink

Thank you for this exhaustive answer Apologies for my late answer I didn't know the module PrtgAPI I will give it a try My question raised after I have found out that when running a query with an invalid or empty content (content=device instead of content=devices) I got every kind of objects


Mar, 2018 - Permalink

Hi there,

The module is a third party application developed by lordmilko! We highly recommend it for doing basic (and even extensive) API-Tasks.

Best regards.


Mar, 2018 - Permalink