Powershell how to pass System variables - powershell

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!#'

Related

How does dotenv cli with "--" (double-dash) running commands?

Im my project i am trying to use dotenv-cli with pnpm. I am using PowerShell 7.2.1 on windows. I have monorepo with package api with script dev in package.json.
First what I tried was:
dotenv -e .\.env -- pnpm dev --filter api
And it did not work:
 ERR_PNPM_RECURSIVE_EXEC_FIRST_FAIL  not found: dev
But when I tried:
dotenv -e .\.env -- pnpm -- dev --filter api
It worked well.
As I had read -- signifies the end of command options, after which only positional arguments are accepted. So why do I need to use it twice for my command to work? Why is it working like that?
The problem is that when you call from PowerShell (unlike from cmd.exe), the command name dotenv resolves to a PowerShell script, namely dotenv.ps1, as you report.
When PowerShell calls a PowerShell-native command - including .ps1 files - its own parameter binder interprets the (first) -- argument and removes it; that is, the target command never sees it.
(The semantics of -- is analogous to that of Unix utilities: -- tells the parameter binder to treat subsequent arguments as positional ones, even if they look like parameter (option) names, such as -foo.)
Thus, unfortunately, you need to specify -- twice in order to pass a single -- instance through to the .ps1 script itself:
# The first '--' is removed by PowerShell's parameter binder.
# The second one is then received as an actual, positional argument by
# dotenv.ps1
dotenv -e .\.env -- -- pnpm dev --filter api
Alternatively, assuming that dotenv.cmd, i.e. a batch file version of the CLI exists too (and is also in a directory listed in $env:PATH), you can bypass this problem by calling it explicitly, instead of the .ps1; when calling external programs (including scripts interpreted by other shells / scripting engines, such as cmd.exe), PowerShell does not remove --:
# Calling the batch-file form of the command doesn't require
# passing '--' twice.
dotenv.cmd -e .\.env -- pnpm dev --filter api
Caveat: While it will typically not matter, the way a batch file parses its arguments differs from how PowerShell does it for its native commands.

PowerShell script in Azure DevOps removes quotes

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:

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

Passing property values with spaces to Ant

I'm trying to pass a space-separated value $env:tt to Ant under PowerShell
$env:tt="val1 val2"
Here are the commands I've tried:
ant '-DTest="$env:tt"'
ant -DTest=$env:tt
With the above commands, Ant doesn't interpret $env:tt. The value of test becomes $env:tt.
ant -DTest="$env:tt"
I got the following response under PowerShell
PS C:\> ant -DTest="$env:tt"
>>
It seems that this command is not finished, and PowerShell expects me to enter some characters to terminate the command.
Any idea on how to do this?
variables are expanded inside double quotes but not when inside single quotes.
ant "-dest=$env:tt"

ant command from powershell - dot used as delimiter for parameter?

I'm trying to run an ant task which I'm invoking from a powershell script:
Invoke-Expression "ant -f $antFilePath -Dtestsuite=$testsuite clean compile run"
The $testsuite variable is a string which includes a dot character, e.g. "systemTest.All", so:
Invoke-Expression "ant -f $antFilePath -Dtestsuite=systemTest.All clean compile run"
My problem is that the dot seems to be interpreted as a delimiter (by powershell? Invoking from cmd works just fine), hence the "All" part gets treated as a ant target (among with clean compile run).
(The use of a dot in the testsuite name is not one of mine doings so that part I can not affect)
Do I need to qoute the ant argument, escape the dot in some way?
Br,
Pete
Try this (but I can't test it), run this directly w/o invoke-Expression:
ant -f $antFilePath "-Dtestsuite=$testsuite" clean compile run