1

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: enter image description here

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
WinBoss
  • 879
  • 1
  • 17
  • 40
  • As 4c74356b41 said [Variable groups](https://learn.microsoft.com/en-us/vsts/build-release/concepts/library/variable-groups) is the better way. – starian chen-MSFT Sep 20 '17 at 03:21
  • What do you think of Variable group? Does it solve your issue? – starian chen-MSFT Sep 25 '17 at 05:20
  • I actually found queue variable (don't know proper name for it, the ones on the screenshot) https://i.stack.imgur.com/eXoTi.png better fit for my use case. But if I wouldn't try Variable Groups, I wouldn't know that. Thank you, very much for your input, guys! – WinBoss Sep 25 '17 at 08:39

2 Answers2

2

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.

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
1

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

Variable groups on 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
ccoutinho
  • 3,308
  • 5
  • 39
  • 47