Azure Pipeline中AzurePowerShell@5任务版本格式错误求助(需用Az模块)
Azure Pipeline AzurePowerShell@5 任务版本格式错误解决方案
问题场景
使用AzurePowerShell@5任务执行PowerShell脚本,YAML配置如下:
- task: AzurePowerShell@5 displayName: 'Execute PowerShell Script on Target VMs' inputs: azureSubscription: ${{ variables.SERVICE_CONNECTION_NAME }} pwsh: true ScriptType: 'FilePath' ScriptPath: 'pipeline-scripts/Deploy-ALZDomainControllers.ps1' env: LOCATION: $(LOCATION) TOP_LEVEL_MG_PREFIX: $(TOP_LEVEL_MG_PREFIX) UPSTREAM_RELEASE_VERSION: $(UPSTREAM_RELEASE_VERSION) IS_PULL_REQUEST: $(IS_PULL_REQUEST)
执行时触发版本格式错误,即使手动指定版本(带/不带引号)仍报错,错误信息如下:
##[debug]Caught exception from task script. ##[debug]Error record: ##[debug]The Azure PowerShell version '' specified is not in the correct format. Please check the format. An example of correct format is 1.0.1 ##[debug]At D:\a\_tasks\AzurePowerShell_72a1931b-effb-4d2e-8fd8-f8472a07cb62\3.236.0\AzurePowerShell.ps1:46 char:5 ##[debug]+ throw (Get-VstsLocString -Key InvalidAzurePsVersion -ArgumentList ... ##[debug]+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##[debug] + CategoryInfo : OperationStopped: (The Azure Power...format is 1.0.1:String) [], RuntimeException ##[debug] + FullyQualifiedErrorId : The Azure PowerShell version '' specified is not in the correct format. Please check the format. An example of correct format is 1.0.1 ##[debug] ##[debug]Script stack trace: ##[debug]at <ScriptBlock>, D:\a\_tasks\AzurePowerShell_72a1931b-effb-4d2e-8fd8-f8472a07cb62\3.236.0\AzurePowerShell.ps1: line 46 ##[debug]at <ScriptBlock>, <No file>: line 1 ##[debug]at <ScriptBlock>, <No file>: line 22 ##[debug]at <ScriptBlock>, <No file>: line 18 ##[debug]at <ScriptBlock>, <No file>: line 1 ##[debug]Exception: ##[debug]System.Management.Automation.RuntimeException: The Azure PowerShell version '' specified is not in the correct format. Please check the format. An example of correct format is 1.0.1 ##[error]The Azure PowerShell version '' specified is not in the correct format. Please check the format. An example of correct format is 1.0.1 ##[debug]Processed: ##vso[task.logissue source=TaskInternal;type=error]The Azure PowerShell version '' specified is not in the correct format. Please check the format. An example of correct format is 1.0.1 ##[debug]Processed: ##vso[task.complete result=Failed]
需求:使用Az模块(依赖Az.Control命令,排除已弃用的AzureRM)解决该版本错误。
解决方案
1. 明确指定Az模块版本并启用自动检测
在任务输入中添加azurePowerShellVersion和preferredAzurePowerShellVersion参数,指定具体Az版本号(需与脚本兼容,示例为9.7.0),配置如下:
- task: AzurePowerShell@5 displayName: 'Execute PowerShell Script on Target VMs' inputs: azureSubscription: ${{ variables.SERVICE_CONNECTION_NAME }} azurePowerShellVersion: 'LatestVersion' preferredAzurePowerShellVersion: '9.7.0' pwsh: true ScriptType: 'FilePath' ScriptPath: 'pipeline-scripts/Deploy-ALZDomainControllers.ps1' env: LOCATION: $(LOCATION) TOP_LEVEL_MG_PREFIX: $(TOP_LEVEL_MG_PREFIX) UPSTREAM_RELEASE_VERSION: $(UPSTREAM_RELEASE_VERSION) IS_PULL_REQUEST: $(IS_PULL_REQUEST)
2. 检查自托管代理的Az模块安装状态
如果使用自托管代理,先验证代理机器是否安装了对应版本的Az模块:
Get-Module -ListAvailable Az
若缺失,运行命令安装指定版本:
Install-Module -Name Az -RequiredVersion 9.7.0 -Force -AllowClobber
3. 前置任务验证模块可用性(可选)
若问题仍存在,添加前置任务确认Az.Control模块是否正常加载:
- task: AzurePowerShell@5 displayName: 'Validate Az Module Availability' inputs: azureSubscription: ${{ variables.SERVICE_CONNECTION_NAME }} azurePowerShellVersion: 'LatestVersion' preferredAzurePowerShellVersion: '9.7.0' pwsh: true ScriptType: 'InlineScript' Inline: | Get-Module Az.Control -ListAvailable Write-Host "Az module version: $(Get-Module Az -ListAvailable | Select-Object -First 1 -ExpandProperty Version)"
4. 降级任务版本(临时 workaround)
如果上述方法无效,可降级到AzurePowerShell@4任务,该版本对版本参数的处理更宽松:
- task: AzurePowerShell@4 displayName: 'Execute PowerShell Script on Target VMs' inputs: azureSubscription: ${{ variables.SERVICE_CONNECTION_NAME }} pwsh: true ScriptType: 'FilePath' ScriptPath: 'pipeline-scripts/Deploy-ALZDomainControllers.ps1' azurePowerShellVersion: 'LatestVersion' env: LOCATION: $(LOCATION) TOP_LEVEL_MG_PREFIX: $(TOP_LEVEL_MG_PREFIX) UPSTREAM_RELEASE_VERSION: $(UPSTREAM_RELEASE_VERSION) IS_PULL_REQUEST: $(IS_PULL_REQUEST)
内容的提问来源于stack exchange,提问作者jzryk8
相关产品推荐
相关产品推荐

