Azure adds leading space to variable

Question:

If I run this powershell code locally:

The output is:

http://merge.azurewebsites.net

When I run this code on a Azure Powershell:

And then create the URL in another Azure Powershell script:

The ouput is

http:// merge.azurewebsites.net

What’s causing this leading space character in the $env:prSourceBranchName?

Answer:

Azure isn’t adding anything – your write-host is!

Your code is doing this:

but presumably you want

Note where the second quote is the write-host in both examples. In the first it’s before the $x variable name. In the second it’s after.

In your question it’s calling this (with the quote before the variable name):

which will write a logging command to the log file, and Azure DevOps will process that and update the environment variable.

You’re probably expecting it to write this to the log file:

but it’s actually writing this:

Try switching your code to this (i.e. second quote after the variable name):

and it should omit the space in front of the branch name in your url.

Source:

Azure adds leading space to variable by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply