Maximize 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.
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
Thanks for your awesome extension and most of all these tips and tricks posts.
Just want to let you know there is a typo in the example script. You’re using {} with the Param, but those need to be ().
LikeLiked by 1 person
Thx for your feedback, you are correct. I updated the code sample.
LikeLike
Please fix the command line arguments as well.
This doesn’t work:
-buildnumber ($Build.BuildNumber)
But this does:
-buildnumber $(Build.BuildNumber)
LikeLike
This is by design, VSTS does pass the variables that way with the $ before the ():
For example, the release name format Release $(Rev:rrr) for build $(Build.BuildNumber) $(Build.DefinitionName) will create releases with names such as Release 002 for build 20170213.2 MySampleAppBuild.
https://docs.microsoft.com/en-us/azure/devops/pipelines/release/?view=vsts
LikeLike
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)
LikeLike
You would be able to pass them in with the field Argument:
-arg1 $(var1) -arg2 $(var2)
LikeLike
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
LikeLike
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
LikeLike
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
LikeLike
Have you tried this extension to run inline scripts:
https://marketplace.visualstudio.com/items?itemName=petergroenewegen.PeterGroenewegen-Xpirit-Vsts-Build-InlinePowershell
It does not have the 500 char limit
LikeLike