Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
9 years ago
Solved

Authentication in Powershell and API to update a dataset.

I have a working Powershell script that runs a SQL query and creates/updates results to a PowerBI dataset. The trouble is it uses my own SSO login to do it.   I want to run this as a task/service t...
  • Anonymous's avatar
    Anonymous
    9 years ago

    Hi Anonymous

     

    I'm currently using PowerShell via SQL Agent to run a .ps1 file that calls the refresh API for some of my datasets.  In order to for the account to access powerBi it must exist in Azure Active Directory and have a PowerBI account (a pro account may be needed depending on what you're doing).  I can't speak for how your organization is structured but for me, I had our Infastructure guys create a service account in our local AD and give it an O365 account.  Once that is done, the account is synced to Azure AD.  I then had them give the account a PBI pro license as it's being used as a service account for embedding.

     

    Then I created a powershell script and I'm using those credentials to create the access token.  As far as not hard coding the PW into the script, I actually created a text file with an encrypted string.  This file is pulled into the script and decrypted at run time.

     

    Here is the code snippet that pulls in the file and decrypts it:

     

    #Get the root folder of this file
    $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
    
    #Get the fully qualified filename
    $pwdFile = "$PSScriptRoot\$pwdFileName"
    
    #Create the key
    $key = (1..16)
    
    #Decrypt the pw
    $password = Get-Content -LiteralPath $pwdFile | ConvertTo-SecureString -Key $key

     

    Here is a link to my post with some code snippets...Note that in my post I'm still using the hard coded PW.  I changed it to a secure file after my post.

     

    https://community.powerbi.com/t5/Developer/programmatic-data-refresh-using-api/m-p/223051#M7045