【问题标题】:Cannot set powershell output variables in Devops step无法在 Devops 步骤中设置 powershell 输出变量
【发布时间】:2020-07-01 00:21:17
【问题描述】:

我正在尝试使用 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\1.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\_temp\1b1b130b-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 也是空的(未初始化)。

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

谢谢,马克。

【问题讨论】:

    标签: azure azure-devops azure-powershell


    【解决方案1】:

    并明确说明这个问题:

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

    1.对于工作范围的变量(变量只在当前工作中有效):

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

    2.对于多任务输出变量(变量在多任务中有效):

    我们应该使用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)
    

    【讨论】:

    • 谢谢 - 这是一个比我的回答更详细的“为什么”。我将在本地针对当前工作对其进行测试,然后将其标记为答案。如果您对文档作者有任何影响,也许他们可以增加这种清晰度。现在我知道原因是有道理的,但是从文档中看,添加 isOutput=true 时必须如何引用的区别并不明显;干杯。
    • 同意,如果文档添加Note:Only format $(referenceName.variableName) is supported in current job when using is output=true 会更好。您可以通过This Page 选项分享您的反馈here,如果作者看到它会回复。希望它有所帮助:)
    【解决方案2】:

    更多的混乱,我让它工作。答案与我在文档和 SO 中阅读的所有内容都背道而驰。

    如果我不使用

    isOutput=true;

    然后就可以了。

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

    【讨论】:

      猜你喜欢
      • 2021-06-28
      • 2022-10-26
      • 1970-01-01
      • 2022-10-17
      • 2016-10-29
      • 2018-09-30
      • 2021-08-21
      • 2018-04-04
      • 1970-01-01
      相关资源
      最近更新 更多