Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve Unable to locate executable file: 'pwsh'. Please verify either the file path exists error when getting buildid with Azure rest API?

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.
like image 757
JCDani Avatar asked Oct 19 '25 19:10

JCDani


1 Answers

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.

like image 185
Michal Rosenbaum Avatar answered Oct 22 '25 14:10

Michal Rosenbaum