首页 > 解决方案 > 如何在 Azure DevOps PowerShell 任务中使用预定义变量?

问题描述

我正在尝试创建一个简单的 Azure DevOps 扩展任务,该任务包括一个简单的 PowerShell 脚本,安装一个dotnet tool然后像这样运行它:

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

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

但是当我尝试运行我的任务时,我收到以下错误:

##[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.

我试图在我的代码中实现小的更改,例如更改\/扭曲它,""但我最终总是得到相同的结果。

请注意,如果我在 Azure Pipeline 中将其作为内联 PowerShell 运行,同样的代码也可以正常工作。

我在这里想念什么?

标签: powershellazure-devopsazure-pipelines

解决方案


在 PowerShell 脚本(内联)中,您需要使用以下语法:

$env:Agent_BuildDirectory

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


推荐阅读