azure-devopsazure-pipelines-release-pipelineazure-pipelines-release-taskwindows-server-2022

Azdo Releases Powershell Inline vs File Path


I am using Azure DevOps Deployment Group Agent to deploy Azure DevOps Releases Tasks.

Is there a way to utilize Azure DevOps built-in variables, such as $(System.DefaultWorkingDirectory), within PowerShell files when executing them through the Azure DevOps Releases task, akin to their accessibility when using inline PowerShell within the same task?

Background of the issue:

The issue is that when I run Inline Powershell scripts, it works fine. But when I move the same script to a file and try to consume the file from the repo that is located in $(System.DefaultWorkingDirectory) which is the agent host server, it can not find it.


Solution

  • When you run Azure DevOps built-in variables like $(System.DefaultWorkingDirectory) via a file on an on-prem agent, you need to add $env before the variable. Here's an example of how you can access $(System.DefaultWorkingDirectory) in a PowerShell file:

    $workingDirectory = $env:System_DefaultWorkingDirectory
    Write-Host "Working Directory: $workingDirectory"
    

    $env is valid and also applies to other Azure DevOps build-in variables in PowerShell when running in the same context.