Cannot execute WMI query from Java in PowerShell -


I use this Java code to execute the PowerShell command:

  import Java.io.buffferedReader; Import java.io.file; Import java.io.InputStream; Import java.io.InputStreamReader; / ** * Operation to execute Windows PowerSlow scripts from Java. * * @author Brian Thorne * / Public class sample {public static void throws exceptions} {String S = executePSCommand ("gwmi Win32_PhysicalMemory | Select Speed"); Println (s); } / ** * executes a power-shell command. * * @Valame command command * @ Returns return results as strings. An error occurs if * exceptions @throws * / public static string throws executePSCommand (string command) exception {string cmd = "cmd / c powershell -ExecutionPolicy RemoteSigned -noprofile -noninteractive" order; Return ACC (CMD); } / ** * Executes a power script * * @ Param script file name * @PRAM Any argument to go to the ELGS script * * @throws exception if an error occurs * / public static string Execution PSScript (string scriptfilemenym, string arguments) throws exceptions (if new !! file (script filemen) .exists ()) new exception throw ("script file does not exist" Yes: "+ scriptfilemenum"); String CMD = "CMD / C PowerHail - Execution Policy Remote Sequode - Noprophil - Non-Retraactive - File" "+ Script Filemen +" \ ""; If (args! = Null & args.length ()> 0) cmd + = "" + args; Return ACC (CMD); } / ** * Executing a batch file If you want to call parsehale from batch file then you need to use this syntax: powershell -ExecutionPolicy RemoteSigned -NoProfile * -one-interactive -file c: / temp / Script.ps1 * * @ The ultimate batchfile name advises any parameter to pass the file name of the batch file to the * ultimate batch file * Remove result as string * @ throw exception if an error occurs * / Sa Wajnik steady string Nishpadnbacfail (string Bacfailnam, string parameter) throws Exception (if !! new file (Bacfilmen). Axis ()) throw new Exception ( "batch file does not exist:" + Bacfailnam); String CMD = "CMD / C" "+ BatchFilename +" \ ""; If (Param! = Null & amp; params.length ()> 0) CMD + = "" + parameter; Return ACC (CMD); } Private Static String Command (string command) throws exception {StringBuilder sbInput = new StringBuilder (); Stringbuilder sbError = new stringbilder (); Runtime runtime = runtime.at timetime (); Process proc = runtime.exec (command); Proc.getOutputStream () pass () .; InputStream inputstream = proc.getInputStream (); InputStreamReader inputstream reader = new inputstream reader (inputstream); Buffer Redder Buffriedr = New Buffettder (InputStream Reader); String line; While ((line = buffed reader. Readline ()) = null {sbInput.append (line) .append ("\ n");} inputstream = proc.getErrorStream (); Inputstreamreader = new inputstream reader (inputstream); Bufferedreader = New BufferedReader (InputStream Reader); while ((line = buffed reader .readline ())! = Null {sbError.append (line) .append ("\ n");} If (sbError.length () & gt; Failed to execute new exception throw ("command [" + command + "]! \ N \ nThe result returned: \ n" + sbError.toString ()); Return "Command [" + "Command +"] Successfully Executed! \ N \ n Results Revert to Income : \ N "+ sbInput.toString ();}}  

In PowerShell I can execute this query successfully:

  gwmi Win32_PhysicalMemory | Choose Speed ​​ 

But when I execute a query using Java code, I get this result:

  Exceptions in the thread "main" Java.lang.Exception: Command [CMD / C PowerHail - Acccurity Policy RemoteAnited - NPropHill - Nonprofit Gwamy Win32_PhysicalMemory | Choose Speed] Failed to execute! The result returned: 'Selection' has not been recognized as an internal or external command, operative program or batch file.  

I think this character is problem | Is there any way to fix this?

Try adding a single speech mark to your command

  string S = executePSCommand ("'gwmi Win32_PhysicalMemory | Select Speed' ');  

or avoid pipe

  string s = executePSCommand ("gwmi win32_PhysicalMemory \\ | speed select");  

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -