Azure虚拟机规模集代理池Az.Accounts模块缺失问题求助
问题:自托管Azure DevOps代理池(Windows 2022 Scale Set)执行Azure PowerShell任务时找不到Az.Accounts模块
环境与任务配置
- 自托管代理池:基于Windows 2022 Data Centre Edition Scale Set创建
- Azure PowerShell任务参数:
- 任务版本:5.*
- 内嵌脚本:
Write-Output "STUFF WORKS" - Azure PowerShell版本设置:
Latest installed version
报错信息
2023-01-13T10:15:17.6969002Z ##[error]Could not find the modules: 'Az.Accounts' with Version: ''. If the module was recently installed, retry after restarting the Azure Pipelines task agent. 2023-01-13T10:15:19.3308359Z ##[error]PowerShell exited with code '1'.
完整任务执行日志:
2023-01-13T10:15:12.2607076Z ##[section]Starting: Test Task 2023-01-13T10:15:12.2806115Z ============================================================================== 2023-01-13T10:15:12.2806386Z Task : Azure PowerShell 2023-01-13T10:15:12.2806528Z Description : Run a PowerShell script within an Azure environment 2023-01-13T10:15:12.2806753Z Version : 5.209.0 2023-01-13T10:15:12.2806898Z Author : Microsoft Corporation 2023-01-13T10:15:12.2807054Z Help : https://aka.ms/azurepowershelltroubleshooting 2023-01-13T10:15:12.2807227Z ============================================================================== 2023-01-13T10:15:15.3504901Z Generating script. 2023-01-13T10:15:15.3776658Z ========================== Starting Command Output =========================== 2023-01-13T10:15:15.4059002Z ##[command]"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'C:\a\_temp\7baa15ed-a6fe-44a4-a586-ee2342cb9868.ps1'" 2023-01-13T10:15:17.0046974Z Added TLS 1.2 in session. 2023-01-13T10:15:17.6969002Z ##[error]Could not find the modules: 'Az.Accounts' with Version: ''. If the module was recently installed, retry after restarting the Azure Pipelines task agent. 2023-01-13T10:15:19.3308359Z ##[error]PowerShell exited with code '1'. 2023-01-13T10:15:19.7131608Z Added TLS 1.2 in session. 2023-01-13T10:15:21.5490716Z ##[section]Finishing: Test Task
问题原因
- Windows 2022官方基础镜像默认不预装Az模块,你误以为系统自带是错误的认知。
- Azure Pipelines托管代理是微软维护的定制镜像,会自动预安装或按需下载指定版本的Az模块(如你提到的Az 8.3.0),但自托管代理(包括Scale Set创建的实例)没有这个自动安装机制。
- Azure PowerShell任务(5.*版本)的核心逻辑依赖Az.Accounts模块完成Azure环境初始化,即使你的脚本只是输出文本,任务也会尝试加载该模块,当代理机器上无任何Az模块时就会触发报错。
解决方案
方案1:Scale Set实例创建时预安装Az模块(推荐)
通过Scale Set的自定义脚本扩展,在虚拟机实例初始化时全局安装Az模块,一劳永逸:
- 创建PowerShell脚本
Install-AzModules.ps1:
# 安装NuGet包提供程序 Install-PackageProvider -Name NuGet -Force # 信任PSGallery仓库 Set-PSRepository -Name PSGallery -InstallationPolicy Trusted # 安装完整Az模块套件 Install-Module -Name Az -AllowClobber -Force
- 将该脚本配置为Scale Set的自定义脚本扩展,确保所有新创建的代理实例都会自动执行安装。
方案2:任务前置步骤临时安装Az模块(适合测试场景)
在Azure PowerShell任务前添加一个普通PowerShell任务,执行模块安装:
Install-PackageProvider -Name NuGet -Force Set-PSRepository -Name PSGallery -InstallationPolicy Trusted # 仅安装所需的Az.Accounts模块 Install-Module -Name Az.Accounts -AllowClobber -Force -SkipPublisherCheck
注意:此方法每次运行任务都会执行安装,会增加任务执行时间,不适合生产环境高频运行的任务。
方案3:替换为普通PowerShell任务(如果脚本无需Azure操作)
如果你的脚本只是执行非Azure相关操作(如示例中的输出文本),直接改用PowerShell任务替代Azure PowerShell任务,避免强制加载Az.Accounts模块。
内容的提问来源于stack exchange,提问作者CM231
相关产品推荐
相关产品推荐

