如何使用azure CLI& amp;和VSTS在没有用户交互的情况下实现自动登录。电源shell脚本

时间:2017-08-29 10:05:03

标签: powershell azure powerbi azure-cli

我正在尝试将 .pbix 文件导入到azure powerbi工作区集合中,因为我使用了azure cli和powerbi cli命令。

Power shell脚本,用于登录azure portal并将 .pbix 文件导入azure 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 portal,但是当我将代码签入VSTS时,power shell脚本执行在发布定义中失败。

错误:

  

未找到订阅' XXXXXXXXX-XXX-XXX-XXX-XXXXXX。请检查您的拼写,或使用azure login命令设置您的订阅。   错误:错误信息已记录到C:\ Users \ buildguest.azure \ azure.err

您能否告诉我如何解决上述错误,并告诉我如何使用power shell脚本从VSTS实现自动登录到azure门户

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

要登录并选择订阅,您可以尝试以下。

<强> ASM : 进口AzurePublishSettingsFile 选择-AzureSubscription

<强> ARM : 登录-AzureRmAccount 选择-AceanRMSubscription

非交互式登录详细here