无法在 Devops 步骤中设置 powershell 输出变量

Cannot set powershell output variables in Devops step

我正在尝试使用 powershell 脚本的输出变量。我正在使用经典 UI 在线使用 Devops,并在发布管道中尝试了 powershell 4.* 和 Powershell 5.* 任务。

我正在使用一个自托管代理,它正在运行并且可以很好地执行许多其他构建和发布 powershell 的工作。 Azure Powershell 模块版本 3.5.0(现在不使用 4.x 是有原因的)。

为简化起见,这里是我的测试内联脚本总共......:

Write-Host '##vso[task.setvariable variable=MobileAppInsightsKey;isOutput=true;]thisisthekey'
Write-Host "This is host"

Write-Output '##vso[task.setvariable variable=MobileAppInsightsKey;isOutput=true;]thisisthekey'
Write-Output "This is output"

这是 Azure powershell 任务的输出。 (4.*)

2020-07-01T00:06:57.2970494Z ##[section]Starting: Azure PowerShell script: InlineScript
2020-07-01T00:06:57.3335882Z 
==============================================================================
2020-07-01T00:06:57.3336692Z Task         : Azure PowerShell
2020-07-01T00:06:57.3337292Z Description  : Run a PowerShell script within an Azure environment
2020-07-01T00:06:57.3337566Z Version      : 4.171.1
2020-07-01T00:06:57.3338039Z Author       : Microsoft Corporation
2020-07-01T00:06:57.3338575Z Help         : https://aka.ms/azurepowershelltroubleshooting
2020-07-01T00:06:57.3338930Z 
==============================================================================
2020-07-01T00:06:58.5902105Z ## Validating Inputs
2020-07-01T00:06:58.5915067Z ## Validating Inputs Complete
2020-07-01T00:06:58.5924850Z ## Initializing Az module
2020-07-01T00:06:59.0747435Z ##[command]Import-Module -Name C:\Program 
Files\WindowsPowerShell\Modules\Az.Accounts.9.0\Az.Accounts.psd1 -Global
2020-07-01T00:07:00.0802372Z ##[command]Clear-AzContext -Scope Process
2020-07-01T00:07:01.5597330Z ##[command]Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue
2020-07-01T00:07:01.9691282Z ##[command]Connect-AzAccount -Identity @processScope
2020-07-01T00:07:03.1860248Z ##[command] Set-AzContext -SubscriptionId 5ec8ec06-XXXX-XXXX-XXXX- c0ff86c50e4 -TenantId ***
2020-07-01T00:07:03.9196710Z ## Az module initialization Complete
2020-07-01T00:07:03.9203692Z ## Beginning Script Execution
2020-07-01T00:07:03.9674782Z ##[command]& 'C:\DevOps\_work\_tempb1b130b-4306-448b-b4b2-e7daefc2382e.ps1' 
2020-07-01T00:07:03.9974844Z This is host
2020-07-01T00:07:04.0101140Z This is output
2020-07-01T00:07:04.0517610Z ##[command]Disconnect-AzAccount -Scope Process -ErrorAction Stop
2020-07-01T00:07:04.4795714Z ##[command]Clear-AzContext -Scope Process -ErrorAction Stop
2020-07-01T00:07:04.9468120Z ## Script Execution Complete
2020-07-01T00:07:04.9857991Z ##[section]Finishing: Azure PowerShell script: InlineScript

请注意,“This is Host”和“This is Output”都显示,但“##vso[....”不显示。

我在后续步骤中尝试读取的 MobileAppInsightsKey 也是空的(未初始化)。

希望有人能指出我正确的方向。

谢谢,马克。

更多的乱七八糟的事情,我成功了。答案与我在文档和 SO 上阅读的所有内容背道而驰。

如果我使用

isOutput=true;

然后就可以了。

我不知道为什么,但很高兴接受教育。 谢谢,马克。

并对这个问题做一个明确的描述:

要在您的方案中定义作业范围的变量,我们不需要添加 isOutput=true;

1.For job-scoped变量(变量只在当前job有效):

Write-Host '##vso[task.setvariable variable=MobileAppInsightsKey]thisisthekey' 就够了。我们可以在CMD任务中通过格式$(MobileAppInsightsKey)输出它的值。

2.For多作业输出变量(变量在多作业有效):

我们应该使用Write-Host '##vso[task.setvariable variable=MobileAppInsightsKey;isOutput=true;]thisisthekey'.

在当前工作中:您可以使用$(referencename.variablename)来获取它的值。 (支持经典管道和yaml管道)

在后续工作中:使用以下格式访问变量,此格式仅支持yaml管道!!!

- job: B
  dependsOn: A
  pool:
    vmImage: 'ubuntu-16.04'
  variables:
    myVarFromJobA: $[ dependencies.A.outputs['setvarStep.myOutputVar'] ]  # map in the variable
                                                                          # remember, expressions require single quotes
  steps:
  - script: echo $(myVarFromJobA)
    name: echovar

因此,对于您想要在同一作业中访问变量的场景,只需删除 isOutput=true;(这不是必需的)。或者如果在语句中添加 isOutput=true;,则使用 $(referencename.variablename) 格式。 (没必要,不推荐,但对现在的工作应该也有用)

另外:

关于 $(referencename.variablename) 格式的详细信息。

对于经典管道:(在 Powershell 任务中将名称设置为测试)

$(Test.MobileAppInsightsKey)表示变量的值。

对于 yaml 管道:

  - powershell: echo "##vso[task.setvariable variable=myOutputVar;isOutput=true]this is the value"
    name: Test
  - script: echo $(Test.myOutputVar)