簡體   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