Fal*_*lco 6 powershell azure azure-cli azure-devops azure-pipelines
情况
我的自托管 Windows 代理运行来自 Azure DevOps 的管道。为了管理 Azure 中的资源,我想使用 Azure CLI 任务。即使在前面的步骤中安装了 Azure CLI,AzureCLI 任务也会失败。
我有两个从我的管道运行的脚本。
2021-03-05T14:50:02.5986237Z ##[error]Azure CLI 2.x is not installed on this machine.
2021-03-05T14:50:02.6391547Z ##[error]Script failed with error: Error: Unable to locate executable file: 'az'. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also verify the file has a valid extension for an executable file.
Run Code Online (Sandbox Code Playgroud)
微软说
After you install new software on an agent, you must restart the agent for the new capability to show up in the pool so that the build can run.After the installation is complete, you will need to reopen PowerShell to use the Azure CLI.AzureCLI 任务无法找到已安装的 Azure CLI 可执行文件。如何修复此问题以便可以运行 AzureCLI 任务?
我已经尝试过的
管道详情
trigger:
branches:
exclude:
- master
pool:
name: SelfHosted-AgentPool
vmImage: 'windows-latest'
variables:
environment.name: 'Test'
stages:
- stage: build_and_deploy
jobs:
- deployment: VMBackup_Testing
displayName: "Enable Backup Protection"
environment: '$(environment.name)'
strategy:
runOnce:
deploy:
steps:
- checkout: self
- task: PowerShell@2
inputs:
filePath: '$(System.DefaultWorkingDirectory)/Templates/Snippets/InstallAzureCLI.ps1'
- task: AzureCLI@2
inputs:
workingDirectory: 'C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin'
azureSubscription: 'XXX'
scriptType: 'ps'
scriptLocation: 'scriptPath'
scriptPath: '$(System.DefaultWorkingDirectory)/Templates/Snippets/EnableBackupProtection.ps1'
Run Code Online (Sandbox Code Playgroud)
安装 Azure CLI 脚本
# Download and Install Azure CLI
Invoke-WebRequest -Uri https://azcliprod.blob.core.windows.net/msi/azure-cli-2.19.1.msi -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList "/I AzureCLI.msi /quiet"; rm .\AzureCLI.msi
# Update PATH for Powershell to use new installed software
setx /M PATH "$env:Path += ;C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin"
# Test if PATH of Azure CLI exists
Test-Path -Path "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin"
# Reload Shell with new PATH
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
# Check if AZ CLI is installed
az version
Run Code Online (Sandbox Code Playgroud)
Azure CLI 命令脚本
# Check if script gets executed
Write-Host "Hello, World!"
# AZ CLI commands to enable Backup Protection
az backup protection enable-for-vm `
--resource-group XXX`
--vault-name XXXX`
--vm $(az vm show -g XXX -n XXX --query id) `
--policy-name DailyBackup
Run Code Online (Sandbox Code Playgroud)
为什么每次在同一个自承载 Windows 代理上运行管道时都需要安装 Azure CLI?
与Microsoft 托管代理不同,您只需在自托管代理计算机上手动安装所需的工具,然后就可以在代理上运行的管道中使用它们。
登录到安装了自承载代理的 Windows 计算机(本地或虚拟机)。
打开 Web 浏览器,从此处下载已发布的最新 Azure CLI 的 MSI 安装程序。
通过 MSI 安装程序安装 Azure CLI 时,通常安装向导会自动将此工具添加到系统环境变量PATH中。安装完成后,可以在机器上打开“编辑系统环境变量”进行检查。如果没有添加到系统环境变量PATH中,可以手动添加。
通过这种方式,当您设置管道在此自托管代理上运行时,您可以直接调用 Azure CLI,而无需在管道中安装 Azure CLI 的任何步骤。
| 归档时间: |
|
| 查看次数: |
13769 次 |
| 最近记录: |