What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general. You are invited to get involved by asking and answering questions!

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
300.000 administrators have chosen PRTG to monitor their network. 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

Your Vote:

Up

Down

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 by  madaw (60) 1 1



Best Answer

Accepted Answer

Votes:

1

Your Vote:

Up

Down

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 by  lordmilko (2,132) 2 1



6 Replies

Votes:

0

Your Vote:

Up

Down

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 by  lordmilko (2,132) 2 1



Votes:

0

Your Vote:

Up

Down

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 by  madaw (60) 1 1



Votes:

0

Your Vote:

Up

Down

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 by  lordmilko (2,132) 2 1



Votes:

0

Your Vote:

Up

Down

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 by  madaw (60) 1 1



Accepted Answer

Votes:

1

Your Vote:

Up

Down

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 by  lordmilko (2,132) 2 1



Votes:

0

Your Vote:

Up

Down

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 by  madaw (60) 1 1



Please log in or register to enter your reply.


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.