

选择专用网络

点击确定退出
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FRNIiNtS-1663211586894)(C:\Users\SERVER\OneDrive\桌面\work\img\image-20220915102922345.png)]](https://1000bd.com/contentImg/2023/11/07/022727201.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1GwY7fcj-1663211586894)(C:\Users\SERVER\OneDrive\桌面\work\img\image-20220915102939857.png)]](https://1000bd.com/contentImg/2023/11/07/022727296.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NUkga23g-1663211586894)(C:\Users\SERVER\OneDrive\桌面\work\img\image-20220915102956544.png)]](https://1000bd.com/contentImg/2023/11/07/022727186.png)
一般使用专用网络,不建议使用公用网络

官方文档
使用管理员权限进入 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"}'
windows + R 输入 services.msc
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YmwKVTGY-1663211586895)(C:\Users\SERVER\OneDrive\桌面\work\img\image-20220915104200620.png)]](https://1000bd.com/contentImg/2023/11/07/022727196.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HsggekB2-1663211586895)(C:\Users\SERVER\OneDrive\桌面\work\img\image-20220915104254282.png)]](https://1000bd.com/contentImg/2023/11/07/022727216.png)
鼠标右键重新启动
pom.xml
<dependency>
<groupId>io.cloudsoft.windows</groupId>
<artifactId>winrm4j</artifactId>
<version>0.12.3</version>
</dependency>
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);
}
}
output
DESKTOP-4QSDB37