<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Getting error using Power Shell Script to update data sources. in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Getting-error-using-Power-Shell-Script-to-update-data-sources/m-p/4404478#M59436</link>
    <description>&lt;P&gt;We are developing CI/CD Pipeline in Azure DevOps. We are able to deploy PBIX to development workspace. Now we deployed the report on production workspace and using Power Shell Script to update data source. but getting error as below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Invoke-PowerBIRestMethod : Encountered errors when invoking the command: {&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;"code": "UnknownError",&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;"pbi.error": {&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;"code": "UnknownError",&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;"parameters": {},&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;"details": []&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;}&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;}&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;We are using service principal to authenticate and all the required permission is allowed. Below is the power shell script we are using:&lt;BR /&gt;$SecuredApplicationSecret = ConvertTo-SecureString -String $clientsec1 -AsPlainText -Force&lt;BR /&gt;$credential = New-Object System.Management.Automation.PSCredential($applictionId, $SecuredApplicationSecret)&lt;BR /&gt;$sp = Connect-PowerBIServiceAccount -ServicePrincipal -Tenant $tenantId -Credential $credential&lt;BR /&gt;$workspace = Get-PowerBIWorkspace -Name $workspacename&lt;BR /&gt;#$dataset1 = Get-PowerBIDataset -WorkspaceId $workspace.Id -Name $reportName&lt;BR /&gt;$dataset1 = Get-PowerBIDataset -WorkspaceId $workspace.Id | Where-Object {$_.name -eq $reportName}&lt;/P&gt;&lt;P&gt;$workspaceId = $workspace.Id&lt;BR /&gt;$datasetId = $dataset.Id&lt;BR /&gt;$workspaceId&lt;BR /&gt;$datasources = Get-PowerBIDatasource -WorkspaceId $workspaceId -DatasetId $datasetId&lt;BR /&gt;$datasources&lt;BR /&gt;foreach($datasource in $datasources) {&lt;BR /&gt;$gatewayId = $datasource.gatewayId&lt;BR /&gt;$datasourceId = $datasource.datasourceId&lt;BR /&gt;$datasourePatchUrl = "gateways/$gatewayId/datasources/$datasourceId"&lt;BR /&gt;Write-Host "Patching credentials for $datasourceId"&lt;BR /&gt;# HTTP request body to patch datasource credentials&lt;BR /&gt;$userNameJson = "{""name"":""username"",""value"":""$sqlUserName""}"&lt;BR /&gt;$passwordJson = "{""name"":""password"",""value"":""$sqlUserPassword""}"&lt;BR /&gt;$patchBody = @{&lt;BR /&gt;"credentialDetails" = @{&lt;BR /&gt;"credentials" = "{""credentialData"":[ $userNameJson, $passwordJson ]}"&lt;BR /&gt;"credentialType" = "Basic"&lt;BR /&gt;"encryptedConnection" = "Encrypted"&lt;BR /&gt;"encryptionAlgorithm" = "RSA-OAEP"&lt;BR /&gt;"privacyLevel" = "None"&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;# Convert body contents to JSON&lt;BR /&gt;$patchBodyJson = ConvertTo-Json -InputObject $patchBody -Depth 6 -Compress&lt;BR /&gt;# Execute PATCH operation to set datasource credentials&lt;BR /&gt;Invoke-PowerBIRestMethod -Method Patch -Url $datasourePatchUrl -Body $patchBodyJson&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Please suggest a solution.&lt;/P&gt;</description>
    <pubDate>Tue, 11 Feb 2025 07:23:50 GMT</pubDate>
    <dc:creator>rajeshlohar1974</dc:creator>
    <dc:date>2025-02-11T07:23:50Z</dc:date>
    <item>
      <title>Getting error using Power Shell Script to update data sources.</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Getting-error-using-Power-Shell-Script-to-update-data-sources/m-p/4404478#M59436</link>
      <description>&lt;P&gt;We are developing CI/CD Pipeline in Azure DevOps. We are able to deploy PBIX to development workspace. Now we deployed the report on production workspace and using Power Shell Script to update data source. but getting error as below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Invoke-PowerBIRestMethod : Encountered errors when invoking the command: {&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;"code": "UnknownError",&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;"pbi.error": {&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;"code": "UnknownError",&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;"parameters": {},&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;"details": []&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;}&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;}&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;We are using service principal to authenticate and all the required permission is allowed. Below is the power shell script we are using:&lt;BR /&gt;$SecuredApplicationSecret = ConvertTo-SecureString -String $clientsec1 -AsPlainText -Force&lt;BR /&gt;$credential = New-Object System.Management.Automation.PSCredential($applictionId, $SecuredApplicationSecret)&lt;BR /&gt;$sp = Connect-PowerBIServiceAccount -ServicePrincipal -Tenant $tenantId -Credential $credential&lt;BR /&gt;$workspace = Get-PowerBIWorkspace -Name $workspacename&lt;BR /&gt;#$dataset1 = Get-PowerBIDataset -WorkspaceId $workspace.Id -Name $reportName&lt;BR /&gt;$dataset1 = Get-PowerBIDataset -WorkspaceId $workspace.Id | Where-Object {$_.name -eq $reportName}&lt;/P&gt;&lt;P&gt;$workspaceId = $workspace.Id&lt;BR /&gt;$datasetId = $dataset.Id&lt;BR /&gt;$workspaceId&lt;BR /&gt;$datasources = Get-PowerBIDatasource -WorkspaceId $workspaceId -DatasetId $datasetId&lt;BR /&gt;$datasources&lt;BR /&gt;foreach($datasource in $datasources) {&lt;BR /&gt;$gatewayId = $datasource.gatewayId&lt;BR /&gt;$datasourceId = $datasource.datasourceId&lt;BR /&gt;$datasourePatchUrl = "gateways/$gatewayId/datasources/$datasourceId"&lt;BR /&gt;Write-Host "Patching credentials for $datasourceId"&lt;BR /&gt;# HTTP request body to patch datasource credentials&lt;BR /&gt;$userNameJson = "{""name"":""username"",""value"":""$sqlUserName""}"&lt;BR /&gt;$passwordJson = "{""name"":""password"",""value"":""$sqlUserPassword""}"&lt;BR /&gt;$patchBody = @{&lt;BR /&gt;"credentialDetails" = @{&lt;BR /&gt;"credentials" = "{""credentialData"":[ $userNameJson, $passwordJson ]}"&lt;BR /&gt;"credentialType" = "Basic"&lt;BR /&gt;"encryptedConnection" = "Encrypted"&lt;BR /&gt;"encryptionAlgorithm" = "RSA-OAEP"&lt;BR /&gt;"privacyLevel" = "None"&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;# Convert body contents to JSON&lt;BR /&gt;$patchBodyJson = ConvertTo-Json -InputObject $patchBody -Depth 6 -Compress&lt;BR /&gt;# Execute PATCH operation to set datasource credentials&lt;BR /&gt;Invoke-PowerBIRestMethod -Method Patch -Url $datasourePatchUrl -Body $patchBodyJson&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Please suggest a solution.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Feb 2025 07:23:50 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Getting-error-using-Power-Shell-Script-to-update-data-sources/m-p/4404478#M59436</guid>
      <dc:creator>rajeshlohar1974</dc:creator>
      <dc:date>2025-02-11T07:23:50Z</dc:date>
    </item>
    <item>
      <title>Re: Getting error using Power Shell Script to update data sources.</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Getting-error-using-Power-Shell-Script-to-update-data-sources/m-p/4406098#M59451</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/550217"&gt;@rajeshlohar1974&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;Please check and confirm if the following info is OK:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;SPAN&gt;Make sure the service principal have&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;admin access&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;to the workspace where the dataset resides&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;Verify the variable&amp;nbsp;$gatewayId and $datasourceId are correct&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;The REST API has the proper permission:&lt;A href="https://learn.microsoft.com/en-us/rest/api/power-bi/datasets/update-datasources-in-group" target="_blank"&gt;Datasets - Update Datasources In Group - REST API&amp;nbsp;&lt;/A&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="vyiruanmsft_1-1739328982272.png" style="width: 999px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/1237714iDD165016BC82A237/image-size/large?v=v2&amp;amp;px=999" role="button" title="vyiruanmsft_1-1739328982272.png" alt="vyiruanmsft_1-1739328982272.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;After confirmed the above info is OK, please update the codes as below:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;$SecuredApplicationSecret = ConvertTo-SecureString -String $clientsec1 -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($applictionId, $SecuredApplicationSecret)
