2

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 exchanging \ for / and wrapping it in "" 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?

4
  • Have you tried changing it to cd $(Agent.BuildDirectory)/tools and then just run dotnet-stryker? Commented Sep 9, 2020 at 5:32
  • Not am option as I need to be able to run this task from specific working directories. Commented Sep 9, 2020 at 12:02
  • 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? Commented Sep 10, 2020 at 9:57
  • @LeoLiu-MSFT it did not solved my issue. Just posted what did as my own answer. Commented Sep 14, 2020 at 23:04

3 Answers 3

3

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

$env:Agent_BuildDirectory

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

Sign up to request clarification or add additional context in comments.

2 Comments

Tried your solution, but I get the same error. You can check my full code here: github.com/raschmitt/run-stryker-task/blob/master/run-stryker/…
@RogerioSchmitt try $($env:Agent_BuildDirectory)/tools
1

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
)

Comments

0

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.