Forum Discussion
Power BI API via PowerShell fails in Azure DevOps
Hi Anonymous
Not sure that you aware of the Azure DevOps Extension called PowerBI Action. Using this you can do some operation easily with out having worry about powershell scripts.
If you still want to do some additional things , you can see the source code of the extension, where the author is also using powershell to build this extension.
The author put all the powershell cleanly in a function, as per that
Function Get-AADToken {
Param(
[parameter(Mandatory = $true)][string]$Username,
[parameter(Mandatory = $true)][SecureString]$Password,
[parameter(Mandatory = $true)][guid]$ClientId,
[parameter(Mandatory = $true)][string]$Resource
)
$authorityUrl = "https://login.microsoftonline.com/common/oauth2/authorize"
## load active directory client dll
$typePath = $PSScriptRoot + "\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
Add-Type -Path $typePath
Write-Verbose "Loaded the Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
Write-Verbose "Using authority: $authorityUrl"
$authContext = New-Object -TypeName Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext -ArgumentList ($authorityUrl)
$credential = New-Object -TypeName Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential -ArgumentList ($UserName, $Password)
Write-Verbose "Trying to aquire token for resource: $Resource"
$authResult = $authContext.AcquireToken($Resource, $clientId, $credential)
Write-Verbose "Authentication Result retrieved for: $($authResult.UserInfo.DisplayableId)"
return $authResult.AccessToken
}I'm also writing a detailed article for CICD for PowerBI using Azure DevOps,
But it's still in progress, I stil need some time to need these too :smileywink:
- Anonymous7 years agoNot applicable
The reason I can't use Power BI Actions (I tried!) is that it needs a username and password. This is not acceptible for our clients. Best practice is to use a client id.
Via PowerShell this is 100% possible. In fact the scripts work when I run them locally on my laptop. They seem to get stuck on the authentication part when I run them as part of a DevOps pipeline.
I'm interested in your blog article!
- Jayendran7 years agoSolution Sage
Hi Anonymous
Ok could you please try my script below
$applicationId = "xxxxxxxx"; $securePassword = "xxxxxx" | ConvertTo-SecureString -AsPlainText -Force $credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $applicationId, $securePassword Connect-PowerBIServiceAccount -ServicePrincipal -Credential $credential -TenantId "xxxx"
Which is using the Client ID autentication part.
I've also provided this answer in SO
By this way it will work in both your local and azure devops
- Anonymous7 years agoNot applicable
This doesn't work because the Power BI cmdlets don't accept service principals for most operations. For example:
Therefore I'm trying to login using a native app (instead of web app) using the code I posted in the OP. That works perfectly fine in Windows, just not in DevOps.