$sp = Connect-PowerBIServiceAccount -ServicePrincipal -Tenant $tenantId -Credential $credential

$workspace = Get-PowerBIWorkspace -Name $workspacename
$dataset1 = Get-PowerBIDataset -WorkspaceId $workspace.Id | Where-Object {$_.name -eq $reportName}

$workspaceId = $workspace.Id
$datasetId = $dataset1.Id  # Corrected variable name

Write-Host "Workspace ID: $workspaceId"
Write-Host "Dataset ID: $datasetId"

$datasources = Get-PowerBIDatasource -WorkspaceId $workspaceId -DatasetId $datasetId
Write-Host "Data Sources: $($datasources | ConvertTo-Json)"

foreach($datasource in $datasources) {
    $gatewayId = $datasource.gatewayId
    $datasourceId = $datasource.datasourceId
    $datasourePatchUrl = "gateways/$gatewayId/datasources/$datasourceId"

    Write-Host "Patching credentials for Data Source ID: $datasourceId"

    # HTTP request body to patch datasource credentials
    $patchBody = @{
        "credentialDetails" = @{
            "credentials" = @{
                "credentialData" = @(
                    @{ "name" = "username"; "value" = "$sqlUserName" },
                    @{ "name" = "password"; "value" = "$sqlUserPassword" }
                )
            }
            "credentialType" = "Basic"
            "encryptedConnection" = "Encrypted"
            "encryptionAlgorithm" = "RSA-OAEP"
            "privacyLevel" = "None"
        }
    }

    # Convert body contents to JSON
    $patchBodyJson = ConvertTo-Json -InputObject $patchBody -Depth 6 -Compress
    Write-Host "Patch Body: $patchBodyJson"

    # Execute PATCH operation to set datasource credentials
    try {
        Invoke-PowerBIRestMethod -Method Patch -Url $datasourePatchUrl -Body $patchBodyJson
        Write-Host "Successfully updated credentials for Data Source ID: $datasourceId"
    } catch {
        Write-Host "Error updating credentials: $_"
    }
}&lt;/LI-CODE&gt;
&lt;P&gt;Best Regards&lt;/P&gt;</description>
      <pubDate>Wed, 12 Feb 2025 02:56:36 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Getting-error-using-Power-Shell-Script-to-update-data-sources/m-p/4406098#M59451</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2025-02-12T02:56:36Z</dc:date>
    </item>
    <item>
      <title>Re: Getting error using Power Shell Script to update data sources.</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Getting-error-using-Power-Shell-Script-to-update-data-sources/m-p/4406418#M59454</link>
      <description>&lt;P&gt;Hi&amp;nbsp;@Anonymous&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for the response. I verified the permissions and all the permission are already given to service principal. Also tried the above solution, but getting below error:&lt;BR /&gt;Invoke-PowerBIRestMethod : Encountered errors when invoking the command: {&lt;BR /&gt;"code": "BadRequest",&lt;BR /&gt;"message": "Bad Request",&lt;BR /&gt;"details": [&lt;BR /&gt;{&lt;BR /&gt;"message": "Invalid value",&lt;BR /&gt;"target": "datasourceDelta"&lt;BR /&gt;}&lt;BR /&gt;]&lt;BR /&gt;}&lt;/P&gt;</description>
      <pubDate>Wed, 12 Feb 2025 07:01:14 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Getting-error-using-Power-Shell-Script-to-update-data-sources/m-p/4406418#M59454</guid>
      <dc:creator>rajeshlohar1974</dc:creator>
      <dc:date>2025-02-12T07:01:14Z</dc:date>
    </item>
    <item>
      <title>Re: Getting error using Power Shell Script to update data sources.</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Getting-error-using-Power-Shell-Script-to-update-data-sources/m-p/4408569#M59475</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/550217"&gt;@rajeshlohar1974&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;The following links are the ones which has the similar requirement as yours, hope its solution can help you.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://community.fabric.microsoft.com/t5/Developer/Update-Datasource-Credential-Using-Only-Powershell-and-RSA-OAEP/m-p/2089776" target="_blank"&gt;Solved: Update Datasource Credential Using Only Powershell... - Microsoft Fabric Community&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/microsoft/PowerBI-Developer-Samples/blob/master/PowerShell%20Scripts/EncryptGatewayCredentials.ps1" target="_blank"&gt;PowerBI-Developer-Samples/PowerShell Scripts/EncryptGatewayCredentials.ps1 at master&lt;/A&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;# Basic Credentials
