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

Viewed 3792

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.
2 Answers

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.

Related