首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Azure DevOps PowerShell任务中使用预定义变量?

如何在Azure DevOps PowerShell任务中使用预定义变量?
EN

Stack Overflow用户
提问于 2020-09-09 02:09:18
回答 2查看 3.5K关注 0票数 1

我正在尝试创建一个简单的Azure DevOps扩展任务,它由一个简单的PowerShell脚本组成,安装一个dotnet tool,然后按如下方式运行:

代码语言:javascript
复制
dotnet tool install dotnet-stryker --tool-path $(Agent.BuildDirectory)/tools

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

但是,当我试图运行我的任务时,我会得到以下错误:

代码语言:javascript
复制
##[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.

我尝试在代码中实现一些小的更改,比如将\替换为/,并将其封装在""中,但最终总是得到相同的结果。

请注意,如果我以内联PowerShell的形式在Azure管道中运行相同的代码,那么这段代码工作得很好。

我在这里错过了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-09-14 23:03:41

结果是,我的问题与Azure Devops变量无关,而是因为我试图像运行Powershell命令一样运行一个字符串。

用一个简单的Invoke-Expression解决了它。

代码语言:javascript
复制
Invoke-Expression "$(Agent.BuildDirectory)/tools/dotnet-stryker"
票数 0
EN

Stack Overflow用户

发布于 2020-09-09 06:37:20

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

代码语言:javascript
复制
$env:Agent_BuildDirectory

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63803805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档