Reputation: 636
Short: I am trying to run a command in the ProcessBuilder:
public static String execute(String cmd) {
System.out.println(cmd);
ProcessBuilder builder = new ProcessBuilder("powershell.exe", cmd);
StringBuilder fullStatus = new StringBuilder();
String line = null;
Process reg;
builder.redirectErrorStream(true);
try {
reg = builder.start();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(reg.getInputStream()));
while((line = bufferedReader.readLine()) != null) {
fullStatus.append(line).append("\n");
}
reg.destroy();
} catch (IOException e1) {
e1.printStackTrace();
}
return fullStatus.toString();
}
If i execute following command:
Get-WmiObject -Query " SELECT * FROM Win32_Printer WHERE Default=$true"
it ignores the " in my file.
Get-WmiObject : Es wurde kein Positionsparameter gefunden, der das Argument "*" akzeptiert. In Zeile:1 Zeichen:1 + Get-WmiObject -Query SELECT * FROM Win32_Printer WHERE Default=$true + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetWmiObjectCommand
How can i fix this?
Upvotes: 2
Views: 609
Reputation: 19684
It looks like your ProcessBuilder
is removing the quotes from your cmd
argument. You can work around this in two ways:
Escaping the quotes:
public static String execute(String cmd) {
cmd = cmd.replace('"',"\\\"")
...
Calling the command with arguments that don't need quotes:
Get-WmiObject -Class Win32_Printer -Filter { Default = True }
Upvotes: 1