【问题标题】:How to use output variable value from one powershell script to another powershell script如何将一个powershell脚本的输出变量值用于另一个powershell脚本
【发布时间】:2020-12-14 02:50:04
【问题描述】:

大家好,我正在开发 ymal 发布管道,我有两个 PowerShell 脚本。 测试1.ps1 test2.ps1

步骤 1) 在 test1.ps1 中,我使用 :

设置输出变量

$link="google.com"

Write-Host "##vso[task.setvariable variable=packageurl]$link"

步骤 2) 现在我想在 test2.ps1 脚本中使用这个 pacakgeurl 变量值。

Ymal 代码如下所示:

- task: AzurePowerShell@3

      displayName: 'Query Latest Package'

      inputs:
        azureSubscription: 'test1'
        ScriptType: FilePath
        ScriptPath: 'source\test1.ps1'

- task: PowerShell@2

      displayName: 'Download package' 
      inputs:
        targetType: filePath 
        filePath: 'source\test2.ps1'

所以基本上我必须通过 PowerShell 脚本使用从 1 个任务到 2 个任务的输出变量值。

我也尝试点击此链接:https://developercommunity.visualstudio.com/content/problem/676342/how-to-use-output-variable-from-a-powershell-scrip.html

谁能帮我解决这个问题..?

提前致谢。

【问题讨论】:

  • 你是如何引用 test2.ps1 中的变量的?

标签: powershell azure-devops yaml azure-pipelines


【解决方案1】:

这如你所料:


trigger: none
pr: none

pool:
  vmImage: 'windows-latest'


steps:
- task: AzurePowerShell@3
  displayName: 'Query Latest Package'

  inputs:
    azureSubscription: 'rg-the-code-manual'
    ScriptType: FilePath
    ScriptPath: 'stackoverflow\94-variables\script-1.ps1'
    azurePowerShellVersion: LatestVersion

- task: PowerShell@2
  displayName: 'Download package' 
  inputs:
    targetType: filePath 
    filePath: 'stackoverflow\94-variables\script-2.ps1'

Yaml 文件就是你所拥有的。

script-1.ps1 设置变量:

$link="google.com"

Write-Host "##vso[task.setvariable variable=packageurl]$link"

script-2.ps1 使用该变量:

Write-Host $env:PACKAGEURL

【讨论】:

    猜你喜欢
    • 2015-07-07
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 2020-07-23
    • 2022-11-23
    • 1970-01-01
    相关资源
    最近更新 更多