Search code examples
javaunixwmiwmi-querywmic

Java WMIC command is not recognized in Unix


I'm trying to execute the following command from a Java application using a process:

/bin/wmic -U banshee/allotquery --password=******** //banshee.4g4g.com '--delimiter="|"' 'SELECT eventCode,eventType,timeGenerated,User,InsertionStrings,Message FROM win32_NTLogEvent WHERE Logfile="Security" AND NOT Message LIKE "%$%"'.

(The password is hidden for security reasons).

Then I run the command in CentOS 6 CLI it succeeds and I see results. When I try to run it from my Java application it is not recognized.

Code:

public final void executeCommand(final String command, final String query) throws IOException {
    if (Utils.isNullOrEmpty(query)) {
        LogUtils.error(SSHExecClient.class, "No parameters were supplied to the command.");
        throw new IOException("No parameters were supplied to the command.");
    }

    final List<String> cmdTest = new ArrayList<String>();
    cmdTest.add(0, "/bin/wmic");
    cmdTest.add("-U");
    cmdTest.add("banshee/allotquery");
    cmdTest.add("--password=******");
    cmdTest.add("//banshee.4g4g.com");
    cmdTest.add("'--delimiter=\"|\"'");
    cmdTest.add("'SELECT eventCode,eventType,timeGenerated,User,InsertionStrings,Message FROM win32_NTLogEvent WHERE Logfile=\"Security\" AND NOT Message LIKE \"%$%\"'");
    LogUtils.error(LocalExecClient.class, "Executing command: " + Utils.getAsString(cmdTest, " "));
    final ProcessBuilder pb = new ProcessBuilder(cmdTest);
    pb.inheritIO();
    try {
        process = pb.start();
        processReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        errReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        process.waitFor();
    } catch(final java.io.IOException | InterruptedException e) {
        LogUtils.error(LocalExecClient.class, "Unable to execute command.");
        LogUtils.error(LocalExecClient.class, e.getMessage());
        throw new IOException("Unable to execute command.");
    }
}

The result I get is (which means the command is not recognized): Usage:

[-?|--help] [--usage] [-d|--debuglevel DEBUGLEVEL] [--debug-stderr]
        [-s|--configfile CONFIGFILE] [--option=name=value]
        [-l|--log-basename LOGFILEBASE] [--leak-report] [--leak-report-full]
        [-R|--name-resolve NAME-RESOLVE-ORDER]
        [-O|--socket-options SOCKETOPTIONS] [-n|--netbiosname NETBIOSNAME]
        [-W|--workgroup WORKGROUP] [--realm=REALM] [-i|--scope SCOPE]
        [-m|--maxprotocol MAXPROTOCOL] [-U|--user [DOMAIN\]USERNAME[%PASSWORD]]
        [-N|--no-pass] [--password=STRING] [-A|--authentication-file FILE]
        [-S|--signing on|off|required] [-P|--machine-pass]
        [--simple-bind-dn=STRING] [-k|--kerberos STRING]
        [--use-security-mechanisms=STRING] [-V|--version] [--namespace=STRING]
        [--delimiter=STRING]
        //host query

UPDATE: I finally resolved the issue by removing all single and double quotes from the query parameters. The reason for that is the fact that they are added automatically by the compiler.


Solution

  • I finally resolved the issue by removing all single and double quotes from the query parameters. The reason for that is the fact that they are added automatically by the compiler.