[英]PowerShell command not getting executed using java Process class. While all other commands are working fine
I am trying to execute below command using java Process class but it's not giving me any response or neither its effecting which it should do.我正在尝试使用 java Process class 执行以下命令,但它没有给我任何响应,也没有给我应有的效果。
But when I am executing the command directly on PowerShell its working fine only it's not working using java code.但是,当我直接在 PowerShell 上执行命令时,它工作正常,只是它无法使用 java 代码。 I have tried other PowerShell commands, and all are working fine accept this one.
我已经尝试了其他 PowerShell 命令,并且一切正常,接受这个命令。
It's a command to disable indexing of a drive.这是禁用驱动器索引的命令。
Output its only printing the command and in response of isAlive() method call it replies with false. Output 它只打印命令并响应 isAlive() 方法调用,它回复 false。
Command: powershell.exe Get-WmiObject -Class Win32_Volume -Filter "DriveLetter='I:'" | Set-WmiInstance -Arguments @{IndexingEnabled=$False}
命令:
powershell.exe Get-WmiObject -Class Win32_Volume -Filter "DriveLetter='I:'" | Set-WmiInstance -Arguments @{IndexingEnabled=$False}
powershell.exe Get-WmiObject -Class Win32_Volume -Filter "DriveLetter='I:'" | Set-WmiInstance -Arguments @{IndexingEnabled=$False}
isAlive: false
还活着:假
There is nothing more in the code I am just calling this method from my main class that's it like classObject.disableIndexing("D")
代码中没有更多我只是从我的主 class 调用这个方法就像
classObject.disableIndexing("D")
Note I am executing the same using admin rights only.请注意,我仅使用管理员权限执行相同的操作。 Please help.
请帮忙。
public String disableIndexing(String driveLetter) {
String returnVal="";
String command = "powershell.exe Get-WmiObject -Class Win32_Volume -Filter \"DriveLetter='"+driveLetter+":'\" | Set-WmiInstance -Arguments @{IndexingEnabled=$False} ";
try {
System.out.println(command);
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
String line1="";
String line="";
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line1 = br.readLine()) != null) {
System.out.println(line1);
if(!line1.isEmpty())
System.err.println(line1);
}
System.out.println(p.isAlive());
if(p.exitValue()==0) {
returnVal="Indexing changed Successfully";
}else {
returnVal="Your Drive is Not Responding Try After Some Time";
}
}catch(Exception e) {
e.printStackTrace();
}
return returnVal;
}
The problem here is likely that |
这里的问题很可能是
|
is interpreted by the default shell (eg. cmd.exe) before being passed to powershell.exe
.在传递给
powershell.exe
之前由默认 shell(例如 cmd.exe)解释。
To work around this, use the -EncodedCommand
command line switch to safely pass the command to powershell.exe
:要解决此问题,请使用
-EncodedCommand
命令行开关将命令安全地传递给powershell.exe
:
public String disableIndexing(String driveLetter) {
String driveLetter = "D";
String powerShellCommand = "Get-WmiObject -Class Win32_Volume -Filter \"DriveLetter='" + driveLetter + ":'\" | Set-WmiInstance -Arguments @{IndexingEnabled=$False}";
// Base64-encode the UTF16LE representation of the command
byte[] powerShellBytes = powerShellCommand.getBytes(StandardCharsets.UTF_16LE);
String encodedCmd = new String(Base64.getEncoder().encode(powerShellBytes));
String command = "powershell.exe -EncodedCommand " + encodedCmd;
try {
// proceed the same as before ...
}
catch (Exception e) {
// ...
}
return returnVal;
}
hey guys I got the solution.嘿伙计们我得到了解决方案。 But even I didn't understand how this worked.
但即使是我也不明白这是怎么回事。 So, what I did is while coping my command from one method to another method I accidently left the 2 extra slashes on my String command and executed it.
所以,我所做的是在将我的命令从一种方法处理到另一种方法时,我不小心在我的 String 命令上留下了 2 个额外的斜杠并执行了它。 And it worked.
它奏效了。 Actual Command:
String command = "powershell.exe Get-WmiObject -Class Win32_Volume -Filter \"DriveLetter='H:'\" | Set-WmiInstance -Arguments @{IndexingEnabled=$False}
" It was not working.实际命令:
String command = "powershell.exe Get-WmiObject -Class Win32_Volume -Filter \"DriveLetter='H:'\" | Set-WmiInstance -Arguments @{IndexingEnabled=$False}
" 它不工作。
Command which worked: String command = "powershell.exe Get-WmiObject -Class Win32_Volume -Filter \\\"DriveLetter='H:'\\\" | Set-WmiInstance -Arguments @{IndexingEnabled=$False}
" I still not able to understand how it worked.有效的命令:
String command = "powershell.exe Get-WmiObject -Class Win32_Volume -Filter \\\"DriveLetter='H:'\\\" | Set-WmiInstance -Arguments @{IndexingEnabled=$False}
" 我还是不行能够理解它是如何工作的。 because in actual PowerShell command it shouldn't have that back slash before and after DriveLetter.因为在实际的 PowerShell 命令中,DriveLetter 前后不应该有反斜杠。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.