简体   繁体   English

Azure 管道,托管 Ubuntu 代理和 Azure CLI 任务与 Z3D265814E1EEEF0DCCZ7 正确使用模块和验证

[英]Azure Pipelines, Hosted Ubuntu Agent and Azure CLI Task with PowerShell Core: Use Az module and authenticate properly

I have a couple of PowerShell scripts that use the PowerShell Az module ( Link ).我有几个使用 PowerShell Az 模块( 链接)的 PowerShell 脚本。 As a result of certain requirements it is now necessary to run these scripts on a hosted Ubuntu agent (18.04).由于某些要求,现在需要在托管的 Ubuntu 代理 (18.04) 上运行这些脚本。

So I selected the Azure CLI task with PowerShell Core as Script type所以我选择了 Azure CLI 任务,其中 PowerShell Core 作为脚本类型

在此处输入图像描述

The first issue I encounter is that the agent does not know the Az cmdlets.我遇到的第一个问题是代理不知道 Az cmdlet。 The second issue is that even when the Az cmdlets are available there is no authenticated context to work with them.第二个问题是,即使 Az cmdlet 可用,也没有经过身份验证的上下文可以使用它们。

How can I resolve these two issues?我该如何解决这两个问题?

First it is necessary to install the Az module on the hosted Ubuntu agent.首先需要在托管的 Ubuntu 代理上安装 Az 模块。 This can be done with a Azure CLI task with script type PowerShell Core by executing这可以通过执行脚本类型为 PowerShell Core 的 Azure CLI 任务来完成

Install-Module -Name Az -AllowClobber -Force

To then be able to authenticate it is necessary to enable on the task然后能够进行身份验证,有必要在任务上启用

在此处输入图像描述

and then in the PowerShell script to authenticate via然后在 PowerShell 脚本中通过

$tenantIdEnv = $env:tenantId
$spnIdEnv = $env:servicePrincipalId
$spnKeyEnvSecureString = ConvertTo-SecureString $env:servicePrincipalKey -AsPlainText -Force
$credentialsServicePrincipal = New-Object System.Management.Automation.PSCredential($spnIdEnv, $spnKeyEnvSecureString)
Connect-AzAccount -ServicePrincipal -TenantId $tenantIdEnv -Credential $credentialsServicePrincipal

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

相关问题 Azure Powershell模块-Linux(.NET Core)支持 - Azure Powershell Module - Linux (.NET Core) Support Azure DevOps:“托管Linux预览”和“托管Ubuntu 1604”代理池之间有什么区别? - Azure DevOps: What's the difference between the “Hosted Linux Preview” and “Hosted Ubuntu 1604” agent pools? 如何使用自制软件在 Ubuntu 20.04 中正确安装 azure-cli? - How to properly install azure-cli in Ubuntu 20.04 using homebrew? PowerShell 7 在 Linux 上,使用证书对 Azure 进行身份验证 - PowerShell 7 on Linux, using Certificate to Authenticate to Azure 为 Azure devops 服务器安装自托管 Linux 代理 - Installing Self Hosted Linux Agent for Azure devops Server ModuleNotFoundError:没有名为“pyodbc”的模块 - Azure 批次 - Ubuntu - ModuleNotFoundError: No module named 'pyodbc' - Azure Batch - Ubuntu Azure 构建代理 - Azure build Agent 如何通过 bash 检查 Linux 上自托管 Azure DevOps 代理上正在运行的作业 - How to check running job on self-hosted Azure DevOps Agent on Linux via bash Azure Devops 管道,知道管道何时完成的方法,使用自托管代理 - Azure Devops pipeline , way to know when pipeline is done , using self hosted agent 在Hosted Linux Agent上运行VSTS构建期间挂载Azure文件存储失败 - Mounting Azure file storage during VSTS build running on Hosted Linux Agent fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM