3

I am using the below code to retrieve the build id of the latest successful build from the azure pipeline . But throws the below error .Please help in fixing the issue

code :

  - task: PowerShell@2
    inputs:
      targetType: 'inline'
      script: 
        $organization = "org_name" 
        $project = "project_name" 
        $definitionid = "27"
        $pat = "eq7pqxcycdp6crbrygwh73ua2kzdvsett3p2sjcx5j"
        $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $pat)))
        $baseUrl = "https://dev.azure.com/$organization/$project/_apis/build/latest/$definitionid?api-version=5.1-preview.1" 
        $latestbuild = Invoke-RestMethod -Uri $baseUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}  -Method GET 

        Write-Host $latestbuild.id
        $id1 = $latestbuild.id
        Write-Host "##vso[task.setvariable variable=buildid]$id1"

Error :

##[error]Unable to locate executable file: 'pwsh'. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also check the file mode to verify the file is executable.
JCDani
  • 307
  • 7
  • 20

3 Answers3

4

You need to install Powershell Core on your machine that hosts Azure DevOps agent. This adds pwsh to the PATH, which will fix your build. Powershell Core Installation guide for Linux.

Michal Rosenbaum
  • 1,801
  • 1
  • 10
  • 18
1

You're using the bash task, but your script is PowerShell. PowerShell scripts and Bash scripts are not the same thing and can't be used interchangeably. Use a powershell (or pwsh for PowerShell Core) task instead.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
  • I have modified the code and now I get a different error "##[error]Unable to locate executable file: 'pwsh'. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also check the file mode to verify the file is executable." How to resolve this ? – JCDani Aug 08 '21 at 04:07
  • @JCDani You need to install PowerShell. – Daniel Mann Aug 08 '21 at 18:26
1

You need to install the PowerShell core module to your target agent, you can do it easily with bash command:

Install PowerShell

sudo apt-get install -y powershell

After PowerShell install bash task, you should create a PowerShell task it can be Azure CLI as well if you want (just use PowerShell core)enter image description here just above your code you have to add #!/usr/bin/env pwsh

This fixed my similar issue.

koko91kon
  • 88
  • 1
  • 6