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
Related
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!#'
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:
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"
I have a PowerShell (2.0) script that runs an executable beneath my program files directory, and I'm using an environmental variable to reference the path:
Start-Process "$($env:ProgramFiles)\ProgramFolder\Executable.exe"
This worked fine on my x86 machine, but now I'm trying to run it on an x64 machine. Since the executable is 32-bit, it resides beneath "C:\Program Files (x86)", and therefore I've adjusted my script as follows since the environmental variable I need to use is ProgramFiles(x86):
Start-Process "$($env:ProgramFiles(x86))\ProgramFolder\Executable.exe"
I'm getting this error though:
Unexpected token '(' in expression or statement.
So how do I reference that environmental variable given that it contains parentheses?
You don't need the subexpression $() in this case:
"${env:ProgramFiles(x86)}\ProgramFolder\Executable.exe"
Outputs:
C:\Program Files (x86)\ProgramFolder\Executable.exe
If you still want to use a subexpression, you can specify a variable name that contains PowerShell syntax characters like so:
"$(${env:ProgramFiles(x86)})\ProgramFolder\Executable.exe"
I can't seem to call this executable correctly in my psake deploy script.
If I do this:
exec { "$ArchiverOutputDir\NServiceBus.Host.exe /install" }
It simply outputs this (and is clearly not calling the executable - just outputting the value of that expression):
c:\ReloDotNet2_ServiceEndpoints\Archiver\NServiceBus.Host.exe /install
But if I do this:
exec { c:\ReloDotNet2_ServiceEndpoints\Archiver\NServiceBus.Host.exe /install }
I get the expected output from the executable.
How do I correctly call an executable with a variable in the path to the executable in psake? If this is actually a PowerShell issue, please feel free to correct the question to reflect that insight.
I
Classic PowerShell issue. Try this instead:
exec { & "$ArchiverOutputDir\NServiceBus.Host.exe" /install }
PowerShell not only executes commands, it also evaluates expressions e.g.:
C:\PS> 2 + 2
4
C:\PS> "hello world"
hello world
What you have given to PowerShell at the beginning of a pipeline is a string expression which it faithfully evaluates and prints to the console. By using the call operator &, you're telling PowerShell that the following thing is either the name of a command (in a string) to be executed or a scriptblock to be executed. Technically you could also use . "some-command-name-or-path". The only difference is that for PowerShell commands, & creates a new scope to execute the command in and . doesn't. For external exes it makes no difference as far as I can tell which one you use but & is typically used.