Use VSTS Variables Inline powershell in build and release pipelines

Tips and tricks Inline Powershell task VSTS, use VSTS/TFS variables in your task

vsts-inlinepowershell-inlinepowershellMaximize how you use your VSTS build and release pipeline with Inline Powershell tasks. In this blog series ‘Tips and Tricks for Inline Powershell’, I will show simple samples on how to get more out of your pipelines. This blog post: Use VSTS Variables.

VSTS Inline powershell task
The Inline PowerShell VSTS task enables you to execute PowerShell from a textbox within your build or release pipeline. You can run a PowerShell script on you agent or as Azure Powershell.
Introduction Inline Powershell Task
Install Inline Powershell Task

Use VSTS Variables Inline Powershell
You can use variables to steer you VSTS Pipeline. These variable can be used, changed or created in your VSTS Inline Powershell task. You have access to your own variables and the default variables VSTS variable.

Reading/Using a variable
When you want to use a variable in your Inline Powershell script, you can add them in the arguments parameter field. For example the default variable Build.BuildNumber can be added by putting -buildnumber ($Build.BuildNumber) in the argument parameters. Then add it as Parameter to your script and you will be able to access the variable.
EnterPowerShellArguments

Param(
    [string]$buildNumber
)
Write-Output "The buildnumber is:$buildNumber"

Create or save a changed variable
You can also set or modify a variable for usage by an other task. The command you can use is task.setvariable:

Write-Host "##vso[task.setvariable variable=variableName]variable value"
Write-Host "##vso[task.setvariable variable=mySecret;issecret=true]tomatos are purple"

Add the ‘issecret=true’ to handle any secret values. When they are printed, they will be shown as *******.

More tips and tricks
Use VSTS variables
Let your task fail
Set progress
Change buildnumber
VSTS Command overview
Call a WebHook
Download a file
Install a Powershell Module
Navigate VSTS as filesystem
Make VSTS API Rest calls
Script example: Act on failed build

Extra resources
See default list VSTS variables: Overview of default VSTS variables

Advertisements

10 thoughts on “Use VSTS Variables Inline powershell in build and release pipelines”

  1. How can I pass the multiple parameter in variable group. eg. Vnetname = “abc”, “34dfs”, “ju32d”. The Vnet values are run in powershell in line script (foreach statement)

    Like

      1. is it possible to use Multipliers (Multi-configuration) in c# code?
        Multipliers: Browsers
        Browsers=chrome_74,chrome_75,firefox_54
        Environment.GetEnvironmentVariable(“Browsers”); —–doesnt work

        thanx, Alex

        Like

      2. I’m not where you browsers come from, but looking at your code I would suggest to put them into a string:
        -browsers “chrome_74,chrome_75,firefox_54”

        and then use them in the script like:
        param($browsers)
        write-host “browsers is $browsers”
        Now you can do a split on the string in powershell

        Like

  2. Hi,

    Using ##vso[task.setvariable variable=variableName]variable value” is a nice feature for testing the pipeline if the amount of variables is limited. I used it in a real scenario and quickly realized how half-baked a solution it is.
    Not to mention that the PowerShell task that is mentioned here has two flaws:
    1) Task version 1 accepts arguments for inline script execution, but there is still a 500 character limit for the script.
    2) Task version 2 has no arguments section at all for inline script execution and there is a 5000 character limit anyways.
    In other words, Microsoft fails again. Nonetheless, the article itself is accurate, so thanks for posting.

    Regards,
    Mateusz Szadziul

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.