Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Failed to automate PowerShell script using service principal for Power BI Security AD group

Hello All,

Daily I want to automate the Powershell script for extracting Power BI Security AD groups using service principal. Few days back we are able to automate the script but suddenly yesterday onwards i am getting the below error. 

PowerShell script I am using to extract Power BI security AD groups which are added to the Workspace as given below

$AppId = "AppID"
$TenantId = "TenantID"
$ClientSecret = "ClientSecret"

$MsalToken = Get-MsalToken -TenantId $TenantId -ClientId $AppId -ClientSecret ($ClientSecret | ConvertTo-SecureString -AsPlainText -Force)

#Connect to Graph using access token
Connect-graph -AccessToken $MsalToken.AccessToken

$Groups = Get-MgGroup -all|Where-Object {($_.DisplayName -like 'a-PBIxx-*')}|Select-Object Id,DisplayName
foreach ($Group in $Groups)
{
$Group_GUID = $Group.Id
$Group_DisplayName = $Group.DisplayName
$GroupMembers = get-mggroupmember -GroupId $Group_GUID -All

foreach ($GroupMember in $GroupMembers)
{
$users = get-mguser -userid $GroupMember.id
$Properties = @{"GroupDisplayName"=$Group_DisplayName;
"MemberDisplayName"=$users.DisplayName;
"MemberEmail"=$users.UserPrincipalName;
"GUID"=$Group_GUID;}


$Obj = New-Object -TypeName PSObject -Property $Properties
Write-Output $Obj | select GUID,GroupDisplayName,MemberDisplayName,MemberEmail |Export-Csv -Path File path/.csv
}
}
Get-Mggroup -all | Where-Object {($_.DisplayName -like 'a-PBIxx-*')} | ? {(Get-Mggroupmember -GroupId $_.Id).count -eq 0 } | Select-Object Id,DisplayName|Export-Csv -Path File path/.csv -NoTypeInformation
$csvData = Import-Csv -Path File path/.csv" | Select-Object @{n='GUID';e={$_.Id}},@{n='GroupDisplayName';e={$_.DisplayName}},MemberDisplayName,MemberEmail
$csvData | Export-Csv -Append -NoTypeInformation -Path "File path/.csv"

 

Ynr0225_0-1705664815516.png

 

Thanks in advance!

 

1 REPLY 1
lbendlin
Super User
Super User

This is not Power BI related. You will have more luck when you post in the PowerShell forums.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.