WinBoss
WinBoss

Reputation: 923

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: 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

Upvotes: 1

Views: 5004

Answers (2)

ccoutinho
ccoutinho

Reputation: 4556

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

Upvotes: 1

4c74356b41
4c74356b41

Reputation: 72171

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.

Upvotes: 2

Related Questions