PowerShell script in Azure DevOps removes quotes - azure-devops

I have a PowerShell script that triggers a command, in this case it's a npx command. One of the arguments for the command contains spaces, locally it works fine but on Azure DevOps it seems like it drops the quotes. This because the script fails complaining about the value of the argument, which is everything until the first occurrence of a space.
The PowerShell script looks a bit simplified like this:
npx testcafe "$env:TESTCAFE_BROWSER_NAME" tests/**/*
The value of the environment variable could be something like chrome#87.0:OS X Catalina
The error in Azure Devops would the be something like:
ERROR Unable to find the browser. "chrome#87.0:OS" is not a browser alias or path to an executable file.
When running the script on my local machine with the same value for the environment variable it succeeds without any errors.

You can try below workarounds to keep the quotes in the script.
1, you can use back tick "`" to escape the quotes. See below:
npx testcafe "`"$env:TESTCAFE_BROWSER_NAME`"" tests/**/*
2, Yon can aslo define the value with the quotes in environment Variables. Define the environment variable in the Variables tab like below:

Related

Azure Devops environment variable not resolving in task

In an Azure Devops Build pipeline, I'm setting an environment variable in Powershell, like below:
And i know the variable works in another powershell task, because of this test:
But I'm not able to actually use this in another, non-powershell task. See this docker build/push task where i'm trying to add a tag. I get an error saying that it can't find that variable. Is my notation wrong somehow? I've tried multiple different variations on the screen shot below, but none have worked.
The notation $env:LOCALTAG is powershell specific. I think you should use the macro syntax $(LOCALTAG) (without $env)
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#macro-syntax-variables.

Powershell how to pass System variables

I am trying to execute the below command on powershell, but the encryption password is not recognised.
This password is used in integration tests.
gradle publish -Djasypt.encrypt.password = $xyz!#
The below command also does not work
cmd /c gradle publish -Djasypt.encrypt.password = $xyz!#
The same command works well on CMD
Any suggestions on passing the arguments (with -D)?
$ is the sigil denoting a variable in PowerShell just like most other shells, so $xyz means the variable named xyz. You need to escape that symbol with a backtick
gradle publish -Djasypt.encrypt.password = `$xyz!#
Alternatively just quote the string with a single quote to prevent variable substitution
gradle publish -Djasypt.encrypt.password = '$xyz!#'

Jenkins Inject Environment variable works with batch command but not powershell

I'm dealing with a very weird anomaly in Jenkins that makes absolutely no sense to me. Essentially Jenkins is behaving differently for a powershell command than for a batch command.
My goal is to pass an environment variable (or parameter) from one Jenkins Job to another. However this variable to be passed is generated during the runtime of the first job.
I made a fake project to test passing variables and I was able to do so by adding a build step to echo out the variable into an env.props file on the node and then used the parameterized trigger plugin to call the next job. I was able to get this to work great in this test scenario but when I tried to implement the same steps in the actual build job (which relies on powershell scripts) it did not work.
After, a lot of trial and error I have found that when I use a windows batch command to echo the variable into a props file and then inject the variable into the job - it works perfect. But when I do the exact same thing with a powershell command it does not inject the variable back in to the job even though I use the exact same line of code. It still writes the variable to the file but Jenkins will not "reinject" this variable back into the job's env variable even though I am using the exact same step to do so.
The command is essentially this:
echo Testvar=Somevalue > C:\Jenkins\env.props
Both sucesffuly write the string to the props file, but when done with a powershell command, Jenkins will not absorb the txt from the run. Almost as if powershell is encoding it in a way that Jenkins cannot read but looks the exact same to me.
Any ideas?
Turns out, it was the encoding!
echo "string" > file.txt
does not produce the same result in batch as powershell.
Switching to
echo "string" | out-file -encoding ASCII file.txt
did the trick.

Invoking a command with variable evaluation in Octopus Deploy Powershell script

I have a simple Powershell script that I execute during an Octopus Deploy installation. This line works fine:
& $exe install --autostart
I runs an application identified by $exe variable with command line arguments "install --autostart".
Now I need to expand command line arguments with a value evaluated from a variable:
& $exe install --autostart -servicename=$serviceName
"$serviceName" is the variable that gets its value during the script execution. Whatever I do it's passed to the line above by variable name, not the value, e.g. it's passed as "$serviceName". I tried single and double quotes, nothing helps. As long it's a command invocation (triggered by the "&" symbol in the beginnging of the line), the rest of the line is interpreted verbatim, no variable substitions.
I used last couple of hours trying to figure this out and this is driving me mad. Any tips are appreciated.
I just did some testing on my side and it looks like if you'd like the variable passed in to the command to be evaluated as a variable it needs whitespace on both sides. So you would want to define your variable as $serviceName = "-servicename=*name*" or if that is not possible then create a new variable just before running the command
$tmpServicename = "-servicename=$($serviceName)"
& $exe install --autostart $tmpServiceName

Thoughtworks go: pass environment variable to a task

How can I pass the GO environment variable to a task. ie
grunt build-discovery-dev --buildNumber=" ${GO_PIPELINE_COUNTER}.
I want the GO_Pipeline_counter to be replaced with the actual value ie 56.
Depends on where you invoke the command from
Shell
grunt build-discovery-dev --buildNumber=$GO_PIPELINE_COUNTER
Powershell
grunt build-discovery-dev --buildNumber=$env:GO_PIPELINE_COUNTER
Cmd
grunt build-discovery-dev --buildNumber=%GO_PIPELINE_COUNTER%
You should wrap environment variables with '%'. E.g.:
grunt build-discovery-dev --buildNumber=" %GO_PIPELINE_COUNTER%.
The braces characters (as in ${VAR}) do not work in GoCD tasks. You really must only use the dollar sign without braces (as in $VAR) for correct replacement of a variable with its value.