1

I created a function that should allow me to execute command.
The first command, executed thanks to setcommand is "powershell" is working well and I get the return of the shell :
"Windows PowerShell Copyright (C) 2009 Microsoft Corporation. Tous droits r‚serv‚s."
My problem is to run a command after powershell is running with out.write function.

//The command is equal to "powershell" and run powershell correctly
private static void executeCommand(String command) {
    try {
    ChannelExec channel = (ChannelExec) session.openChannel("exec");
    ((ChannelExec) channel).setCommand(command);

     OutputStream out = channel.getOutputStream();
     InputStream in = channel.getInputStream();

    channel.connect();
    out.write(("mkdir C:\\test" + "\n").getBytes());
    out.flush();

    byte[] tmp = new byte[1024];
    while (channel.getExitStatus() == -1) {
        while (in.available() > 0) {
            int i = in.read(tmp, 0, 1024);
            if (i < 0)
                break;
            System.out.print(new String(tmp, 0, i));
        }
        if (channel.isClosed()) {
            System.out.println("exit-status: " + channel.getExitStatus());
            break;
        }
        try {
            Thread.sleep(1000);
        } catch (Exception ee) {
            System.out.println(ee);
        }
    }
    channel.disconnect();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSchException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Do you have any solution to solve my problem or any other way to do the job ?

EDIT :

    Process p;

    try {

        p = new ProcessBuilder()
        .inheritIO()
        .command("powershell", "$user='root'; $pass='test'; $passwd=ConvertTo-SecureString -AsPlainText $pass -Force; $cred=New-Object System.Management.Automation.PSCredential -ArgumentList $user, $passwd; Invoke-command -ComputerName 10.64.2.35 -ScriptBlock {Get-ChildItem C:\\} -credential $cred").start();
        p.waitFor();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Jerome Campeaux
  • 353
  • 2
  • 9
  • 19

1 Answers1

3

Why not use pocessbuilder api?

Sample code - update your code accordingly.

 Process p = new ProcessBuilder()
            .inheritIO()
            .command("invoke-command", "-remoteServername", "ServerXYZ",
                    "-filepath", "C:\\scripts\\script.ps1").start();
    p.waitFor();
Sheetal Mohan Sharma
  • 2,908
  • 1
  • 23
  • 24
  • Thank you for reply :) Isn't it -ComputerName for the option ? ServerXYZ correspond to the host right ? And if I have to authenticate, have I just to put my credentials in command in the command() function ? – Jerome Campeaux Dec 23 '15 at 11:51
  • Please refer to javadocs...Commands are just arguments. The key is .inheritIO().command call. public ProcessBuilder command(List commandArgs) here commandArgs is the list containing the program and its arguments. I would say try with some other box with cmd.exe to get started. – Sheetal Mohan Sharma Dec 23 '15 at 15:37
  • Post updated. Seems to work but the problem is that invoke-command is not working, I think it's because I have to use the Set-item function before. and this function is not working. – Jerome Campeaux Dec 23 '15 at 15:46
  • Any errors? This might help https://technet.microsoft.com/en-us/library/hh849719.aspx and http://www.computerperformance.co.uk/powershell/powershell_invoke.htm – Sheetal Mohan Sharma Dec 23 '15 at 15:51
  • yes the error is after the invoke-command, it says that the connection can't be done and the trustedhost is not enable – Jerome Campeaux Dec 23 '15 at 15:54
  • That says it....check this post http://stackoverflow.com/questions/6587426/powershell-remoting-with-ip-address-as-target – Sheetal Mohan Sharma Dec 23 '15 at 15:56