Runtime.getRuntime()。exec()的输出与执行命令行的结果不同

时间:2018-12-02 16:49:26

标签: java powershell cmd crm truncated

如何从Powershell命令修复截断的输出Java。 当我直接从cmd执行命令行时,会得到此结果

powershell C:\CMDLETS\getaccountattributes.ps1    
Key                 Value
---                               -----    
address2_county 7d8b4132e6a6e44682046b524f3904ca@O=IAM;
15dc3a803c4012479db49b28c9e590c5@O=IAM    
systemuserid a418a064-a1ed-e811-80e9-005056bd633b    
accessmode  Microsoft.Xrm.Sdk.OptionSetValue

但是当我在Java中运行它时:

    ExecuteShellComand executeshellecomand = new ExecuteShellComand();
    String command = "powershell C:\\CMDLETS\\getaccountattributes.ps1"; 
    log.info("avant exctuion du shell");
    String output = executeshellecomand.executeCommand(command);
    log.info("Val of output " +output);
    BufferedReader reader = new BufferedReader( new StringReader(output) )



String executeCommand(String command) {

    StringBuffer output = new StringBuffer();
    StringBuffer outputError = new StringBuffer();


    Process p;
    try {
        p = Runtime.getRuntime().exec(command);
        log.info("runtime powerhsel "+ p.getInputStream().toString());
        int test= p.waitFor();

        String line = "", errorLine = "" ;
        BufferedReader reader =
                       new BufferedReader(new InputStreamReader(p.getInputStream()));

        BufferedReader errinput = new BufferedReader(new InputStreamReader(
                p.getErrorStream()));

        while ((line = reader.readLine())!= null) {
            output.append(line + "\n");

        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return output.toString();

Results :
Key                               Value                                        
---                               -----                                        
address2_county                   7d8b4132e6a6e44682046b524f3904ca@O=IAM;15d...

systemuserid                      a418a064-a1ed-e811-80e9-005056bd633b         
accessmode                        Microsoft.Xrm.Sdk.OptionSetValue 

第一个属性被截断。 请问我如何得到我想要的结果。 谢谢!

1 个答案:

答案 0 :(得分:0)

与用于导出csv的管道一起使用

powershell C:\ CMDLETS \ getaccountattributes.ps1 | Export-csv C:\ CMDLETS \ getaccountattributes.csv