[英]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.