我正在尝试设置在 Azure 中的虚拟机上运行的 Azure DevOps 管道。我正在 azure 中进行构建,并将工件下载到虚拟机进行部署。但是,当我尝试运行 web.config 转换或文件复制等任务时,我收到如下错误:
##[错误]当前操作系统无法运行此任务。这通常意味着该任务是仅为 Windows 编写的。例如,为 Windows 桌面 PowerShell 编写。 ##[debug]System.InvalidOperationException:当前操作系统无法运行此任务。这通常意味着该任务是仅为 Windows 编写的。例如,为 Windows 桌面 PowerShell 编写。
这很奇怪,因为虚拟机运行的是 Windows。 我已确保安装最新版本的 powershell
最佳答案
使用适用于 Windows 的自托管代理后,请确保已将 Windows 引用为 YAML 管道中的主机池。另外,如果您使用的是 azure PowerShell 任务,请确保您已在充当自托管代理的 VM 中安装了 Powershell 模块。
我在 Azure 门户中创建了一个 Windows VM(Windows Server 2019 Datacenter Gen2),并使用 PAT token 授权它作为 Azure DevOps 中的自托管代理,如下所示:-
自托管代理在 Azure DevOps 中在线:-
Note- It is a part of Default Pool in my Azure DevOps org
现在,我在 Azure DevOps 中使用此代理运行了 Azure-Powershell 内联脚本:-
Yaml 管道代码:-
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
- main
pool:
name: Default
steps:
- script: echo Hello, world!
displayName: 'Run a one-line script'
- script: |
echo Add other tasks to build, test, and deploy your project.
echo See https://aka.ms/yaml
displayName: 'Run a multi-line script'
- task: AzurePowerShell@5
inputs:
azureSubscription: 'SID subscription(xxxxxx365-f598-44d6-xxxx-e2b6e97cb2a7)'
ScriptType: 'InlineScript'
Inline: 'Get-AzResource | Format-Table'
azurePowerShellVersion: 'LatestVersion'
Note:- I have added pool as Default where my agent pool is connected
in Azure DevOps, Make sure you use the correct pool here in your YAML
code:-
pool:
name: Default
Azure Powershell 脚本成功运行,如下所示:-
现在,当我运行另一个 SQL Inline 脚本并将我的池设置为另一个代理或 ubuntu-image 时,我得到了与您相同的错误代码:-
trigger:
- main
pool:
vmImage: ubuntu-latest
管道输出:-
当我将池更改为 Windows 时,错误已解决并且任务成功运行,如下所示:-
trigger:
- main
pool:
vmImage: windows-latest
管道输出:-
确保选择正确的池,并且 Powershell 模块已正确安装在充当 Azure DevOps 代理的虚拟机内。
引用:-
关于c# - 具有自托管代理的 Azure DevOps Pipeline 无法运行任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75324950/