0

Goal: Running bat files remotely for test needs.

Requirements:

  • Waiting till the operation succeeds.
  • Getting the result code and using it.
  • Logging all the output.
  • Situation:

    I am running my code as java application on Windows 7. The application creates the process which runs cmd.exe to run bat from the local machine. The bat file contains a command for psexec to run another bat file remotely.

    I have tried other ways without creating local bat file: tried to run psexec command, but failed to pass more than 1 argument.

    Listing:

    public static void main(String[] args) throws IOException,
            InterruptedException
    {       
        String[] command = { "cmd", };
        Process p = Runtime.getRuntime().exec(command);
    
        new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
        new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
        PrintWriter stdin = new PrintWriter(p.getOutputStream());
        stdin.println("cd C:\\Autotests\\Bat\\");
        stdin.println(".\\localBat.bat");
        // write any other commands you want here
        stdin.close();
        int returnCode = p.waitFor();
        System.out.println("Return code = " + returnCode);
    
    }
    
    public static class SyncPipe implements Runnable
    {
        public SyncPipe(InputStream istrm, OutputStream ostrm)
        {
            istrm_ = istrm;
            ostrm_ = ostrm;
        }
    
        public void run()
        {
            try
            {
                final byte[] buffer = new byte[1024];
                for (int length = 0; (length = istrm_.read(buffer)) != -1;)
                {
                    ostrm_.write(buffer, 0, length);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    
        private final OutputStream  ostrm_;
        private final InputStream   istrm_;
    }
    

    Contents of localBat.bat:

    C:\\PSTools\\psexec.exe \\**.**.**.** -s -u domain\user -p password C:\somepath\remoteBat.bat
    

    What's the problem?

    If I run the localBat.bat manually from the command line I get all the output of the remoteBat.bat displayed in the console. But when I run my application, the remoteBat.bat runs, I get the exit code stroed but I don't get the entire output, only these lines:

    PsExec v2.11 - Execute processes remotely
    Copyright (C) 2001-2014 Mark Russinovich
    Sysinternals - www.sysinternals.com
    Connecting to **.**.**.**...
    Starting PSEXESVC service on **.**.**.**...
    Connecting with PsExec service on **.**.**.**...
    Starting C:\somepath\remoteBat.bat on **.**.**.**...
    C:\somepath\remoteBat.bat exited on **.**.**.** with error code 0.
    

    Impact

    I can't handle and analyze the situation when the exit code is different from 0.

    Please, help me, I'm stuck here.

    • this is known issue - http://forum.sysinternals.com/faq-common-pstools-issues_topic15920.html . To execute a command on a remote script and get the output you can use WMIC or xcmd - > http://feldkir.ch/xcmd.htm – npocmaka May 28 '15 at 10:45
    • Thanks for advice. I will check WMIC. I have tried using xcmd firstly, but it did not work for me. – Funia Prazdnikova May 28 '15 at 12:50
    • have on mind that xcmd does not work against localhost.Check also this : http://stackoverflow.com/questions/18182690/how-to-start-remotely-process-in-powershell – npocmaka May 28 '15 at 12:55
    • It's over comlicated for me ( i don't have enough ti,e to learn using WMI. – Funia Prazdnikova May 29 '15 at 08:33
    • 1
      I have found a work-around: the batch file on remote machine rights its output to a shared folder so that I can simply read it in my application. – Funia Prazdnikova May 29 '15 at 09:51

    0 Answers0