Reputation: 33
I'm working on a Java macro that runs within another program as part of a computational fluid dynamics package. One thing that annoys me about this package is that the monitor going on standby seems to pause the simulations. But seeing as I have access to these macros I thought that I would add a section to change my power settings to keep the monitor awake.
I'm rather new to Java and so I found the easiest way of doing this would be to call PowerScript to do the actual settings changes. So far, I'm able to read the current state of the settings using the following (hidden for readability since this part works).
String command;
command = "powershell.exe $p = Get-CimInstance -Name root\\cimv2\\power -Class win32_PowerPlan -Filter \"IsActive=\'True\'\"; $p.ElementName";
Process powerShellProcess = Runtime.getRuntime().exec(command);
powerShellProcess.getOutputStream().close();
String line;
BufferedReader stdout = new BufferedReader(new InputStreamReader(
powerShellProcess.getInputStream()));
line = stdout.readLine();
System.out.println("The current power mode is: "+line);
stdout.close();
The next step would be to set the power settings using something like this:
String powerMode = "Balanced";
command = "powershell.exe $p = Get-CimInstance -Name root\\cimv2\\power -Class win32_PowerPlan -Filter \"ElementName=\'"+powerMode+"\'\"; Invoke-CimMethod -InputObject $p[0] -MethodName Activate";
System.out.println(command);
powerShellProcess = Runtime.getRuntime().exec(command);
powerShellProcess.getOutputStream().close();
new InputStreamReader(powerShellProcess.getInputStream());
The command prints properly as
powershell.exe $p = Get-CimInstance -Name root\cimv2\power -Class win32_PowerPlan -Filter "ElementName='Balanced'"; Invoke-CimMethod -InputObject $p[0] -MethodName Activate
When running that command (minus the "powershell.exe", of course) in PowerShell works perfectly, but when calling it from Java results in the -Filter "ElementName='Balanced'" returning null.
Can anyone tell me why the filter argument is not being passed properly? It works fine when filtering by "IsActive" as shown in the first part but not when filtering by "ElementName". Could it have something to do with the escape sequence nightmare around the element name?
Upvotes: 2
Views: 139
Reputation: 143
Powershell is very finicky on its handling of quotes on the command line. The easy solution is to send in the query as
-Filter 'ElementName=\"Balanced\"'
For more info on this see https://connect.microsoft.com/PowerShell/feedback/details/376207/executing-commands-which-require-quotes-and-variables-is-practically-impossible
Upvotes: 3