0

I am using Java ProcessStream to write input commands to powershell on my local machine. Now, the problem is that along with the output, I am seeing input commands as well. How can I restrict input command from being shown on the output.

Below is the code to reproduce the same:

public class Example {
    public static boolean isAlive(Process o) {
        try {
            p.exitValue();
            return false;
        } catch (Exception e) {return true;}
    }
    
    public stati void main(String[] args) throws IOException {
        ProcessBuilder builder = new ProcessBuilder("powershell");
        builder.redirectErrorStream(true);
        Process process = builder.start();
        InputStream out = process.getInputStream();
        OutputStream in = process.getOutputStream();
        String bufferString = "echo hello \n";
        byte[] buffer = bufferString.getBytes();
        int n = buffer.length;
        
        in.write(buffer, 0, n);
        in.flush();
        
        while(isAlive(process)) {
            int no = out.available();
            if (no >0) {
                int m = out.read(buffer, 0, Math.min(no, buffer.length));
                System.out.println(new String(buffer, 0, m));
            }
        }
    }
}

OUTPUT:

PS C://> echo hello
hello

I need only "hello" in output.

Vishwanath
  • 149
  • 2
  • 14
  • I'm afraid it is impossible to get rid of `PS C://>` part. So you have to filter your input stream yourself. – talex Apr 09 '21 at 13:09

1 Answers1

0

The following works for me (but the java program never terminates and I have to kill it).

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class Example {
    public static boolean isAlive(Process p) {
        try {
            p.exitValue();
            return false;
        }
        catch (Exception e) {
            return true;
        }
    }
    
    public static void main(String[] args) throws IOException {
        ProcessBuilder builder = new ProcessBuilder("powershell", "-NoLogo");
        builder.redirectErrorStream(true);
        Process process = builder.start();
        InputStream is = process.getInputStream();
        try (InputStreamReader isr = new InputStreamReader(is);
             BufferedReader out = new BufferedReader(isr)) {
            OutputStream in = process.getOutputStream();
            String bufferString = "echo hello" + System.lineSeparator();
            byte[] buffer = bufferString.getBytes();
            in.write(buffer);
            in.flush();
            while (isAlive(process)) {
                String line = out.readLine();
                if (!line.startsWith("PS")) {
                    System.out.println(line);
                }
            }
        }
    }
}

The output produced by the above code is simply:

hello

@talex mentioned the following in his comment to your question:

it is impossible to get rid of PS C://> part

This is not true since you can customize the PowerShell prompt but you wrote that you don't want to see the echo hello in the output and that is not possible and therefore, as @talex mentioned:

you have to filter your input stream yourself

Not displaying echo hello means not displaying the entered command. Imagine that you open a PowerShell window and don't see the command you entered but after you hit ENTER you want to see the output. So I don't think that there is a way to not display echo hello and therefore, in the above code, I don't print lines that start with the PowerShell prompt.

You can find details about launching PowerShell in about_PowerShell_exe

Abra
  • 19,142
  • 7
  • 29
  • 41
  • Thanks Abra for the detailed answer. But my use case is to allow "\n" as part of command. For e.g. command can be: "echo x; \n echo y". The above code will not remove "echo y" part of the command. – Vishwanath Apr 09 '21 at 16:12