function EncryptBasicCredentials {
    param (
        [Parameter(Mandatory=$True,Position=1)]
        [String]$Username,
        [Parameter(Mandatory=$True,Position=2)] 
        [String]$PasswordAsString,
        [Parameter(Mandatory=$True,Position=3)] 
        [string]$GatewayExponent,
        [Parameter(Mandatory=$True,Position=4)] 
        [string]$GatewayModulus
    )
    
    # Create the objects to perform the necessary encryption on the credentials.  
    $gatewayKeyObj = [Microsoft.PowerBI.Api.Models.GatewayPublicKey]::new($GatewayExponent, $GatewayModulus)
    $credentialsEncryptor = [Microsoft.PowerBI.Api.Extensions.AsymmetricKeyEncryptor]::new($gatewayKeyObj)
    $basicCreds = [Microsoft.PowerBI.Api.Models.Credentials.BasicCredentials]::new($username, $PasswordAsString)

    # Construct the CredentialDetails object. The resulting "Credentials" property on this object will have been encrypted appropriately, ready for use in the request payload.
    $credentialDetails = [Microsoft.PowerBI.Api.Models.CredentialDetails]::new(
        $basicCreds, 
        [Microsoft.PowerBI.Api.Models.PrivacyLevel]::Organizational, 
        [Microsoft.PowerBI.Api.Models.EncryptedConnection]::Encrypted, 
        $credentialsEncryptor)
    
    # Construct the body for the API request.
    $body = @{
        credentialDetails = @{
            credentialType      = "Basic";
            credentials         = $credentialDetails.Credentials;
            encryptedConnection = "Encrypted";
            encryptionAlgorithm = "RSA-OAEP";
            privacyLevel        = "Organizational";
        }
    }
    $bodyJson = $body | ConvertTo-Json

    Write-Output $bodyJson
}&lt;/LI-CODE&gt;
&lt;P&gt;Best Regards&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2025 07:45:16 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Getting-error-using-Power-Shell-Script-to-update-data-sources/m-p/4408569#M59475</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2025-02-13T07:45:16Z</dc:date>
    </item>
    <item>
      <title>Re: Getting error using Power Shell Script to update data sources.</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Getting-error-using-Power-Shell-Script-to-update-data-sources/m-p/4409108#M59486</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/550217"&gt;@rajeshlohar1974&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P data-pm-slice="0 0 []"&gt;You are facing an UnknownError while updating the data source credentials in Power BI using a PowerShell script within an Azure DevOps CI/CD pipeline. The issue is likely related to service principal permissions, incorrect dataset references, or an invalid JSON request body structure. First, ensure the service principal has the necessary API permissions in Azure AD, such as Dataset.ReadWrite.All, Tenant.ReadWrite.All, and Workspace.ReadWrite.All, and that Admin consent has been granted. Additionally, in the Power BI Admin Portal, enable service principal access under Tenant Settings and confirm that the service principal is assigned as a Member or Admin in the target workspace.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another common issue is incorrect dataset and workspace references in your script. You need to correctly assign $datasetId = $dataset1.Id instead of using an undefined variable $dataset. Also, verify if $gatewayId and $datasourceId are populated by printing their values before making the API call. If they are empty, the dataset might not be properly mapped to a gateway. Furthermore, check the JSON request body structure—it appears to be missing a comma after "credentials", which could be causing the API to reject the request. You can restructure it by ensuring the credential details follow Power BI’s expected format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To debug further, manually test the Power BI API by running Invoke-PowerBIRestMethod with a GET request to check if the dataset and gateway are correctly linked. If the error persists, confirm that the dataset is configured to use a gateway (if required) and that the workspace settings allow external connections. By addressing these potential issues, you should be able to resolve the error and successfully update the data source credentials in your Power BI deployment process.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2025 13:03:18 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Getting-error-using-Power-Shell-Script-to-update-data-sources/m-p/4409108#M59486</guid>
      <dc:creator>Poojara_D12</dc:creator>
      <dc:date>2025-02-13T13:03:18Z</dc:date>
    </item>
    <item>
      <title>Re: Getting error using Power Shell Script to update data sources.</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Getting-error-using-Power-Shell-Script-to-update-data-sources/m-p/4416990#M59567</link>
      <description>&lt;P&gt;Hello&amp;nbsp;@Anonymous&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried the above, but still not working.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a power bi report connected with an on-premises data source using on-premises data gateway. When I publish report to workspace, it is working well but when I publish report using CI/CD pipeline from Development to Production workspace, then I need to update datasource like database name and credentials. I am able to update database name and server name using /default.updateparameters API, but getting error in updating credentials.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also I found that gateway ID getting from dataset is different from gateway ID on Power BI, it seems it updates when parameters updated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Suggest possible solution.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Feb 2025 05:47:09 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Getting-error-using-Power-Shell-Script-to-update-data-sources/m-p/4416990#M59567</guid>
      <dc:creator>rajeshlohar1974</dc:creator>
      <dc:date>2025-02-19T05:47:09Z</dc:date>
    </item>
  </channel>
</rss>

