解决winrm4j中文乱码问题和执行脚本文件

基于winrm4j版本0.10.0上重新写了个执行类,原代码关于设置编码的地方都写死了,无法重新设置,所以返回的中文会是乱码。这问题已向原作者反馈了https://github.com/cloudsoft/winrm4j/issues/133 ,希望后期能完善。

我自己重写了一个类,可以把这代码弄到自己项目:https://gitee.com/lnkToKing/open-share-code/blob/master/src/utils/WinRmExecutor.java
注意需要依赖 org.apache.commons:commons-lang3 工具包

使用方式

        final WinRmClientBuilder builder = WinRmClient
                .builder(WinRmExecutor.getEndpointUrl("127.0.0.1", null))
//                .builder("http://127.0.0.1:5985/wsman")
                .credentials("test", "123456");
        try (WinRmExecutor executor = new WinRmExecutor(builder)) {
//            executor.setUseHttps(true);
            WinRmToolResponse response = executor.executeCmd("ipconfig");
            System.out.println(response.getStatusCode());
            System.out.println(response.getStdOut());
            System.out.println(response.getStdErr());

            System.out.println("---------------------");
            response = executor.executePs("echo $env:temp");
            System.out.println(response.getStatusCode());
            System.out.println(response.getStdOut());
            System.out.println(response.getStdErr());

            System.out.println("---------------------");
            String script = "@echo off\n" +
                    "echo %1 %2\n" +
                    "echo %3";
            response = executor.executeBatScript(script, "aa bb 123", null);
            System.out.println(response.getStatusCode());
            System.out.println(response.getStdOut());
            System.out.println(response.getStdErr());
        }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
Ansible是一种用于自动化IT环境的工具,它可以简化管理和部署任务。虽然Ansible最初是为Linux系统设计的,但它也可以用于Windows系统。在这里,我将介绍一些适用于Windows管理员的Ansible技巧。 1. 安装Ansible:首先,你需要在Windows系统上安装Ansible。你可以使用PowerShell命令来安装Ansible。在PowerShell中运行以下命令: ```powershell Invoke-WebRequest -Uri https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1 -OutFile ConfigureRemotingForAnsible.ps1 .\ConfigureRemotingForAnsible.ps1 ``` 2. 配置Ansible:完成安装后,你需要配置Ansible以使用它。在Windows系统上,你需要打开WinRM服务并启用基本身份验证。在PowerShell中运行以下命令: ```powershell Set-Item -Path WSMan:\localhost\Service\Auth\Basic -Value $true Set-Item -Path WSMan:\localhost\Service\AllowUnencrypted -Value $true Start-Service WinRM ``` 3. 编写Playbook:现在,你可以编写Ansible Playbook来自动化Windows系统上的任务。你可以使用PowerShell模块来运行PowerShell命令。以下是一个使用PowerShell模块的示例Playbook: ```yaml - name: Run PowerShell command hosts: windows gather_facts: no tasks: - name: Run PowerShell command win_shell: Get-Process ``` 这个Playbook会在Windows系统上运行Get-Process命令,并返回进程列表。 4. 运行Playbook:完成Playbook编写后,你可以使用ansible-playbook命令来运行它。在PowerShell中,你可以使用以下命令来运行Playbook: ```powershell ansible-playbook playbook.yml -i inventory.ini -u username -k ``` 在这里,playbook.yml是你的Playbook文件,inventory.ini是你的主机清单文件,username是你的Windows用户名,-k选项将提示你输入密码。 这些是一些适用于Windows管理员的Ansible技巧。通过使用Ansible,你可以自动化Windows系统上的任务,并简化管理和部署。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值