Command-line arguments to an exe

2019-05-12 07:39发布

I have a slight problem with Powershell strings. There is a Java program I'm using called SymmetricDS which accepts arguments to a Windows batch file for certain administrative functions. One of these is to open registration for a node, passing a string containing the node's group and id:

& .\bin\sym.bat -p symmetric.properties --open-registration 'store,01'

In the batch file, java is called, with properties I passed above being passed in like so:

java -Dthis -Dthat -Dfoo -Dbar %1 %2 %3 %4 %5 %6 %7 %8 %9

The function in the java itself fails after this part:

int index = argument.trim().indexOf(",");
if (index < 0) {
    throw new SymmetricException("LauncherMissingFilenameTriggerSQL", OPTION_OPEN_REGISTRATION);
}

index apparently is -1. Why it is not finding the comma character, I don't know. I just know that when I run the same in a .cmd or .bat file, it works just fine:

.\bin\sym.bat -p symmetric.properties --open-registration "store,01"

I suspect it has something to do with Powershell's string being Unicode, and something improper happens in their journey from there into a batch file and then into java.

Anybody know how I can call this directly in Powershell?

Resolved: zdan's answer works, as does tricking Powershell a little bit like this:

.\bin\sym.bat -p symmetric.properties --open-registration '"store,01"'

1条回答
▲ chillily
2楼-- · 2019-05-12 08:38

It's not a unicode issue. You stumbling against powershell's parser. When you invoke the call operator, powershell parses the rest of your arguments and then passes them to the script you are calling. The single quotes around the string 'store,01' effectively get stripped and gets interpreted a list that is expanded into two separate arguments.

Just run cmd.exe directly, while carefully quoting your command string:

 cmd.exe /c ' .\sym.bat -p symmetric.properties --open-registration "store,01" '

Note that you have to pass the command to cmd.exe as a single string, otherwise it will be parsed by powershell.

查看更多
登录 后发表回答