Passing property values with spaces to Ant - powershell

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"

Related

Can i run a exe with cmd or powershell if i have spaces in username?

Ex:
cmd /C start C:\Users\Bob Builder\Desktop\New Folder\test.exe
I'm trying to use cmd to start a file but since there are spaces in the path, cmd is throwing an error after Bob.
Error:
"Windows cannot find C:\Users\Bob. Make sure you typed the name
correctly, then try again."
The system cannot find the file C:\Users\Bob.
Its simply failing to accept the spaces. It's driving me crazy because I'm spoiled with C# working out of the box. I don't know much about this, I have been spending way too much time trying to figure this out. Some help would be greatly appreciated.
In order for a path that contains spaces to be recognized as a single path (argument), it must be quoted.
In order for an executable to execute in the current console window, synchronously, with its streams connected to the calling shell, it must be invoked directly, not via start.
Direct invocation from cmd.exe (only "..." quoting supported):
"C:\Users\Bob Builder\Desktop\New Folder\test.exe"
From PowerShell:
& 'C:\Users\Bob Builder\Desktop\New Folder\test.exe'
Note:
PowerShell also supports '...' strings (single-quoted), which are verbatim strings that are preferable to "..." (double-quoted) ones if you do not require expansion of variables (string interpolation) - see the conceptual about_Quoting_Rules help topic.
For syntactic reasons, PowerShell requires the use of &, the call operator to invoke commands that are quoted and/or contain variable references - see this answer for details.
By contrast, use start in cmd.exe / Start-Process in PowerShell (whose built-in alias is also start) to launch an executable in a new window (on Windows), asynchronously, with no (direct) ability to capture the launched executable's output:
From cmd.exe:
start "title" "C:\Users\Bob Builder\Desktop\New Folder\test.exe"
Note:
Specifying "title" - i.e. a self-chosen (console) window title - is required for syntactic reasons in this case: without it, the double-quoted path itself would be interpreted as the window title, and the - implied - executable to launch would be another cmd.exe instance.
Note that if you launch a GUI application this way, the title argument is irrelevant, because no new console window is created.
Conversely, if you launch a console application specified by double-quoted path and therefore must use a title argument, note that "" will result in the new window having no title.
From PowerShell (parameter -FilePath is positionally implied):
Start-Process 'C:\Users\Bob Builder\Desktop\New Folder\test.exe'
Note:
Start-Process does not support specifying a window title, so you may want to call cmd.exe's internal start command for that (or other features not supported by Start-Process, such as specifying the process priority).
To work around quoting problems, invoke cmd.exe's start from PowerShell by passing the entire start command as a single string to cmd /c:
cmd /c 'start "title" "C:\Users\Bob Builder\Desktop\New Folder\test.exe"'
cmd /C start "C:\Users\Bob Builder\Desktop\New Folder\test.exe"
Quotes are your friend. Sometimes even double quotes are too!
Seems like cmd won't work for me. Powershell worked with this script:
$env:Path += ";C:\Users\Bob Builder\Desktop\New Folder\"
test.exe

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

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

TFSBuild Powershell step turning script arguments into strings. How to avoid this?

I've configured the following arguments in a Powershell build step: -protocol:http -portsToOpen 9512,9513,9512.
Once TFSBuild runs the whole script, it throws the following error:
"System.Int32[]". Error: "Cannot convert
"9512,9513,9515" to "System.Int32"
The problem is TFSBuild is running the script sorrounding 9512,9513,9515 with quots (i.e. '9512,9513,9515').
Is there any solution for this? One possible workaround would be running powershell.exe from a Command build step... But I'd like to know if there's some direct solution to this issue.
I don't use TFS but I can tell you PowerShell is parsing that as an integer array (System.Int32[]) and trying to pass that as an argument to -portsToOpen
If you don't have variables on that line you could use the stop parsing operator --% as that
directs Windows PowerShell to refrain from interpreting input as Windows PowerShell commands or expressions.
icacls X:\VMS --% /grant Dom\HVAdmin:(CI)(OI)F
You could also actually put it in quotes so that PowerShell just treats it as a string. I could not find the documentation to back up how it expects input for that parameter so I am not sure.

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