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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
SamInogic
Super User
Super User

Help with Editing Data Source Credentials in PowerShell for Power BI Embedded Dashboards in D365 CRM

Hello,

We are currently migrating Power BI embedded dashboards from one Dynamics 365 CRM tenant to another. The process involves creating a new Power BI report within a workspace. Since the Dynamics 365 Solution does not support the inclusion of Power BI reports within unmanaged solutions, we have resorted to using PowerShell scripting to manage the migration.

During this migration, we encountered an issue where the report IDs from the original environment remained linked when moving the Power BI embedded dashboards to the new environment. To address this, we have utilized Environment Variables to map the new report and workspace IDs appropriately.

However, we are facing an issue when attempting to edit the Data Source Credentials using PowerShell scripting. Specifically, we are receiving a "Bad Request" error, as shown in the screenshot below.

SamInogic_0-1736838004550.jpeg

 

Here is the PowerShell code we are currently using to edit the Data Source Credentials:

Connect-PowerBIServiceAccount

 

$accesstoken = Get-PowerBIAccessToken

 

$patchBody = '"credentialDetails": {

   "credentials": {"accessToken":"'+$($accesstoken)+'"},

    "encryptedConnection": "Encrypted",

   "encryptionAlgorithm": "None",

  "privacyLevel": "Organizational",

    "useEndUserOAuth2Credentials": true

}'

 

Invoke-PowerBIRestMethod -Url "https://api.powerbi.com/v1.0/myorg/gateways/9eaed562-e9c2-46e4-91cf-276ed0947b41/datasources/f118185..." -Method Patch -Body $patchBody  

 

We would greatly appreciate any guidance or suggestions on resolving this error in the PowerShell scripting process for updating the Data Source Credentials.

 

Thanks!

Inogic Professional Services: Power Platform/Dynamics 365 CRM
An expert technical extension for your techno-functional business needs
Drop an email at crm@inogic.com
Service: https://www.inogic.com/services/
Tips and Tricks: https://www.inogic.com/blog/
1 ACCEPTED SOLUTION
SamInogic
Super User
Super User

Hi,

 

Thanks so much for all your help and suggestions. After trying the different recommendations, we followed these steps, and it resolved the issue:

$body = @{

    "credentialDetails" = @{

        "credentialType"         = "OAuth2"

        "encryptedConnection"    = "Encrypted"

        "encryptionAlgorithm"    = "None"

        "privacyLevel"           = "Organizational"

        "useCallerAADIdentity"   = $true

    }

}

 

# Convert the body to JSON format

$jsonBody = $body | ConvertTo-Json -Depth 5

 

Invoke-PowerBIRestMethod -Url "https://api.powerbi.com/v1.0/myorg/gateways/$($datasource.GatewayId)/datasources/$($datasource.Datas..." -Method PATCH -Body $jsonBody

 

I appreciate the time you all took to assist me.

 

Thanks!

 

Inogic Professional Services: Power Platform/Dynamics 365 CRM
An expert technical extension for your techno-functional business needs
Drop an email at crm@inogic.com
Service: https://www.inogic.com/services/
Tips and Tricks: https://www.inogic.com/blog/

View solution in original post

3 REPLIES 3
SamInogic
Super User
Super User

Hi,

 

Thanks so much for all your help and suggestions. After trying the different recommendations, we followed these steps, and it resolved the issue:

$body = @{

    "credentialDetails" = @{

        "credentialType"         = "OAuth2"

        "encryptedConnection"    = "Encrypted"

        "encryptionAlgorithm"    = "None"

        "privacyLevel"           = "Organizational"

        "useCallerAADIdentity"   = $true

    }

}

 

# Convert the body to JSON format

$jsonBody = $body | ConvertTo-Json -Depth 5

 

Invoke-PowerBIRestMethod -Url "https://api.powerbi.com/v1.0/myorg/gateways/$($datasource.GatewayId)/datasources/$($datasource.Datas..." -Method PATCH -Body $jsonBody

 

I appreciate the time you all took to assist me.

 

Thanks!

 

Inogic Professional Services: Power Platform/Dynamics 365 CRM
An expert technical extension for your techno-functional business needs
Drop an email at crm@inogic.com
Service: https://www.inogic.com/services/
Tips and Tricks: https://www.inogic.com/blog/
v-yiruan-msft
Community Support
Community Support

Hi @SamInogic ,

Could you please provide the following info for the further troubleshooting?

  • Whether the access token $accesstoken is being retrieved correctly and is valid?
  • Whether the gateway and data source IDs used in the endpoint URL are correct and accessible within the new tenant?

  • Whether the account you are using has the necessary permissions for updating data source credential?

# Connect to the Power BI Service
Connect-PowerBIServiceAccount

# Get Access Token
$accesstoken = Get-PowerBIAccessToken

# Construct the request body
$patchBody = @{
"credentialDetails" = @{
"credentials" = @{
"accessToken" = $accesstoken
}
"encryptedConnection" = "Encrypted"
"encryptionAlgorithm" = "None"
"privacyLevel" = "Organizational"
"useEndUserOAuth2Credentials" = $true
}
}

# Convert the request body to JSON
$jsonBody = $patchBody | ConvertTo-Json -Depth 10

# API Endpoint (replace with your specific gateway and data source ID)
$apiUrl = "https://api.powerbi.com/v1.0/myorg/gateways/9eaed562-e9c2-46e4-91cf-276ed0947b41/datasources/f118185..."

# Send the PATCH request
Invoke-PowerBIRestMethod -Url $apiUrl -Method Patch -Body $jsonBody -ContentType "application/json"

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
lbendlin
Super User
Super User

That's patching the credentials for the gateway connection, not for the semantic model?

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! Prices go up Feb. 11th.

Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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