繁体   English   中英

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

[英]PowerShell command not getting executed using java Process class. While all other commands are working fine

我正在尝试使用 java Process class 执行以下命令,但它没有给我任何响应,也没有给我应有的效果。

但是,当我直接在 PowerShell 上执行命令时,它工作正常,只是它无法使用 java 代码。 我已经尝试了其他 PowerShell 命令,并且一切正常,接受这个命令。

这是禁用驱动器索引的命令。

Output 它只打印命令并响应 isAlive() 方法调用,它回复 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}

还活着:假

代码中没有更多我只是从我的主 class 调用这个方法就像classObject.disableIndexing("D")

请注意,我仅使用管理员权限执行相同的操作。 请帮忙。

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;
        
}

这里的问题很可能是| 在传递给powershell.exe之前由默认 shell(例如 cmd.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;
}

嘿伙计们我得到了解决方案。 但即使是我也不明白这是怎么回事。 所以,我所做的是在将我的命令从一种方法处理到另一种方法时,我不小心在我的 String 命令上留下了 2 个额外的斜杠并执行了它。 它奏效了。 实际命令: String command = "powershell.exe Get-WmiObject -Class Win32_Volume -Filter \"DriveLetter='H:'\" | Set-WmiInstance -Arguments @{IndexingEnabled=$False} " 它不工作。

有效的命令: String command = "powershell.exe Get-WmiObject -Class Win32_Volume -Filter \\\"DriveLetter='H:'\\\" | Set-WmiInstance -Arguments @{IndexingEnabled=$False} " 我还是不行能够理解它是如何工作的。 因为在实际的 PowerShell 命令中,DriveLetter 前后不应该有反斜杠。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM