我正在尝试将 .pbix 文件导入到 azure powerbi 工作区集合中,为此我使用了 azure cli 和 powerbi cli 命令。
用于登录 azure 门户并将 .pbix 文件导入 azure powerbi 工作区集合的 Power shell 脚本:
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'
}
每当我在本地计算机中运行上述脚本时,它都会成功登录到 Azure 门户,但是当我将代码 checkin VSTS 时,Power shell 脚本在发布定义中执行失败。
错误:
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
您能否告诉我如何解决上述错误,并告诉我如何使用 Power shell 脚本从 VSTS 实现自动登录 Azure 门户
最佳答案
根据您的错误日志,请确保您先登录您的帐户,然后执行azure account set "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
。在本地,也许您已经登录了您的帐户,因此,您可以首先执行 azure account set "XXXXXXXXXXXXXXXXXXXXXXXXXX"
不会出现错误日志。
如果您这样做,我建议您可以使用服务主体登录您的帐户。请引用这个link创建新的服务主体。
azure login --service-principal -u "displayNamee" -p "password " --tenant "cliet id"
azure account set "*********************"
关于powershell - 如何使用 azure CLI 和 power shell 脚本实现自动登录,无需用户交互从 VSTS 进入 azure 门户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45936250/