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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Service Principal Not able to use Get-PowerBIWorkspace -All

I have a script using a service principal to extract power bi workspaces on our tenant. 

When I use the code below I get an Unauthroized error code in power shell. This had worked for moths on end before it suddenly stopped. I tried re adding back -Scope Organization and that did not solve the issue either. 

 

$workspaces = Get-PowerBIWorkspace -All

 

Any ideas?

Status: Investigating

Hi  @aleckealey18 ,

 

The “Unauthorized” error typically means that the account you’re using doesn’t have the necessary permissions to perform the operation. 
make sure it has the necessary permissions. The required scope would look like Group.Read.All, Group.ReadWrite.All, Workspace.Read.All, or Workspace.ReadWrite.All.
During the investigation we found a case that may be similar, hope it helpsc# - Operation returned an invalid status code 'Unauthorized'' - Stack Overflow

 

Best regards.
Community Support Team_Scott Chang

Comments
v-tianyich-msft
Community Support
Status changed to: Investigating

Hi  @aleckealey18 ,

 

The “Unauthorized” error typically means that the account you’re using doesn’t have the necessary permissions to perform the operation. 
make sure it has the necessary permissions. The required scope would look like Group.Read.All, Group.ReadWrite.All, Workspace.Read.All, or Workspace.ReadWrite.All.
During the investigation we found a case that may be similar, hope it helpsc# - Operation returned an invalid status code 'Unauthorized'' - Stack Overflow

 

Best regards.
Community Support Team_Scott Chang

rjones
Frequent Visitor

Take with a grain of salt because I am outside of my comfort zone with coding but here's some powershell code that does work for me. The important parts at least. Maybe it will give you an idea. 

 

#Import
Import-Module MicrosoftPowerBIMgmt
Import-Module MicrosoftPowerBIMgmt.Profile

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 ### bypass tls protocal restrictions

#The Azure app was created on my login/account. If we go live with this need to replace with service account, might have to create a new app or figure out a different way.
$password = 'YourPassword' | ConvertTo-SecureString -asPlainText -Force
$username = 'YourUserNameEmail'
$credential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $username, $password


Connect-PowerBIServiceAccount -Credential $credential

#directory to csv export

$ActivityLogsPathWorkSpaces = "\\blah\blah\Workspaces.csv"
$ActivityLogsPathReports = "\\blah\blah\Reports.csv"

 

#Export to csv file
$workspaceReports = @()

# Get the data
$workspaces = Get-PowerBIWorkspace -All

# Get the report and dataset data for each workspace
foreach ($workspace in $workspaces) {
$reports = Get-PowerBIReport -WorkspaceId $workspace.Id
$datasets = Get-PowerBIDataset -WorkspaceId $workspace.Id

$reports | Add-Member -NotePropertyName WorkspaceId -NotePropertyValue $workspace.Id
$workspaceReports += $reports

}

# Output the results for each type to CSV files
$workspaces | Export-CSV -Force -Path $ActivityLogsPathWorkSpaces

 

Disconnect-PowerBIServiceAccount

 

I can't remember if there was an Azure app or something like that for authentication. I made a note to myself at the time so appears so.