How do I escape a semicolon (;) character for an Azure DevOps SSH task? - azure-devops

I am trying to run a command like:
java -jar pathToJar/jarFile.jar "connectionstring=jdbc:impala:server:port;param1=value1"
I am running it via an Azure DevOps SSH task. The problem with this is that the semicolon character (;) is a special character (command separator). I've tried escaping with \ but that doesn't help.
With no escaping I see the following command has been run:
java -jar pathToJar/jarFile.jar "connectionstring=jdbc:impala:server:port
The ;param1=value1 suffix is missing.

Taking a look at the code on GitHub, I'm assuming that you have 'Commands' selected as the run option. By default it splits the command on ; or line break. In this case you need to switch the option to 'inline' or put it in a script file and run it that way.
Use 'inline' if you are going to have a single command that contains a semi-colon

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

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:

How to split a CLI command in Azure Devops over multiple lines? (Running on Windows)

I am running the following in a CLI task on Azure Devops (inline)
rem Create the Service Plan
call az appservice plan create --resource-group %RESOURCE_GROUP% --name %SERVICE_NAME% --sku B1
Whch works just fine. However, I'm having to scroll to see the whole thing and I have other commands which are even longer. So I'm trying to split it over multiple lines so I can see more clearly what is going on.
Looking at Microsoft Docs it looked like the solution was to put a backslash on the end of each line. So I tried:
I've tried:
rem Create the Service Plan
call az appservice plan create \
--resource-group %RESOURCE_GROUP% \
--name %APP_SERVICE_NAME% \
--sku B1
Which didn't work. I then read something that recommended the back-tick/back-quote at the end of each line:
rem Create the Service Plan
call az appservice plan create `
--resource-group %RESOURCE_GROUP% `
--name %APP_SERVICE_NAME% `
--sku B1
This also didn't work. Any help greatly appreciated.
Never mind. Worked it out. Turns out you need to use '^'
rem Create the Service Plan
call az appservice plan create ^
--resource-group %RESOURCE_GROUP% ^
--name %APP_SERVICE_NAME% ^
--sku B1
For me the ` character is working. I posted earlier that it wasn't but I was inadvertently using the wrong character.
(That post seems to have been deleted by a moderator though I'm not sure why - perhaps because it was more of a "me too" than an answer, but it did add that the above "^" solution doesn't work for me).
In case it was the same issue as I had, ensure the "Backtick" you use is the correct one (it's immediately to the left of "1" on my microsoft keyboard). I was trying to use a ' in the failed attempt as the CLI docs were reading made it hard to distinguish.
The character that you need to use to specify that the command is split over multiple lines, depends on the environment in where the CLI command is run.
In other words, it depends on the scriptType property of the AzureCLI#2 command.
Powershell/Powershell Core: use a backtick `
bash: use backslash \
batch: use accent circonflexe ^
from azure cloud shell, type AZ, then copy paste the az command with \ for multiple line will not work.
but there is a fix, you click on the + sign like a adding a folder sign, it will bring a full page Azure Cloud Shell window, change to Bash from the pull down (default is Poweshell), then you see prompt changed to name#Azure:~$, now you can use az commend with \ for multiple lines.

Launch external program with multiple commands via Powershell

I am currently attempting to launch a different console (.exe) and pass multiple commands; while starting and entering a command works just fine, I haven't been able to find out how multiple ones can be entered via powershell.
& "C:\Program Files\Docker Toolbox\start.sh" docker-compose up -d --build
The given command works fine, but as mentioned I need to pass more than one command - I tried using arrays, ScriptBlocks and different sequences, though to no avail.
Edit:
Noticed that the docker build has a -f tag which allows me to specify a file; however, the issue now seems to be that the executed cmd removes all backslashes & special characters, rendering the path given useless.
Example:
&"C:\Program Files\Docker Toolbox\start.sh" 'docker-compose build -f
path\to\dockerfile'
will result in an error stating that "pathtodockerfile" is an invalid path.
Your start.sh needs to be able to handle multiple arguments. This doesn't look like a PowerShell question
Turns out that it was easier than expected; Solved it by executing a seperate file that contained the two commands needed and passing it to the start.sh file.
&"C:\Program Files\Docker Toolbox\start.sh" './xyz/fileContainingCommands.sh'