ZoolWay
ZoolWay

Reputation: 5505

Using variable in Powershell task, inline script

I am trying to write a very easy inline script which should update the variable NameSuffix to a new value. I wanted to use a local helper variable newVar.

Write-Host "Detected version: $(AssemblyInfo.AssemblyVersion)";
$newVar = "v$(AssemblyInfo.AssemblyVersion.Major).$(AssemblyInfo.AssemblyVersion.Minor).$(AssemblyInfo.AssemblyVersion.Build).$(AssemblyInfo.AssemblyVersion.Release)";
Write-Output ("##vso[task.setvariable variable=NameSuffix;]$(newVar)");

But Visual Studio Online / VSTS tells me when the scrips runs:

The term 'newVar' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Using a variable this way seems common in script files and works on the command line.

So how can I accomplish this simple assignment of a variable (with a concated value) as a build task in VSO/VSTS?

Upvotes: 1

Views: 5290

Answers (1)

Mark Wragg
Mark Wragg

Reputation: 23415

Per the comments, the issue is because newvar should be $newvar to correctly reference the variable. The $() part is the subexpression operator, which is usually used within a String when you have a non-simple single variable that you want expanded to a string (such as accessing the property of a variable).

In this case, your variable is simple, so you can do away with the sub-expression operator I believe. Equally I'm not sure the outer-brackets or semi-colons are needed, simplifying your code to this:

Write-Host "Detected version: $(AssemblyInfo.AssemblyVersion)"
$newVar = "v$(AssemblyInfo.AssemblyVersion.Major).$(AssemblyInfo.AssemblyVersion.Minor).$(AssemblyInfo.AssemblyVersion.Build).$(AssemblyInfo.AssemblyVersion.Release)"
Write-Output "##vso[task.setvariable variable=NameSuffix;]$newVar"

Upvotes: 2

Related Questions