简体   繁体   English

PowerShell 命令未使用 java 进程 class 执行。而所有其他命令工作正常

[英]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.

相关问题 Java使用进程生成器执行Adb命令时出错 - Java getting error while executing Adb Commands using process builder 获取类的数组列表的索引。 爪哇 - Getting indexof arraylist of class. java 作为servlet而不是java类工作。 为什么? - working as servlet but not as java class. why? java.lang.VerifyError:验证器拒绝类。 代码在调试模式下工作正常,但在发布模式下不会抛出此错误 - java.lang.VerifyError: Verifier rejected class. Code working fine in debug mode, but not throwing this error in release mode 错误:找不到或加载主类。 Powershell / Java - Error: Could not find or load main class. Powershell/Java 编译Java包类。 无法访问课程。 使用软件包时,类文件包含错误的类 - compiling java package classes. Cannot access Class. class file contains wrong class while working with packages 在运行时将命令发送到命令行Java程序 - Send commands to a command line Java program while it is working 在Windows cmd中使用Java中的Process类运行多个命令 - run multiple commands in windows cmd using Process class in java '。类。' java中的错误 - '.class.' error in java 从Java执行时,MYSQL命令给出错误。 相同的命令在Linux Terminal上可以正常工作。 可能是什么原因? - MYSQL command gives error while being executed from java. Same command works fine on Linux Terminal. What can be the reason?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM