Command works on command line, but fails in batch file - windows

When trying to setup Glassfish configuration in a batch file there is one command that works when run directly from the command line - but fails when put in a windows batch file.
The command:
call asadmin.bat create-auth-realm --classname com.sun.enterprise.security.auth.realm.ldap.LDAPRealm --property jaas-context="ldapRealm":directory="ldap\://domain.com\:389:base-dn=dc\=domain,dc\=com:group-base-dn=ou\=Groups,ou\=domain,dc\=com":search-bind-dn="CN\=username,OU\=Accounts,OU\=domain,DC\=com":search-bind-password="password":search-filter="(&(objectCategory\=user)(sAMAccountName\=%s))":group-search-filter="(&(objectCategory\=group)(member\=%d))" a-realm
When run on the command line exactly as above it completes with the response:
Command create-auth-realm executed successfully.
When run from a batch file exactly as above it fails with the response:
(member\ was unexpected at this time.
Note that the escaping of some equals characters is for Glassfish, not an attempt at escaping characters for the windows batch command.
My guess is that when run in a batch file, the batch file is treating some characters as special. I have tried escaping the parentheses with no luck.
How can this command work in a batch file!?

Your problem is in the variables %s and %d.
If they need to be interpreted by the batch file (they are environment variables), they should be %s% and %d%.
IF they are not environment variables, and need to be interpreted in (don't know what glassfish is), then they should be %%d and %%s

It seems like a problem with the percent signs.
In a batch file percent signs are removed, when no matching percent sign is found or a enclosed variable isn't defined.
On the command line they simply stay unchanged.
In a batch file a percent sign can be escaped by a second percent.
call asadmin.bat create-auth-realm --classname com.sun.enterprise.security.auth.realm.ldap.LDAPRealm --property jaas-context="ldapRealm":directory="ldap\://domain.com\:389:base-dn=dc\=domain,dc\=com:group-base-dn=ou\=Groups,ou\=domain,dc\=com":search-bind-dn="CN\=username,OU\=Accounts,OU\=domain,DC\=com":search-bind-password="password":search-filter="(&(objectCategory\=user)(sAMAccountName\=%%s))":group-search-filter="(&(objectCategory\=group)(member\=%%d))" a-realm

Windows use of carets ( ^ ) to escape special characters. Try to replace the backslashes at the carets.

call asadmin.bat create-auth-realm --classname com.sun.enterprise.security.auth.realm.ldap.LDAPRealm --property jaas-context="ldapRealm":directory="ldap\://domain.com\:389:base-dn=dc\=domain,dc\=com:group-base-dn=ou\=Groups,ou\=domain,dc\=com":search-bind-dn="CN\=username,OU\=Accounts,OU\=domain,DC\=com":search-bind-password="password":search-filter="(&(objectCategory\=user)(sAMAccountName\=%s))":group-search-filter="(&(objectCategory\=group)(member\=%d))" a-realm
^
the &'s need to be escaped with ^ I think. try it... ;)

Related

proper way to remove double quotes from string in batch

I've got a batch script (app1.bat) calling another batch script (app2.bat) which itself calls a program in windows (program.exe).
app2.bat calls program.exe with a parameter after a flag in this way:
program.exe -f Parameter with whitespaces coming into the program
What I want to do is to pass the phrase that comes to program.exe from app1.bat into app2.bat but i don't know how to properly handle the doublequotes. Currently I am passing the phrase from app1.bat to app2.bat in double quotes and inside an app2.bat (prior to executing program.exe) I get rid of the quotes like that:
inside app1.bat
call app2.bat "Parameter with whitespaces coming into the program"
inside app2.bat
set old_phrase=%1%
set new_phrase=%old_phrase:"=%
program.exe -f %new_phrase%
old_phrase is
"Parameter with whitespaces coming into the program"
and new_phrase I end up with is
Parameter with whitespaces coming into the program
Is there any standard way to handle such a situation (being passing a string to an external program which expects a tring without quotes and being ok with whitespaces, whereas batch does not allow for no-quotes-and-whitespaces strings)
When you execute call /? from cmd to launch the help you will see quite a bit around expansion of %n
The first one states:
%~1 - expands %1 removing any surrounding quotes (")
You can therefore dump all the other set commands and simply run this in your batch file:
program.exe -f %~1

How to escape pipe character from PowerShell command line to pass into non-PowerShell command

I'm in a PowerShell console session, trying to run a Gradle script that exists in the cwd from the PowerShell command line.
The Gradle command accepts an argument which includes quotes that can contain one or more pipe characters, and I can't for the life of me figure out how to get PowerShell to escape it using just one line (or any number of lines, actually - but I'm not interested in multi-line solutions).
Here's the command:
./gradlew theTask -PmyProperty="thisThing|thatThing|theOtherThing"
...which produces this error:
'thatThing' is not recognized as an internal or external command, operable program or batch file.
I've tried all of these variants, none of which work. Any idea?
./gradlew theTask -PmyProperty="thisThing`|thatThing`|theOtherThing"
./gradlew theTask -PmyProperty=#"thisThing|thatThing|theOtherThing"
./gradlew theTask -PmyProperty="thisThing\|thatThing\|theOtherThing"
./gradlew theTask -PmyProperty="thisThing\\|thatThing\\|theOtherThing"
./gradlew theTask -PmyProperty="thisThing^|thatThing^|theOtherThing"
Well, I have found that:
First call your script like this as I first suggested:
./gradlew theTask -PmyProperty="thisThing^|thatThing^|theOtherThing"
Then modify your gradlew.bat script by adding quotes:
set CMD_LINE_ARGS="%*"
The problem is: now CMD_LINE_ARGS must be called within quotes or the same error will occur.
So I assume that your command line arguments cannot be something else and I'm handling each parameter one by one
rem now remove the quotes or there will be too much quotes
set ARG1="%1"
rem protect or the pipe situation will occur
set ARG1=%ARG1:""=%
set ARG2="%2"
set ARG2=%ARG2:""=%
set ARG3="%3"
set ARG3=%ARG3:""=%
echo %ARG1% %ARG2% %ARG3%
The output is (for my mockup command):
theTask -PmyProperty "thisThing|thatThing|theOtherThing"
The "=" has gone, because it has separated parameters from PowerShell. I suppose this won't be an issue if your command has standard argument parsing.
Add as many arguments as you want, but limit it to 9 anyway.
Found another possibility here. One can use single quotes and double quotes like this. Not sure if that helps to solve the original problem but it helped to solve mine.
'"thisThing|thatThing|theOtherThing"'

using the DOS start command when passed arguments have quotes

I have a question about the DOS start command.
I have already read this topic:
Using the DOS “start” command with parameters passed to the started program
Using the "start" command with parameters passed to the started program
but my question is a little different.
I have this problem: I need to pass paths that need to be quoted.
For example, if path have no quotes this works fine:
start "" app.exe -option c:\myapp\myfile.txt
but if path have double quotes it doesn't works.
I have this line in my BATCH file:
start "" myapp.exe -option %mypath%
and when %mypath% contains double quotes (paths that have spaces or other characters in names) the start command returns very strange results.
Thanks
Sandro
Normally it's not a problem to use parameters there with quotes, but you get problems if your app-path has also quotes.
Then you need to add an extra CALL statement.
start "" app.exe -option c:\myapp\myfile.txt - Works
start "" app.exe -option "c:\myapp\myfile.txt" - Works
start "" "app.exe" -option c:\myapp\myfile.txt - Works
start "" "app.exe" -option "c:\myapp\myfile.txt" - Don't works
start "" CALL "app.exe" -option "c:\myapp\myfile.txt" - Works
This might help, but it is a bit way round about method and slight modification may required to suit your need.
The idea is to:
Dump the environment variable which has quotes to a text file with a predefined name. Like:"set mypath2 > withQt.bat"
Use windows power shell or some third party tool to find and replace quotes in that file.
Create another text file (one time step only) containing string "Set "
Use copy command to append the file mentioned in step2 with the file created in step3 and create a batch file with a predefined name. Like: copy base.bat + withQt.bat withtqt.bat
Run the batch file, which creates another/replaces the environment variable with value without quotes.
Sorry, I couldn't get something more elegant at this time.

Why does appcmd.exe behave differently when executed inside a batch file?

I have the following appcmd to add an exception to IIS7's ISAPI and CGI restrictions. The exception I am adding should look like:
c:\perl\bin\perl.exe "%s" %s
Here is the command line:
appcmd set config -section:isapiCgiRestriction "-+[path='c:\perl\bin\perl.exe \"%s\" %s', allowed='true', description='Perl CGI']"
If execute this from the command line it does this correctly, however if I execute this inside a .cmd batch file the path gets mangled and ends up looking like:
c:\perl\bin\perl.exe "s
The trouble seems arise because I have to escape the quotation marks around the first %s perl.exe parameter. But why this should behave differently in a batch file is a bit of a puzzle.
Can anyone explain why this is happening?
The problem is that the command processor reads your "%s" %s and finds two mathing % signs, so this makes a valid batch variable (namely %" %). And after expanding that into nothing, only your "s remains.
You can escape a single %-sign in your batch file by doubling it, like this:
c:\perl\bin\perl.exe "%%s" %%s

CMD: Check for quotes

Let's say, the user drag and drops an file into my batch file, which causes that the batch file copies the file to a certain directory. the problem is the following command:
copy "%1" "C:\path\to\it"
The problem here is the quotes around%1. When you drag and drop something in a batch file, normally, it wouldn't put quotes, so everything's fine. But when i convert my batch file to EXE, it actually puts quotes there.
So, what i want to do is, checking if the file does have quotes, if not, than do the command above, otherwise, do the command without quotes.
Thanks.
Would the following work?
copy %1 "C:\Dir1\Dir2"
my few attempts to find a problem not quoting %1 have not resulted in adverse effects.
Not sure if the answer given here was what you were seeking, and I may be a couple of years late, but I would like to offer an alternate solution to your quoting problem as I have run into it something similar myself. Maybe it will benefit someone else.
You ARE able to strip the quotes from around variables, including the ones that are dragged and dropped. Here's how:
add a tilde to the %n variable (e.g., copy %~1 "C:\path\to\it")
For other variables within the batch file, use a similar technique, this time performing a substitution of the double-quote for nothing :"=, as in:
set filename="C:\path\to\it"
echo %filename% (will give you "C:\path\to\it")
set noquotefilename=%filename:"=%
echo %noquotefilename% (will give you C:\path\to\it without the quotes)
It is not my original solution, but I found it near the bottom of the following post:
Removing double quotes from variables in batch file creates problems with CMD environment
The substitution technique has other applications, as you could easily substitute any character for any other (e.g., :\=_ to substitute a back-slash for an underscore).
Hope this helps!
The problem is the process by which you are converting to EXE. Try a different BAT to EXE converter.

Resources