What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

API Iterate group child nodes and hierarchy (powershell)

Votes:

0

Do you have a sample of how to return all the child groups belonging to a parent group (knowing the parent group object id), using the API directly, in powershell?

I have been through the entire API documentation, and trawled the KB and can't find an example. Some seem close, but I haven't been able to alter them to work.

I have a fairly large and deep group hierarchy i need to manage from ps.

Thank you.

api collection groups powershell

Created on Aug 11, 2020 1:28:40 PM



Best Answer

Accepted Answer

Votes:

1

Utilizing the Invoke-WebRequest example above (but using JSON instead of XML), the following is a working example that shows how to recurse a group hierarchy using PowerShell

function GetGroups($parentId)
{
    $response = Invoke-WebRequest "https://prtg.example.com/api/table.json?content=groups&columns=objid,name,probe,condition,fold,groupnum,devicenum,upsens,downsens,downacksens,partialdownsens,warnsens,pausedsens,unusualsens,undefinedsens,totalsens,schedule,basetype,baselink,notifiesx,intervalx,access,dependency,position,status,comments,priority,message,parentid,tags,type,active&count=*&filter_parentid=$parentId&username=prtgadmin&password=password"

    ($response.Content | ConvertFrom-Json).groups
}

function RecurseGroups($parentId)
{
    $groups = GetGroups $parentId

    foreach($group in $groups)
    {
        $group

        RecurseGroups $group.objid
    }
}

RecurseGroups 1001

This will return all descendant groups of the group or probe with ID 1001

Created on Aug 13, 2020 8:40:08 AM



8 Replies

Votes:

0

Hi madaw,

You can easily return the descendants of a group in PowerShell by using PrtgAPI

Get-Group -Id 1001 | Get-Group

To retrieve only direct children of a group, you can additionally specify -Recurse:$false

Get-Group -Id 1001 | Get-Group -Recurse:$false

To retrieve the direct children of a group using the raw HTTP API, you would execute a request for groups, specifying a filter_parentid with the ID of the parent group to retrieve children from

Invoke-WebRequest "https://prtg.example.com/api/table.xml?content=groups&columns=objid,name,probe,condition,fold,groupnum,devicenum,ups
ens,downsens,downacksens,partialdownsens,warnsens,pausedsens,unusualsens,undefinedsens,totalsens,schedule,basetype,base
link,notifiesx,intervalx,access,dependency,position,status,comments,priority,message,parentid,tags,type,active&count=*&
filter_parentid=1001&username=prtgadmin&passhash=12345678"

Regards,

lordmilko

Created on Aug 12, 2020 6:04:03 AM



Votes:

0

Hi lordmilko,

Thanks for responding. Yes, I saw that when researching the KB. Thats why I said 'using the API directly'. I work in a secure air gapped network and are blocked from installing anything without multiple mgt authorisations, triplicate signoffs, etc, etc.

I think I can see everything I need to do directly with the API, except for walking up and down the groups. If there is a way to do that in powershell then i can build this process, without having to get through all the red tape, to try and convince the client to use prtg.

Thank you.

Created on Aug 12, 2020 11:16:27 AM



Votes:

0

PRTG provides the /api/table.xml?content=sensortree endpoint for retrieving a basic outline of the structure of PRTG. If you want the full information on objects that is returned from API calls like content=groups however you basically need to recurse the tree yourself. In theory, it's simply a matter of recursively iterating over each group that's returned and retrieving any child groups that may be a member of it.

e.g. you first retrieve the child groups of the group 1001, returning groups, 2001 and 2002. You then retrieve the child groups of groups 2001, 2002, and then the child groups of those child groups, and so on. In order to make the recursion easier you would simply wrap your recursion logic up in a function that keeps iteratively calling itself to retrieve more records.

You can kind of get an idea as to how PrtgAPI does it by looking a the source code here.

You can see that we recurse between the methods

1. GetAdditionalGroupRecordsInternal, which then calls

2. ProcessChilrenAndMaybeRecurseGroupRecords ,which then calls

3. MaybeRecurseGroupRecordsInternal, which then descends even deeper into

1. GetAdditionalGroupRecordsInternal again, and it just keeps going deeper until we stop finding child groups

Obviously, in PrtgAPI's case this is wayyy more complicated than it will need to be for you (PrtgAPI's code needs to be modular, and needs to support traversing a group hierarchy for finding sensors, devices or groups).

While PrtgAPI makes writing this sort of logic fairly easy by having deserialized the XML or JSON response to a structured type, you can still achieve the same thing by using the raw API and writing a whole bunch of logic to execute additional API calls as required.

As to why PRTG doesn't support retrieving the entire hierarchy in one go, the answer they gave in a KB post was they think calculating the object tree would have "poor performance" (whether or not that is true or not is up for debate, but either way that's the current situation)

Created on Aug 13, 2020 5:32:23 AM



Votes:

0

Thank you, I understand. I had pulled PrtgAPI and been through the code and already found the functions you discuss. I was also sure there should be a straightforward way in powershell to do the same just for groups, directly with the API. I found the sensortree and got that working for the top level group.

The issue i ran into was, as you mention, 'You then retrieve the child groups..', and hence my original question.

I couldn't work out: 1. how to get all the child groups of a specific group, directly with the API 2. how to then get the id of each of the child groups

Created on Aug 13, 2020 8:00:40 AM



Accepted Answer

Votes:

1

Utilizing the Invoke-WebRequest example above (but using JSON instead of XML), the following is a working example that shows how to recurse a group hierarchy using PowerShell

function GetGroups($parentId)
{
    $response = Invoke-WebRequest "https://prtg.example.com/api/table.json?content=groups&columns=objid,name,probe,condition,fold,groupnum,devicenum,upsens,downsens,downacksens,partialdownsens,warnsens,pausedsens,unusualsens,undefinedsens,totalsens,schedule,basetype,baselink,notifiesx,intervalx,access,dependency,position,status,comments,priority,message,parentid,tags,type,active&count=*&filter_parentid=$parentId&username=prtgadmin&password=password"

    ($response.Content | ConvertFrom-Json).groups
}

function RecurseGroups($parentId)
{
    $groups = GetGroups $parentId

    foreach($group in $groups)
    {
        $group

        RecurseGroups $group.objid
    }
}

RecurseGroups 1001

This will return all descendant groups of the group or probe with ID 1001

Created on Aug 13, 2020 8:40:08 AM



Votes:

0

Thanks lordmilko. I also found a way of accessing PrtgAPI, so are considering switching to that if I run into more issues.

Created on Aug 19, 2020 9:49:59 AM



Votes:

0

Hi lordmilko

Is there anyway to do this is reverse in the PRTGAPI module? Like from a device or a sensor I want to get the hierarchy (parent groups). I see the "group" property on the device object (get-device), but I would then have to get the groups one by one and wondering if there's an easier/better way.

Thanks!

/Splint

Created on Aug 30, 2022 12:37:34 PM



Votes:

0

You would need to recursively retrieve the ParentId of the device and its parents

$parent = Get-Device -Id 1001

while($parent -ne $null)
{
    $parent = Get-Group -Id $parent.ParentId
}

To recurse up parents of arbitrary types you would do Get-Object -Id $parent.ParentId -Resolve

Created on Sep 1, 2022 12:44:59 AM




Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.