• java WinRM 远程连接 windows10 执行脚本


    服务端环境配置

    1、开启windows 远程管理

    在这里插入图片描述
    在这里插入图片描述

    选择专用网络
    在这里插入图片描述

    点击确定退出

    2、将网络类型修改成专用网络

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FRNIiNtS-1663211586894)(C:\Users\SERVER\OneDrive\桌面\work\img\image-20220915102922345.png)]

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1GwY7fcj-1663211586894)(C:\Users\SERVER\OneDrive\桌面\work\img\image-20220915102939857.png)]

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NUkga23g-1663211586894)(C:\Users\SERVER\OneDrive\桌面\work\img\image-20220915102956544.png)]
    一般使用专用网络,不建议使用公用网络
    文档地址
    官方文档

    3、配置 WinRM

    使用管理员权限进入 PowerShell

    #开启WinRM远程管理
    Enable-PSRemoting –force
    
    #设置WinRM自启动
    Set-Service WinRM -StartMode Automatic
    
    #对WinRM服务进行快速配置,包括开启WinRM和开启防火墙异常检测,默认的5985端口
    winrm quickconfig
    
    弹出选项输入 y
    
    #为WinRM服务配置认证
    winrm set winrm/config/service/auth '@{Basic="true"}'
    
    #为WinRM服务配置加密方式为允许非加密:
    winrm set winrm/config/service '@{AllowUnencrypted="true"}'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    4、重启 winRM 服务

    windows + R 输入 services.msc

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YmwKVTGY-1663211586895)(C:\Users\SERVER\OneDrive\桌面\work\img\image-20220915104200620.png)]

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HsggekB2-1663211586895)(C:\Users\SERVER\OneDrive\桌面\work\img\image-20220915104254282.png)]
    鼠标右键重新启动

    客户端连接

    pom.xml

    <dependency>
      <groupId>io.cloudsoft.windows</groupId>
      <artifactId>winrm4j</artifactId>
      <version>0.12.3</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    WinRMHelper

    package com.info2soft.core.service.impl;
    
    import io.cloudsoft.winrm4j.client.WinRmClientContext;
    import io.cloudsoft.winrm4j.winrm.WinRmTool;
    import io.cloudsoft.winrm4j.winrm.WinRmToolResponse;
    
    public class WinRMHelper {
    
        private String ip;
    
        private String username;
    
        private String password;
    
        public static final int DEFAULT_PORT = 5985;
    
        public WinRMHelper(final String ip, final String username, final String password) {
            this.ip = ip;
            this.username = username;
            this.password = password;
        }
    
        public String execute(final String command) {
            WinRmClientContext context = WinRmClientContext.newInstance();
            WinRmTool tool = WinRmTool.Builder.builder(ip, username, password).port(DEFAULT_PORT).useHttps(false).context(context).build();
            tool.setOperationTimeout(5000L);
            WinRmToolResponse resp = tool.executeCommand(command);
            context.shutdown();
            return resp.getStdOut();
        }
    
        public static void main(String[] args) {
            WinRMHelper exec = new WinRMHelper("192.168.26.45", "tom", "passsword");
            String resp = exec.execute("hostname");
            System.out.println(resp);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    output

    DESKTOP-4QSDB37

  • 相关阅读:
    pytorch学习——循环神经网络RNN讲解及其实现
    博弈论——劳资博弈
    Excel周报制作
    如何在 WSL 下实现 NGINX 反向代理
    spring boot整合redis—邮箱验证码
    【OpenVI】AIGC纪元,兔年AI绘画实践
    力扣:递增子序列java
    适合您的智能手机的 7 款优秀手机数据恢复软件分享
    “微软爱写作”连词摘录
    DP0001A高压差分探头具有哪些具体性能?
  • 原文地址:https://blog.csdn.net/qq_40965479/article/details/126867665