Forum Discussion
Using PowerShell and Graph to get list of all owners of groups
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.
- EntilZhaApr 09, 2020Iron Contributor
VasilMichev - 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
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
- VasilMichevApr 10, 2020MVP
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.
- EntilZhaApr 10, 2020Iron Contributor
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