Reputation: 65
I am using an On-Prem Server to run VSTS Build/Releases.
Currently, I am trying to pass a variable from VSTS: $(password)
to my script.
Suppose the value of this variable $(password)
is 'stringwith$sign'`
This variable $(password)
needs to be injected into a string in my script:
$string = "I need to inject my password string from VSTS here "$(password)""
The String should look like this:
$string = I need to inject my password string from VSTS here "stringwith$sign"
How do I achieve this? The build/release will fail if I simply add it as $(password) since it thinks $sign
in "stringwith$sign"
is a variable. I cannot even use ''
quotes since my variable $(password)
needs to be inserted in $string
.
Upvotes: 4
Views: 5590
Reputation: 38106
You just need to use the format ${env:password}
instead of $(password)
to get the variable password's value.
Such as if you add a PowerShell task with below script:
$string="I need to inject my password string from VSTS here ${env:test}"
echo $string
Then it will show I need to inject my password string from VSTS here stringwith$sign
in the build log.
Upvotes: 1
Reputation: 72171
You can also use format operator:
$myVar = 'okay$dollar'
'My string with my var {0} inside' -f $myVar
or Get-Variable
:
$myVar = 'okay$dollar'
"My string with my var $(Get-Variable myvar | select -expandproperty value) inside"
Upvotes: 0
Reputation: 7282
Without seeing any sample code, it's a bit hard to tell how your script works.
But basically, if you are setting a string literal that contains special characters, you can stop them from being parsed by using single quotes instead of double-quotes. For example, if you execute
$password = "stringwith$sign"
$password
Then the value of password is stringwith
.
This is because powershell has parsed the string and treated $sign as being the name of a variable and has attempted to insert the value of $sign. But as $sign hasn't been declared, the default value of empty string is used.
However, if you used single quotes, i.e.
$password = 'stringwith$sign'
$password
Then the value of password is stringwith$sign
.
Subsequently, setting
$string = "I need to inject my password string from VSTS here ""$password"""
gives $string the value of
I need to inject my password string from VSTS here "stringwith$sign"
Upvotes: 4