SOLVED

How to List of Azure AD groups for a Service Principal using Power shell

Brass Contributor

Hi,

 

I want to fetch list of Azure AD groups which are assigned/ added as member for a service principal.

 

I am using below script but it is taking too much of time due to for loop each AD group check, Can you please suggest any other way we can do in optimized.

 

$ADGroups = Get-AzADGroup
foreach ($ADGroup in $ADGroups)
{
$GroupMembers = Get-AzADGroupMember -ObjectId $ADGroup.Id | where-Object {$_.Type -eq "ServicePrincipal"}

$GroupMember = $GroupMembers | where-Object {$_.DisplayName -eq "xxxxxxxxx"}
if($GroupMember)
{
$string = [pscustomobject]@{
    "ServicePrincipalName" = $GroupMember.DisplayName
    "ADGroupName" = $ADGroup.DisplayName
    }
$string

}

}
1 Reply
best response confirmed by Brahmaiah (Brass Contributor)
Solution

Got solution by using below script.

 

$ServicePrincipalId = Get-AzureADServicePrincipal -All $true | Where {$_.DisplayName -eq 'xxxxx'}

Get-AzureADServicePrincipalMembership -ObjectId $ServicePrincipalId.ObjectId
1 best response

Accepted Solutions
best response confirmed by Brahmaiah (Brass Contributor)
Solution

Got solution by using below script.

 

$ServicePrincipalId = Get-AzureADServicePrincipal -All $true | Where {$_.DisplayName -eq 'xxxxx'}

Get-AzureADServicePrincipalMembership -ObjectId $ServicePrincipalId.ObjectId

View solution in original post