【问题标题】:Execute WQL with Java using PowerShell使用 PowerShell 使用 Java 执行 WQL
【发布时间】:2014-06-14 13:10:07
【问题描述】:

我测试了这个在 PowerShell 中执行 WQL 查询的 Java 代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Operations for executing Windows Powershell Scripts from Java.
 *
 * @author Brian Thorne
 */
public class Sample
{

    public static void main(String[] args) throws Exception
    {
        executePSCommand("Get-WmiObject Win32_Processor");
    }

    /**
     * Executes a Powershell command.
     *
     * @param command the command
     * @return the result as String.
     * @throws Exception if an error occurs
     */
    public static String executePSCommand(String command) throws Exception
    {
        String cmd = "cmd /c powershell -ExecutionPolicy RemoteSigned -noprofile -noninteractive " + command;
        return exec(cmd);
    }

    /**
     * Executes a Powershell script.
     *
     * @param scriptFilename the filename of the script
     * @param args any arguments to pass to the script
     * @return the result as String.
     * @throws Exception if an error occurs
     */
    public static String executePSScript(String scriptFilename, String args) throws Exception
    {
        if (!new File(scriptFilename).exists())
            throw new Exception("Script file doesn't exist: " + scriptFilename);

        String cmd = "cmd /c powershell -ExecutionPolicy RemoteSigned -noprofile -noninteractive -file \"" + scriptFilename + "\"";
        if (args != null && args.length() > 0)
            cmd += " " + args;
        return exec(cmd);
    }

    /**
     * Executes a batch file. If you want to call Powershell from the batch file you need to do it using this syntax: powershell -ExecutionPolicy RemoteSigned -NoProfile
     * -NonInteractive -File c:/temp/script.ps1
     *
     * @param batchFilename the filename of the batch file
     * @param params any parameters to pass to the batch file
     * @return the result as String.
     * @throws Exception if an error occurs
     */
    public static String executeBatchFile(String batchFilename, String params) throws Exception
    {
        if (!new File(batchFilename).exists())
            throw new Exception("Batch file doesn't exist: " + batchFilename);

        String cmd = "cmd /c \"" + batchFilename + "\"";
        if (params != null && params.length() > 0)
            cmd += " " + params;
        return exec(cmd);
    }

    private static String exec(String command) throws Exception
    {
        StringBuffer sbInput = new StringBuffer();
        StringBuffer sbError = new StringBuffer();

        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec(command);
        proc.getOutputStream().close();
        InputStream inputstream = proc.getInputStream();
        InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
        BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

        String line;
        while ((line = bufferedreader.readLine()) != null)
        {
            sbInput.append(line + "\n");
        }

        inputstream = proc.getErrorStream();
        inputstreamreader = new InputStreamReader(inputstream);
        bufferedreader = new BufferedReader(inputstreamreader);
        while ((line = bufferedreader.readLine()) != null)
        {
            sbError.append(line + "\n");
        }

        if (sbError.length() > 0)
            throw new Exception("The command [" + command + "] failed to execute!\n\nResult returned:\n" + sbError.toString());

        return "The command [" + command + "] executed successfully!\n\nResult returned:\n" + sbInput.toString();
    }
}

出于某种原因,当我运行代码时,我没有得到任何输出。你能帮忙修复这个代码吗?

【问题讨论】:

    标签: java powershell wmi powershell-2.0


    【解决方案1】:

    您的 main 方法只调用executePSCommand,它返回一个字符串。你永远不会对返回值做任何事情,因此可能不会出现任何输出。

    这样的事情怎么样,

    String s = executePSCommand("Get-WmiObject Win32_Processor");
    System.Out.PrintLn(s);
    

    【讨论】:

      猜你喜欢
      • 2010-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多