简体   繁体   English

如何使用azure CLI和Power Shell脚本实现自动登录而无需用户从VSTS进入azure门户

[英]How to implement automated login without user interaction into azure portal from VSTS using azure CLI & power shell script

I am trying to import .pbix file into azure powerbi workspace collection, for that I used the azure cli and powerbi cli commands. 我正在尝试将.pbix文件导入azure powerbi工作区集合,因为我使用了azure cli和powerbi cli命令。

Power shell script for logging into azure portal and import the .pbix file into azure powerbi workspace collection: 用于登录到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'

} }

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. 每当我在本地计算机上运行以上脚本时,它将成功登录到azure门户,但是当我将代码签入VSTS时,在发行版定义中Power Shell脚本执行失败。

Error: 错误:

The subscription 'XXXXXXXXX-XXX-XXX-XXX-XXXXXX was not found. 找不到订阅“ XXXXXXXXX-XXX-XXX-XXX-XXXXXX”。 Please check your spelling, or use the azure login command to set your subscription. 请检查您的拼写,或使用azure登录命令设置您的订阅。 error: Error information has been recorded to C:\\Users\\buildguest.azure\\azure.err 错误:错误信息已记录到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 您能告诉我如何解决上述错误,还可以告诉我如何使用Power Shell脚本实现从VSTS到Azure门户的自动登录

According to your error log, please ensure you should login your account firstly, then execute azure account set "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX" . 根据您的错误日志,请确保您首先登录您的帐户,然后执行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. 在本地,也许您已经登录了帐户,因此,您可以先执行azure account set "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"而不会出现错误日志。

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 "*********************"

在此处输入图片说明

For login and selecting a subscription, you can try below. 要登录并选择订阅,您可以尝试以下操作。

ASM : Import-AzurePublishSettingsFile Select-AzureSubscription ASM :Import-AzurePublishSettingsFile Select-AzureSubscription

ARM : Login-AzureRmAccount Select-AzureRMSubscription ARM :登录-AzureRmAccount选择-AzureRM订阅

Non-interactive logins are detailed here 非交互式登录在这里详细说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 安装 Azure CLI 并在同一脚本中运行 az login 命令,无需重新启动电源 shell - Install Azure CLI and run az login command in same script with out restarting the power shell 使用Azure Power Shell或Azure CLI创建日志分析警报 - To create the Log Analytics alerts using Azure Power Shell or Azure CLI 如何使用 Power Shell 脚本访问 Azure 中的 Kudu - How to access Kudu in Azure using power shell script 在Azure中运行Power Shell脚本 - Running a power shell script in Azure Azure DevOps Pipeline - Power shell脚本,使用变量复制文件 - Azure DevOps Pipeline - Power shell script , Copy Files using Variables 使用Power Shell从Azure存储中导入发布设置文件 - Importing publish settings file from azure storage using power shell 为什么我无法使用从ASP.Net代码执行的Power Shell命令登录到Azure? - Why am I not able to login to Azure using power shell commands that are being executed from ASP.Net code? Azure Power Shell-如何更改网络子网 - Azure Power Shell - How to change network subnet 无法注销并使用Azure Power Shell执行非交互式登录 - Not able logout and do a non interactive login with Azure power shell 是否有任何 PowerShell 脚本或命令可以从 Azure 门户获取租户中所有用户访问角色的报告? - Is there any PowerShell script or command to get a report of all the user's access role in tenant wise from Azure portal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM