Java - 运行Powershell并向其解析命令

时间:2016-03-22 10:52:02

标签: java powershell command

我正致力于服务台的项目。该服务台有大约10种各种工具可以为客户提供结果。我已经把这些工具放在一个Java-Tool上了。 有一件事不想工作。 客户可以使用多个XenApp / XenDesktop服务器。客户来自服务器,我想让RAM Avarage Util在我的工具的GUI中显示它们。

我想运行一个PowerShell,并将多次命令粘贴到powershell。但它想要工作。在我的例子中,程序在while循环中挂起

while(!file.exists() & !file.canWrite()){}

运行一次后。

我的问题是,我可以运行一次PowerShell并多次粘贴命令吗?在C#中,我尝试了它,它的工作原理。但我想在C#中重写7000行代码。

抱歉我的英语不好。我希望你能理解我的问题,并能帮助我。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.security.auth.login.LoginException;

public class SystemAvarageUtil {

    public static void getSystemAvarageUtil(String desktopServer, String appServer, String personalNummer) throws IOException, InterruptedException{

        final Process process = Runtime.getRuntime().exec("PowerShell -NoExit -Command -");
        while(personalNummer.equals("XXXXXX")){

            File file = new File("H:\\sdassistence\\tmp\\Util.tmp");
            if(file.exists()){
                file.delete();
            }

            PrintWriter commands = new PrintWriter(process.getOutputStream(),true);
            //desktopServer RAM
            commands.println("$ComputerNameDesktop=\""+ desktopServer +"\"");
            commands.println("$ComputerSystemDesktop = Get-WmiObject -ComputerName $ComputerNameDesktop -Class Win32_operatingsystem -Property CSName, TotalVisibleMemorySize, FreePhysicalMemory");
            commands.println("$MachineNameDesktop = $ComputerSystemDesktop.CSName");
            commands.println("$FreePhysicalMemoryDesktop = ($ComputerSystemDesktop.FreePhysicalMemory) / (1mb)");
            commands.println("$TotalVisibleMemorySizeDesktop = ($ComputerSystemDesktop.TotalVisibleMemorySize) / (1mb)");
            commands.println("$TotalVisibleMemorySizeRDesktop = \"{0:N2}\" -f $TotalVisibleMemorySizeDesktop");
            commands.println("$TotalFreeMemPercDesktop = ($FreePhysicalMemoryDesktop/$TotalVisibleMemorySizeDesktop)*100");
            commands.println("$TotalFreeMemPercRDesktop = \"{0:N2}\" -f $TotalFreeMemPercDesktop");
            //commands.println("$TotalVisibleMemorySizeRDesktop GB");
            //commands.println("$TotalFreeMemPercRDesktop %");

            //appServer RAM
            commands.println("$ComputerNameApp=\""+ appServer +"\"");
            commands.println("$ComputerSystemApp = Get-WmiObject -ComputerName $ComputerNameApp -Class Win32_operatingsystem -Property CSName, TotalVisibleMemorySize, FreePhysicalMemory");
            commands.println("$MachineNameApp = $ComputerSystemApp.CSName");
            commands.println("$FreePhysicalMemoryApp = ($ComputerSystemApp.FreePhysicalMemory) / (1mb)");
            commands.println("$TotalVisibleMemorySizeApp = ($ComputerSystemApp.TotalVisibleMemorySize) / (1mb)");
            commands.println("$TotalVisibleMemorySizeRApp = \"{0:N2}\" -f $TotalVisibleMemorySizeApp");
            commands.println("$TotalFreeMemPercApp = ($FreePhysicalMemoryApp/$TotalVisibleMemorySizeApp)*100");
            commands.println("$TotalFreeMemPercRApp = \"{0:N2}\" -f $TotalFreeMemPercApp");
            //commands.println("$TotalVisibleMemorySizeRApp GB");
            //commands.println("$TotalFreeMemPercRApp %");


            commands.println("$TotalVisibleMemorySizeRDesktop+\";\"+$TotalFreeMemPercRDesktop+\";\"+$TotalVisibleMemorySizeRApp+\";\"+$TotalFreeMemPercRApp | Out-File -Encoding UTF8 -FilePath \"H:\\sdassistence\\tmp\\Util.tmp\"");
            commands.println("exit");
            commands.close();


            while(!file.exists() & !file.canWrite()){}
            Thread.sleep(1000);
            FileReader fr = new FileReader(file);
            BufferedReader in = new BufferedReader(fr);

            String test[] = in.readLine().split(";");
            System.out.println(test[0] + " " + test[1] + " " + test[2]);
            fr.close();in.close();process.destroy();
        }
    }

    public static void main(String[] args) throws LoginException, IOException, InterruptedException {

        getSystemAvarageUtil("DesktopServer","AppServer","XXXXXX");

    }


}

0 个答案:

没有答案