因爲編碼問題,java經過遠程調用winrm形式,在控制檯返回字符中,包括中文時,顯示???,出現中文亂碼!java
經過調整$outputencoding的方式,依然出現亂碼。因此採用曲線實現的方式,經過將結果重定向輸入到文件(指定文件編碼格式),再讀取文件的形式,得到執行腳本的結果。ide
代碼以下:ui
package com.test; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import io.cloudsoft.winrm4j.client.WinRmClientContext; import io.cloudsoft.winrm4j.winrm.WinRmTool; import io.cloudsoft.winrm4j.winrm.WinRmToolResponse; public class RemoteWinrm4JTest { public static void main(String[] args) throws UnsupportedEncodingException, InterruptedException { WinRmClientContext context = WinRmClientContext.newInstance(); WinRmTool.Builder builder = WinRmTool.Builder.builder("192.168.1.128", "Administrator", "password"); //builder.setAuthenticationScheme(AuthSchemes.NTLM); builder.port(5985); builder.useHttps(false); builder.disableCertificateChecks(false); builder.context(context); WinRmTool tool = builder.build(); tool.setOperationTimeout(0L); System.out.println("========"); WinRmToolResponse resp = tool.executePs("C:/Users/Administrator/WindowsAuditPolicy.ps1 |out-file WindowsAuditPolicy_result.txt -encoding utf8"); Thread.sleep(3000); resp = tool.executeCommand("type C:\\Users\\Administrator\\WindowsAuditPolicy_result.txt"); String out = resp.getStdOut(); String err = resp.getStdErr(); System.out.println(out); context.shutdown(); } }