2017-08-29 40 views
0

我試圖導入.pbix將文件導入到azure powerbi工作區集合中,爲此我使用了azure cli和powerbi cli命令。如何使用azure CLI和電源外殼腳本在沒有用戶交互的情況下將自動登錄從VSTS實現到Azure門戶

用於登錄到蔚藍的門戶和 .pbix文件導入到蔚藍powerbi工作區集合

電源外殼腳本:

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門戶,但是當我將代碼簽入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

能否請你告訴我如何解決上面的錯誤,並告訴我如何實現自動化登錄到使用電源外殼腳本

回答

1

根據您的錯誤日誌,請確保您應該首先登錄您的帳戶執行azure account set "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"。在你的本地,也許你已經登錄了你的賬戶,所以,你可以先執行azure account set "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX",不用錯誤日誌。

如果你這樣做,我建議你可以用服務主管登錄你的賬戶。請參閱此link以創建新的服務主體。

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

enter image description here

+0

感謝@Walter,解決了登錄問題。但是我正在面臨的問題是「npm WARN棄用[email protected]:改用uuid模塊」,當我執行這個命令時npm install powerbi-cli -g。你能告訴我如何解決這個錯誤? – pradeep

+0

@pradeep是的,我遇見了同樣的錯誤日誌。它似乎有力量問題。對不起,我沒有這個工作,但我試圖找到根源。 –

0

對於登錄VSTS蔚藍門戶並選擇訂閱,您可以在下面嘗試。

ASM: 進口AzurePublishSettingsFile 選擇-AzureSubscription

ARM: 登錄-AzureRmAccount 選擇-AzureRMSubscription

非交互式登錄是詳細here

+0

錯誤日誌和'蔚藍帳戶設置 「XXXXXXXXXXXXXXXXXXXXXXXXXXXXX」'不是從PowerShell中。 –

相關問題