Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在PowerShell中从Java执行WMI查询_Java_Powershell_Wmi_Powershell 3.0 - Fatal编程技术网

无法在PowerShell中从Java执行WMI查询

无法在PowerShell中从Java执行WMI查询,java,powershell,wmi,powershell-3.0,Java,Powershell,Wmi,Powershell 3.0,我使用以下Java代码执行PowerShell命令: 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

我使用以下Java代码执行PowerShell命令:

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
    {
        String s = executePSCommand("gwmi Win32_PhysicalMemory | Select Speed");
        System.out.println(s);
    }

    /**
     * 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
    {
        StringBuilder sbInput = new StringBuilder();
        StringBuilder sbError = new StringBuilder();

        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).append("\n");
        }

        inputstream = proc.getErrorStream();
        inputstreamreader = new InputStreamReader(inputstream);
        bufferedreader = new BufferedReader(inputstreamreader);
        while ((line = bufferedreader.readLine()) != null)
        {
            sbError.append(line).append("\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();
    }
}
在PowerShell中,我可以成功执行此查询:

 gwmi Win32_PhysicalMemory | Select Speed
但是,当我使用Java代码执行查询时,我得到以下结果:

Exception in thread "main" java.lang.Exception: The command [cmd /c powershell -ExecutionPolicy RemoteSigned -noprofile -noninteractive gwmi Win32_PhysicalMemory | Select Speed] failed to execute!

Result returned:
'Select' is not recognized as an internal or external command,
operable program or batch file.

我想这个字符就是问题所在。有没有办法解决这个问题?

尝试在命令中添加单个语音标记

String s = executePSCommand("'gwmi Win32_PhysicalMemory | Select Speed'");
或者逃离管道

String s = executePSCommand("gwmi Win32_PhysicalMemory \\| Select Speed");