Using PowerShell and Graph to get list of all owners of groups

Frequent Contributor

I’m using PowerShell and Graph to get list of all owners of groups created by Teams. The PS scripts is working and able to get user’s ID from the response; however, I’m having issues getting the remaining information from the response like Display Name and UPN.

 

Response.JPG

 

Snip-it from my scripts that pulls a group’s owners:

Function GroupOwnership($GOObjectID,$GOHeader)

{
$GOCount = 0

         #$GOUri = "https://graph.microsoft.com/v1.0/groups/$GOObjectID/owners"

         $OwnerRequest  = Invoke-RestMethod -Uri $GOUri -Headers $GOHeader -Method Get -ContentType "application/json"

         $GOResults = $OwnerRequest.value

         Foreach($GOResult in $GOResults)

                {

                Write-Host $GOResult.ID "..." $GOResult.DisplayName -ForegroundColor Yellow

              } #End of $GOResults Foreach

         } #End of GroupOwnership  Function

 

All I get Response from this Function is the User ID.

PSScriptResponse.JPG

Question: What do I need to do in PowerShell to capture the remaining response (UPN, Display Name...) from the Graph response?

 

Thank You,

 

-larry

7 Replies

You're not showing the actual query you're running, unless it's the commented one. But as seen from the Graph explorer screenshot, it will return the DisplayName and UPN of any owner by default, and if not you can add a $select statement to the query to request them. Apart from that it's a matter of parsing the response, make sure to convert the JSON to an object, etc.

@Vasil Michev   - thank you for your response.

 

I added the Select in Graph Explorer and I like the result; however, when I applied it to my PowerShell script, I still don’t get the response I’m looking for the Display Name, UPN….

 

Snip-it from Graph Explorer

Graph-Response.JPG

 

Updated Snip-it of PowerShell Script

 

         $GOUri = "https://graph.microsoft.com/v1.0/groups/$GOObjectID/owners?`$select=ID,displayName,userPrincipalName"

         $OwnerRequest  = Invoke-RestMethod -Uri $GOUri -Headers $GOHeader -Method Get -ContentType "application/json"

         $GOResults = $OwnerRequest.value

         Foreach($GOResult in $GOResults)

                {

                Write-host $GOResult

                Write-Host $GOResult.ID "..." $GOResult.displayName  -ForegroundColor Yellow

              }

 

The result from PowerShell sill only shows me the ID number and not the Display Name

Result of Write-host $GOResult :     @{@odata.type=#microsoft.graph.user; id=37bca6da-aa3d-491a-b04f-6040a99b17e8; displayName=; userPrincipalName=}

Result Write-Host $GOResult.ID:    "..." $GOResult.displayName  -ForegroundColor Yellow:   37xxxxxx-xxxx-xxxx-xxxx-60xxxxxxxxxx …

 

Thank You,

-Larry

Works for me with or without adding the select statement. Check your permissions as well, they recently introduced some changes that might "hide" attribute values if you don't have sufficient permissions.

@Vasil Michev 

 

You were correct, I was missing a permission. Once I added Directory.Read.All to the API Permission it started working.

 

Again Thank You Very Much!!!!!

 

-Larry

@Vasil Michev  

 

Thank You, it was a permission issue. I had to add Directory.Read.All to the API Permissions.

 

Again Thank You Very Much!!!

 

-Larry

Well I tried to explain to MS folks that this behavior will confuse people... Oh well.

@Larry Jones 

I am needing to get list of all group owners is it possible to have a copy of your script, is it available for download ?

Thanks