Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago

Powershell API Update datasource

Hi,

we're creating poweshell script for the deployment of our datasets/reports to different environments.

Later will use this scripts in Azure DevOps for automatic deployment.

ServicePrincipal is created for the deployments (Create service principle) . Deployment is working fine but we've an issue when we want to update the datasource.

We're connecting to a Azure SQL database.

Everytime we receive the error below when calling the command: "

Invoke-PowerBIRestMethod -Url "groups/$($destinationWorkspace.Id)/datasets/$($datasetid)/Default.UpdateDatasources" -Method POST -Body $NewConnectionBody | ConvertFrom-Json"
The value of the $NewConnectionBody looks like
 
Is there a solution that we can change the datasource using powershell? 

8 Replies

Replies have been turned off for this discussion
  • gauravkh's avatar
    gauravkh
    Frequent Visitor

    Try using "Invoke-RestMethod" with complete URL
    I suppose you are also adding "Content-Type" and header with "Authorization"

  • Anonymous's avatar
    Anonymous
    Not applicable

    Would you mind sharing how you manage to get the Bearer token using Service Principal and Powershell? 

    I have created a SP, added it as admin to the workspace and also added the requiered delegated permissions in Azure (Power BI Service - https://analysis.windows.net/powerbi/api/)

    I can use the .Net nuget client package to do work on the workspace, updating reports/datasets and so on. But I can't manage to get the authorization token when I use either powershell, Invoke-RestMethod, or Postman.

     

     

     

     

    function getBearer([string]$TenantID, [string]$ClientID, [string]$ClientSecret)
    {
      $TokenEndpoint = {https://login.microsoftonline.com/{0}/oauth2/token} -f $TenantID 
      #$ARMResource = "https://analysis.windows.net/powerbi/api/";
      $ARMResource = "https://api.powerbi.com/";
    
      $Body = @{
              'resource'= $ARMResource
              'client_id' = $ClientID
              'grant_type' = 'client_credentials'
              'client_secret' = $ClientSecret
      }
    
      $params = @{
          ContentType = 'application/x-www-form-urlencoded'
          Headers = @{'accept'='application/json'}
          Body = $Body
          Method = 'Post'
          URI = $TokenEndpoint
      }
    
      $token = Invoke-RestMethod @params
    
      Return "Bearer " + ($token.access_token).ToString()
    }

     

    That's how I try to acquire the  token but I keep getting "AADSTS500011: The resource principal named https://api.powerbi.com/ was not found in the tenant named ... " (It's the same with https://analysis.windows.net/powerbi/api/) however https://management.azure.com/ returns a bearer token. 

     

    Thank you in advance ! 

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Anonymous 

       

      i'm not using bearer token.

      But maybe this can help you, this is how i connect:

       

       

      $PowerBIServiceApplicationKey ="xxx"
      $PowerBIServiceApplicationID ="xxx"
      $TenantID = "yyy"
      
      #Install Power Bi Modules
      foreach ($moduleName in @("MicrosoftPowerBIMgmt.Reports", "MicrosoftPowerBIMgmt.Workspaces", "MicrosoftPowerBIMgmt.Profile","MicrosoftPowerBIMgmt.Data"))
      {
        if  ((Get-Module $moduleName) -eq $null)
        {
            Write-Host "Installing $moduleName"
            Install-Module -Name $moduleName -Force
        }
      }
      securedPassword = $PowerBIServiceApplicationKey  | ConvertTo-SecureString -asPlainText -Force
      $credential = New-Object System.Management.Automation.PSCredential($PowerBIServiceApplicationID,$securedPassword)
      
      #Connect to PowerBI
      Connect-PowerBIServiceAccount -ServicePrincipal -Credential $credential -Tenant $TenantID

      Maybe u can use the command

      Get-PowerBIAccessToken 

      to retrieve what you need after the connect?

       

       
       
       
      • Anonymous's avatar
        Anonymous
        Not applicable

        Of course , why I didn't try with the actual Power BI packages is beyond me... 

         

        Thank you very much Anonymous  !