azure-devops - Azure Powershell - Az 模块在 Ubuntu 托管的构建代理上不起作用
问题描述
我有一个在 Ubuntu 16.04 托管构建代理上运行在 Azure DevOps 中的构建。我正在使用最新版本的“Azure Powershell”任务(版本 4.* 预览版),它应该是多平台的,支持 Powershell 核心,并支持使用Azure Powershell Az 模块。
但是,它并不完全奏效。在运行我的任何脚本之前,它会出错:
##[section]Starting: Azure PowerShell script: InlineScript
==============================================================================
Task : Azure PowerShell
Description : Run a PowerShell script within an Azure environment
Version : 4.0.0
Author : Microsoft Corporation
Help : [More Information](https://go.microsoft.com/fwlink/?LinkID=613749)
==============================================================================
##[warning]Can\'t find loc string for key: GeneratingScript
GeneratingScript
[command]/usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command . '/home/vsts/work/_temp/e66222aa-283d-4dfd-b5c1-f1d2a4a3ba9f.ps1'
Could not find the module Az.Accounts with given version. If the module was recently installed, retry after restarting the Azure Pipelines task agent.
At /home/vsts/work/_tasks/AzurePowerShell_72a1931b-effb-4d2e-8fd8-f8472a07cb62/4.0.0/InitializeAz.ps1:25 char:5
+ throw ("Could not find the module Az.Accounts with given version. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (Could not find ...nes task agent.:String) [], RuntimeException
+ FullyQualifiedErrorId : Could not find the module Az.Accounts with given version. If the module was recently installed, retry after restarting the Azure Pipelines task agent.
##[error]PowerShell exited with code '1'.
##[error]PowerShell wrote one or more lines to the standard error stream.
##[section]Finishing: Azure PowerShell script: InlineScript
Az Powershell 模块似乎在 Windows VS2017 托管代理上正常工作/加载,但在 Ubuntu 上没有运气。有关解决此问题的任何建议吗?
解决方案
通过添加在构建代理上安装 Az Powershell 模块的先前构建步骤,我能够让 Az Powershell 在 Ubuntu 代理上的 Azure DevOps 构建中工作。
我添加了一个 powershell 脚本来安装 Az 模块和卸载 Azure-Rm 模块;我从命令行任务中调用它,这样我就可以将它包装起来,sudo
使其成为全局更改。
这是命令行任务(YAML):
steps:
- displayName: 'Install Az Powershell Modules'
script: |
sudo /usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -File "$(Build.Repository.LocalPath)/build/install-az-modules.ps1"
这是build/install-az-modules.ps1
脚本:
<#
.SYNOPSIS
Build agent script to install Az Powershell modules. This script should be run as sudo.
On a linux build agent, this command can be run as:
sudo /usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -Command . '$(Build.Repository.LocalPath)/build/install-az-modules.ps1'
#>
# Disable status info to clean up build agent stdout
$global:ProgressPreference = 'SilentlyContinue'
$global:VerbosePreference = "SilentlyContinue"
$azureRmModule = Get-InstalledModule AzureRM -ErrorAction SilentlyContinue
if ($azureRmModule) {
Write-Host 'AzureRM module exists. Removing it'
Uninstall-Module -Name AzureRM -AllVersions
Write-Host 'AzureRM module removed'
}
Write-Host 'Installing Az module...'
Install-Module Az -Force -AllowClobber
if (Get-Command Uninstall-AzureRm -ErrorAction SilentlyContinue) {
Write-Host 'Running Uninstall-AzureRm...'
Uninstall-AzureRm
}
推荐阅读
- html - 如何使空跨度可悬停?
- bash - 使用 bash 中的 openssl 以编程方式针对链和任意时间戳验证证书(用于续订)
- python - 从数组的数组中访问 n 元素来确定获胜者
- css - 将图片添加到图库页面的问题
- python - 如果我通过 conda 安装 tf,是否需要手动为 tensorflow-gpu 安装 CUDA 驱动程序
- qt - 如何在 Qt3d 中手动添加骨架?
- foreach - 在 Make 中的 foreach 调用中调用宏两次
- javascript - 按属性过滤 GeoJson 数据以获取数据子集
- python - 有没有办法保存 OSMnx 中生成的等时图?
- java - Java 简单的圣诞树