Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
122 views
in Technique[技术] by (71.8m points)

How to distinguish between a system and normal process by getting processname and id in powershell using Java

Here is my code:

public class PowerShellCommand  {
public static void main(String[] args) throws Exception {
    PowerShellCommand call=new PowerShellCommand();
ids=(call.commandsilent("powershell.exe Get-Process | Format-Table ` Id"));
processes=(call.commandsilent("powershell.exe Get-Process | Format-Table ` ProcessName -AutoSize"));

}
String commandsilent(String command)throws Exception
   {String output="";
              //String command = "powershell.exe  your command";
 //Getting the version
//command = "powershell.exe Get-Process";
 // Executing the command
 Process powerShellProcess = Runtime.getRuntime().exec(command);
 // Getting the results
 powerShellProcess.getOutputStream().close();
 String line;
// System.out.println("Standard Output:");
 BufferedReader stdout = new BufferedReader(new InputStreamReader(
   powerShellProcess.getInputStream()));
 while ((line = stdout.readLine()) != null) {
output+=line+",";

 //System.out.println(line);
 }
 stdout.close();
 //System.out.println("Standard Error:");
 BufferedReader stderr = new BufferedReader(new InputStreamReader(
   powerShellProcess.getErrorStream()));
   stderror="";
 while ((line = stderr.readLine()) != null) {
     stderror+=line;
// System.out.println(line);
 }
 stderr.close();
 //System.out.println("Done");
 return output;
   }
}

I have got the process ids in the string array processes[] and process id in string array ids[]

But I can't find any method to differentiate between them.

question from:https://stackoverflow.com/questions/65650978/how-to-distinguish-between-a-system-and-normal-process-by-getting-processname-an

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just run method commandsilent("powershell.exe Get-Process -IncludeUserName | Select-Object Id, Username"); in main() method .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...