Search code examples
c#azuredevopspipeline

Azure DevOps Pipeline with selfhosted agent fails to run tasks


I am trying to set up an Azure devops pipeline that runs on a virtual machine in Azure. I'm doing the build in azure and the download the artifact to the VM for deployment. However, when I try to run tasks like web.config transform or file copy, I get an error like this:

##[error]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. ##[debug]System.InvalidOperationException: 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.

This quite odd since the VM is running windows. I have made sure to install the latest version of powershell


Solution

  • After you use a self-hosted agent for Windows, Make sure you have referenced windows as a host pool in your YAML pipeline. Also, If you’re using the azure PowerShell task, Make sure you have installed the Powershell module in your VM acting as a self-hosted agent.

    I created one windows VM (Windows Server 2019 Datacenter Gen2) in Azure Portal and authorized it to work as a self-hosted agent in Azure DevOps with the PAT token like below:-

    enter image description here

    enter image description here

    Self hosted agent is online in Azure DevOps:-

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

    enter image description here

    Now, I ran an Azure-Powershell inline script with this Agent in Azure DevOps :-

    Yaml pipeline code:-

    # 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
    
    

    The Azure Powershell script ran successfully like below:-

    enter image description here

    Now, when I ran another SQL Inline script with my pool set to another agent OR ubuntu-image, I got same error code as yours:-

    trigger:
    
    - main
    
      
    
    pool:
    
    vmImage: ubuntu-latest
    
    

    Pipeline Output:-

    enter image description here

    When I changed the pool to windows, the error was resolved and the task ran successfully like below:-

    trigger:
    
    - main
    
      
    
    pool:
    
    vmImage: windows-latest
    
    

    Pipeline Output:-

    enter image description here

    Make sure you’re selecting the correct pool and the Powershell module is installed properly inside that VM which is acting as an Azure DevOps agent.

    Reference:-

    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