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
cgstdevops
New Member

PowerShell API Get/Update Datasource for SPN

Hello,

 

I am trying to automate the changing of a datasource for a report with a PowerShell script

 

When trying to call Get-PowerBIDatasource  and Invoke-PowerBIRestMethod to update a datasource when authenticated as a service principal, I am receiving a 403 Unauthorized error. 

 

My SPN is an admin in the workspace and I  made sure to take over the dataset.   

I did confirm that I can do this with a non-service principal.

 

Thanks!

4 REPLIES 4
migs
Frequent Visitor

I am running into the same issue. I can connect fine with the -ServicePrincipal option. Get-PowerBIWorkspace runs fine. Get-PowerBiReport runs fine as well. The script fails at the following line:

$PbiDataSourcesObject = (Get-PowerBIDatasource -DatasetId $PbiReportObject.DatasetId)

 

The PS error message I get is:

An error has occurred!!
Error Line Number : 110
Error Command : $PbiDataSourcesObject = (Get-PowerBIDatasource -DatasetId $PbiReportObject.DatasetId)
Error Message : Operation returned an invalid status code 'Forbidden'

v-juanli-msft
Community Support
Community Support

Hi @cgstdevops 

Before you run Get-PowerBIDatasource  and Invoke-PowerBIRestMethod command, make sure you log in using Connect-PowerBIServiceAccount.

https://docs.microsoft.com/en-us/powershell/module/MicrosoftPowerBIMgmt.Profile/Invoke-PowerBIRestMe...

 

Here are similar threads you could refer to:

Powershell API Update datasource

https://docs.microsoft.com/en-us/rest/api/power-bi/gateways/updatedatasource#windows_credentials_exa...

https://docs.microsoft.com/en-us/power-bi/developer/encrypt-credentials

 

Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks for the reply.

 

I am logging in successfully with  Connect-PowerBIServiceAccount so that's not the issue. After logging in, I can publish a report, get a report and several other operations, but I am unable to modify datasources from a service account. As mentioned before, I get a 403 Unathorized error. It looks as if service principals are not allowed to call these methods.  

 

I was able to do this with an actual user, so I am currently impersonating a real user to accomplish this automation. 

 

Thanks again and please let me know if there is something I am missing.

 

Neal

Hi @cgstdevops ,

 

Can you provide your full powershell script, so that I can try to reproduce the issue ? 

 

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!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.