0
votes

I'm trying to create a simple Azure DevOps Extension task which consists of a simple PowerShell script installing a dotnet tool and then running it like this:

dotnet tool install dotnet-stryker --tool-path $(Agent.BuildDirectory)/tools

$(Agent.BuildDirectory)/tools/dotnet-stryker

But when I try to run my task I get the following error:

##[error]System.Management.Automation.ParseException: At D:\a\_tasks\run-stryker_400ea42f-b258-4da4-9a55-68b174cae84c\0.14.0\run-stryker.ps1:17 char:25
##[debug]Processed: ##vso[task.logissue type=error;]System.Management.Automation.ParseException: At D:\a\_tasks\run-stryker_400ea42f-b258-4da4-9a55-68b174cae84c\0.14.0\run-stryker.ps1:17 char:25
+ $(Agent.BuildDirectory)/tools/dotnet-stryker
+                         ~
You must provide a value expression following the '/' operator.

At D:\a\_tasks\run-stryker_400ea42f-b258-4da4-9a55-68b174cae84c\0.14.0\run-stryker.ps1:17 char:25
+ $(Agent.BuildDirectory)/tools/dotnet-stryker
+                         ~~~~~~~~~~~~~~~~~~~~
Unexpected token 'tools/dotnet-stryker' in expression or statement.
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)
   at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at Microsoft.TeamFoundation.DistributedTask.Handlers.LegacyVSTSPowerShellHost.VSTSPowerShellHost.Main(String[] args)
##[error]LegacyVSTSPowerShellHost.exe completed with return code: -1.

I've tried to implement small changes in my code like changing \ for / and warping it around "" but I end up always getting the same result.

Note that this same code works just fine if I run it inside an Azure Pipeline as inline PowerShell.

What am I missing here?

3
Have you tried changing it to cd $(Agent.BuildDirectory)/tools and then just run dotnet-stryker? - Yan Sklyarenko
Not am option as I need to be able to run this task from specific working directories. - Rogerio Schmitt
How about the issue? Does the answer below resolved your question, If not, would you please let me know the latest information about this issue? - Leo Liu-MSFT
@LeoLiu-MSFT it did not solved my issue. Just posted what did as my own answer. - Rogerio Schmitt

3 Answers

3
votes

In a PowerShell script (it inline) you need to use the following syntax:

$env:Agent_BuildDirectory

Reference: https://docs.microsoft.com/en-us/azure/devops/pipelines/release/variables?view=azure-devops&tabs=batch#using-default-variables

1
votes

How to use predefined variables in Azure DevOps PowerShell Task?

You could try to pass this predefined variable as a input in the task.json file, like:

  "inputs": [
    {
      "name": "BuildDirectory",
      "type": "string",
      "label": "The Build Directory",
      "defaultValue": "$(Agent.BuildDirectory)",
      "required": true,
      "helpMarkDown": "The folder of the test project to execute(e.g. Sample.Tests.csproj)."
    },

Then in the run-stryker.ps1 file:

param(
    [string] $BuildDirectory
)
0
votes

It ended up being that my issue was not related with Azure Devops variables, but with the fact that I was trying to run a string as if it was a Powershell command.

Solved it with a simple Invoke-Expression.

Invoke-Expression "$(Agent.BuildDirectory)/tools/dotnet-stryker"