【问题标题】:How to use predefined variables in Azure DevOps PowerShell Task?如何在 Azure DevOps PowerShell 任务中使用预定义变量?
【发布时间】:2020-09-09 02:09:18
【问题描述】:

我正在尝试创建一个简单的 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 运行,同样的代码也可以正常工作。

我在这里错过了什么?

【问题讨论】:

  • 您是否尝试将其更改为cd $(Agent.BuildDirectory)/tools,然后运行dotnet-stryker
  • 不是选项,因为我需要能够从特定的工作目录运行此任务。
  • 这个问题怎么样?下面的答案是否解决了您的问题,如果没有,请告诉我有关此问题的最新信息吗?
  • @LeoLiu-MSFT 它没有解决我的问题。刚刚发布了我自己的答案。

标签: powershell azure-devops azure-pipelines


【解决方案1】:

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

$env:Agent_BuildDirectory

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

【讨论】:

【解决方案2】:

如何在 Azure DevOps PowerShell Task 中使用预定义变量?

您可以尝试将此预定义变量作为task.json 文件中的输入传递,例如:

  "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)."
    },

然后在run-stryker.ps1文件中:

param(
    [string] $BuildDirectory
)

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      • 2020-05-03
      • 2020-12-01
      • 2020-06-21
      • 2021-11-04
      • 2019-04-02
      • 2021-11-04
      相关资源
      最近更新 更多