首页 > 解决方案 > 如何将一个powershell脚本的输出变量值用于另一个powershell脚本

问题描述

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

步骤 1)在 test1.ps1 中,我使用以下方法设置输出变量:

$link="google.com"

写主机“##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

谁可以帮我这个事 ..?

提前致谢。

标签: powershellazure-devopsyamlazure-pipelines

解决方案


这可以按您的预期工作:


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

推荐阅读