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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Feb2025 NL Carousel

Fabric Community Update - February 2025

Find out what's new and trending in the Fabric community.