c# - 具有自托管代理的 Azure DevOps Pipeline 无法运行任务

标签 c# azure devops pipeline

我正在尝试设置在 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 中的自托管代理,如下所示:-

enter image description here

enter image description here

自托管代理在 Azure DevOps 中在线:-

Note- It is a part of Default Pool in my Azure DevOps org

enter image description here

现在,我在 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 脚本成功运行,如下所示:-

enter image description here

现在,当我运行另一个 SQL Inline 脚本并将我的池设置为另一个代理或 ubuntu-image 时,我得到了与您相同的错误代码:-

trigger:

- main

  

pool:

vmImage: ubuntu-latest

管道输出:-

enter image description here

当我将池更改为 Windows 时,错误已解决并且任务成功运行,如下所示:-

trigger:

- main

  

pool:

vmImage: windows-latest

管道输出:-

enter image description here

确保选择正确的池,并且 Powershell 模块已正确安装在充当 Azure DevOps 代理的虚拟机内。

引用:-

The current operating system is not capable of running this task. That typically means the task was written for Windows only. For example, written for Windows Desktop PowerShell. – AzureFileCopy@4 | What I Broke – Programming and Web Development

关于c# - 具有自托管代理的 Azure DevOps Pipeline 无法运行任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75324950/

相关文章:

c# - 如何在 Worker Role 托管的 Web API 中获取客户端服务器的唯一标识符

postgresql - 如何使用 Docker Postgres :9. 3 图像将数据存储在外部驱动器中?

ruby - Chef ruby​​_block 执行导致错误

ansible - 错误: mapping values are not allowed in this context

c# - 将 xUnit 项目添加到 Unity 项目时,单元测试是不确定的

c# - 如何在 C# 中的 IF 语句中使用 Or?

postgresql - 有没有办法将 PostgreSQL 数据库从 Azure 迁移到 AWS RDS PostgreSQL

c# - IdentityServer3 的 ASP.NET 身份插件 UI (IdentityManager) 在部署到 Azure (WebApp) 后不起作用

c# - 通过 REST API 上传文件时修复 Artifactory 中的校验和

c# - 关于将类型 'int' 隐式转换为 'char' ,为什么 `s[i] += s[j]` 和 `s[i] = s[i]+s[j] ` 不同