I am trying to import .pbix file into azure powerbi workspace collection, for that I used the azure cli and powerbi cli commands.

Power shell script for logging into azure portal and import the .pbix file into azure powerbi workspace collection:

Param(

#Install npm prerequisites : Azure-cli and Powerbi-cli | Y : N | Optional,  default No
[Parameter(Mandatory=$False)] [bool] $Prerequisites=$false,

# Run the authentication process for the Azure Sub | Y : N | Optional, default No
[Parameter(Mandatory=$True)] [bool] $Authentication=$true,

# Name of the resource group | Ex : "MyResourceGroup" | Mandatory
[Parameter(Mandatory=$True)] [string] $ResourceGroupName,

# Location on Azure for the deployment | Ex : "West US" | Mandatory
[Parameter(Mandatory=$True)] [string] $Location,

# Name of the Workspace collection name | Ex : "MyPBIWorkspace" | Mandatory
[Parameter(Mandatory=$True)] [string] $WorkSpaceCollectionName,

# Id of the Power BI Workspace | Ex : "XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXX" | Mandatory
[Parameter(Mandatory=$True)] [string]   $WSguid,

# Path of the PowerBI Report(.pbix) file |    Ex : "E:\Users\PowerShellScriptForPowerBI" | Mandatory
[Parameter(Mandatory=$True)] [string] $ReportsFilePath

)
cls
Write-Host -BackgroundColor Black -ForegroundColor Green "##### Script launched ###### "

if ($prerequisites)
{
Write-Host -BackgroundColor Black -ForegroundColor Yellow "Installing the NPM Packages..."
Write-Host -BackgroundColor Black -ForegroundColor Yellow "Installing Azure-CLI"
$output = npm install azure-cli -g
Write-Host -BackgroundColor Black -ForegroundColor Green "Azure-CLI Installed"
Write-Host -BackgroundColor Black -ForegroundColor Yellow "Installing PowerBI-CLI"
$output = npm install powerbi-cli -g
Write-Host -BackgroundColor Black -ForegroundColor Green "PowerBI-CLI Installed"
}

if ($authentication)
{
  Write-Host -BackgroundColor Black -ForegroundColor Yellow "Authentication on Azure selected..."
  #azure login
  #$azureAccountName ="XXXXXXXXXXXXXXXXXXXXXX"
  #$azurePassword = ConvertTo-SecureString "XXXXXXXXXXXXXX=" -AsPlainText -Force
  #$psCred = New-Object System.Management.Automation.PSCredential($azureAccountName, $azurePassword)
  #Login-AzureRmAccount -C -Credential $psCred
  #Add-AzureRmAccount  -Credential $psCred -TenantId 'XXXXXXXXXXXXXXXXx' -ServicePrincipal
  #azure login --service-principal -u "XXXXXXXXXXXXXXXX" --password "XXXXXXXXXXXxx=" --tenant "XXXXXXXXXXXXXXXXXXXXx"
  azure account set "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
  #azure login -u http://myClientEncryptApplication --service-principal --tenant XXXXXXXXXXXXXXXXXXXXXX
  Write-Host -BackgroundColor Black -ForegroundColor Green "Authentication on Azure done"
 }

 try {

 Write-Host -BackgroundColor Black -ForegroundColor Yellow "Getting and storing access key..."
 #$CollectionName =$WorkSpaceCollectionName
 $accesKeyPBIJSON = azure powerbi keys list $ResourceGroupName     $WorkSpaceCollectionName --json
 $accesKeyPBIJSON = $accesKeyPBIJSON | ConvertFrom-Json
 $accesKeyPBI = $accesKeyPBIJSON.key1
 Write-Host -BackgroundColor Black -ForegroundColor Green "Acces Key stored : $accesKeyPBI"

 Write-Host -BackgroundColor Black -ForegroundColor Yellow "Importing the PBIX..."
 $path = $ReportsFilePath +"\Reports\*.pbix" 

 $basename = gi $path | select basename, Name
 $filePath = $ReportsFilePath +"\Reports\" + $basename[0].Name

 $fileName = $basename[0].BaseName

 $output = powerbi import -c $WorkSpaceCollectionName -w $WSguid -k  $accesKeyPBI -n "$fileName" -f "$filePath" 

 Write-Host -BackgroundColor Black -ForegroundColor Green "PBIX Imported : $fileName"
 Write-Host -BackgroundColor Black -ForegroundColor Green "###### Script done ######"
 }
 catch {
 Write-Host $Error[0] -ForegroundColor 'Red'

}

Whenever I run the above script in local machine it will successfully logged into azure portal but when I check in the code into VSTS, power shell script execution failed in the release definition.

Error:

The subscription 'XXXXXXXXX-XXX-XXX-XXX-XXXXXX was not found. Please check your spelling, or use the azure login command to set your subscription. error: Error information has been recorded to C:\Users\buildguest.azure\azure.err

Can you please tell me how to resolve the above error and also tell me how to implement automate login into azure portal from VSTS using power shell script

2

There are 2 answers

2
Shui shengbao On BEST ANSWER

According to your error log, please ensure you should login your account firstly, then execute azure account set "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX". On your local, maybe you had login your account, so, you could execute azure account set "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX" firstly without error log.

If you do this, I suggest you could login your account with service principal. Please refer to this link to create a new service principal.

azure login --service-principal -u "displayNamee" -p "password " --tenant "cliet id"
azure account set "*********************"

enter image description here

1
Pradebban Raja On

For login and selecting a subscription, you can try below.

ASM: Import-AzurePublishSettingsFile Select-AzureSubscription

ARM: Login-AzureRmAccount Select-AzureRMSubscription

Non-interactive logins are detailed here