Apr 08 2020
08:48 AM
- last edited on
Jan 14 2022
04:32 PM
by
TechCommunityAP
Apr 08 2020
08:48 AM
- last edited on
Jan 14 2022
04:32 PM
by
TechCommunityAP
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.
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.
Question: What do I need to do in PowerShell to capture the remaining response (UPN, Display Name...) from the Graph response?
Thank You,
-larry
Apr 08 2020 08:59 AM
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.
Apr 09 2020 10:50 AM
@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
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
Apr 10 2020 12:13 AM
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.
Apr 10 2020 06:27 AM
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
Apr 10 2020 08:22 AM
Thank You, it was a permission issue. I had to add Directory.Read.All to the API Permissions.
Again Thank You Very Much!!!
-Larry
Apr 11 2020 08:48 AM
Well I tried to explain to MS folks that this behavior will confuse people... Oh well.
Dec 10 2020 12:59 AM
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