0

I have a requirement where my string is of format as below:

<?define BuildNumber = "8314" ?>

I am using below powershell script in TFS 2017 build template to replace the build number value:

$content = Get-Content -path "$(Build.SourcesDirectory)\Install\Common\Constants.wxi"
$num  = $(Build.BuildId)
$content -Replace '(BuildNumber\s*=\s*")\d*("\s*)', "`$1 $num `$2" |  Out-File $(Build.SourcesDirectory)\Install\Common\Constants.wxi

This gives output like <?define BuildNumber = " 27994 " ?> which is incorrect as I do not want spaces in the value. When I tried using below code, it does not work.

$content -Replace '(BuildNumber\s*=\s*")\d*("\s*)', "`$1$num`$2" |  Out-File  $(Build.SourcesDirectory)\Install\Common\Constants.wxi

Output : <?define $27994 ?>

I tried all combinations but cannot get the quotations to work correctly. Please suggest a solution.

3

1 Answer 1

2

Use curly braces to "escape" group number

$content -Replace '(BuildNumber\s*=\s*")\d*("\s*)', "`${1}$num`$2" | Out-File $(Build.SourcesDirectory)\Install\Common\Constants.wxi

A little clarification about why the original code doesn't work: after $num variable has been resolved the replace string became $127994$2. That means that the -replace operator is trying to find group $127994 which obviously does not exist. When we add curly braces it becomes ${1}27994$2 which is completely legit.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the quick reply. Worked like magic. I am a little new to powershell scripting.
@Mustafa I've added a clarification about why your code doesn't worked as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.