如何通过在powershell中使用java获取processname和id来区分系统进程和普通进程

htrmnn0y  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(230)

这是我的密码:

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;
   }
}

我在字符串数组processs[]中获得了进程id,在字符串数组ids[]中获得了进程id
但我找不到任何方法来区分它们。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题