简体   繁体   中英

VSTS passing variables into Azure PowerShell Script Arguments

I have multiple Azure PowerShell tasks in which I'd like to pass variables. Currently I'm passing variables with Script Arguments, like on the screenshot: 在此处输入图片说明

The problem with Script Arguments is that I'd have to edit them in every process if there is a change. Is it possible to have just a single variable that I can link to multiple processes? I've looked at VSTS: Pass build variables into Powershell script task but no matter how I edit my scripts they refuse to work. That topic also doesn't explain if variables should be put into process variables or Queue Build Variables. Here is the script that I'm trying to automate:

    param
(
    [Parameter(Mandatory)]
    [String]$NewFileLocation1
)

Set-AzureRmVMCustomScriptExtension -Argument "-NewFileLocation1 $NewFileLocation" -ResourceGroupName Test-RG1`
    -VMName VM1 `
    -Location northeurope `
    -FileUri https://raw.githubusercontent.com/x/assets/master/PowerShell/PS1/PS1/PS1.ps1 `
    -Run 'PS1.ps1' `
    -Name PS1

PS1.ps:

    param
(
    [Parameter(Mandatory)]
    [String]$NewFileLocation

)
New-Item $NewFileLocation

Here's what I'm using:

Param (
    [string]$location = $env:location,
    ...
    [string]$tagEnvironment = $env:tagEnvironment
)

I'm using VSTS Library Variable groups, you can link variable groups to different release pipelines.

Just to develop a little bit more on how to use the aforementioned variable groups, on Azure DevOps you can declare them in the Pipelines Menu, on the Library sub-menu. In the image below, I am declaring a variable group named Tailspin Space Game - Release

Azure DevOps Pipelines 上的变量组


I can then reference the variable groups in the YAML code as follows:

variables:
- group: 'Timeline CV - Release'

And then I would have to pass them to the powershell as arguments, as follows:

- task: PowerShell@2
        displayName: 'Run PowerShell Task'
        inputs:
          filePath: 'path\to\powershell\script\script.ps1'
          arguments: '$(groupVar1) $(groupVar2) $(groupVar3)'

Build variables would be available to the PowerShell script without explicitly passing them as arguments this way. However, I was not able to achieve the same with any variable within the variable group.


I can use the variable groups now in a powershell script, that is being called in the powershell task, the following way:

$GroupVar1OnPowerShellScript = $env:groupVar1
$GroupVar2OnPowerShellScript = $env:groupVar2
$GroupVar3OnPowerShellScript = $env:groupVar3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM