locked
Update dataset credential of powerBI RRS feed

  • Question

  • Hello Team,

    I am following the below link to update the Credentials of a dataset via powershell through basic auth:

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

    Below is my sample code:

    $datasetname="<<Dataset name>>"
    $workspacename="PowerAutomation"
    
    
    
    $clientsec = "<<Client Secret>>" | ConvertTo-SecureString -AsPlainText -Force
    
    $credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "<<App ID>>", $clientsec 
    Connect-PowerBIServiceAccount -ServicePrincipal -Credential $credential -TenantId "<<Tenant Id>>"
    
    $workspace =Get-PowerBIWorkspace -Name $workspacename
    
    $workspace
    
    
    # GetDataSets
    $DatasetResponse=Invoke-PowerBIRestMethod -Url "groups/$($workspace.id)/datasets" -Method Get | ConvertFrom-Json
    
    
    # Get DataSet
    $datasets = $DatasetResponse.value
    
         foreach($dataset in $datasets){
                    if($dataset.name -eq $datasetname){
                    $datasetid= $dataset.id;
                    break;
                    }
    
                }
    
    
    $BounGateway=Invoke-PowerBIRestMethod -Url "groups/$($workspace.id)/datasets/$($datasetid)/Default.GetBoundGatewayDataSources" -Method GET | ConvertFrom-Json
    
    
    
    
    $Body = @{
    				credentialDetails = @{
    					credentialType = "Basic";
    					credentials = "{`"credentialData`":[{`"name`":`"username`", `"value`":`"test`"},{`"name`":`"password`", `"value`":`"xyz`"}]}";
    					encryptedConnection = "Encrypted";
    					encryptionAlgorithm = "None";
    					privacyLevel = "None"
    				}
    			} | ConvertTo-Json
    
    
    $Body
    
    
    Invoke-PowerBIRestMethod -Url "gateways/$($BounGateway.value.gatewayId)/datasources/$($BounGateway.value.id)" -Method PATCH -Body $Body | ConvertTo-Json

    I am getting the below error :

    Message        : Response status code does not indicate success: 400 (Bad Request).
    StackTrace     :    at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
                        at Microsoft.PowerBI.Commands.Profile.InvokePowerBIRestMethod.<InvokeRestMethod>d__31.MoveNext()
    Exception      : System.Net.Http.HttpRequestException
    InvocationInfo : {Invoke-PowerBIRestMethod}
    Line           : Invoke-PowerBIRestMethod -Url 
                     "gateways/$($BounGateway.value.gatewayId)/datasources/$($BounGateway.value.id)" -Method PATCH -Body $Body | 
                     ConvertTo-Json

    So can anyone tell me what I am doing wrong based on the MSFT documentation.

    Thursday, April 30, 2020 11:52 AM

Answers