Windows 7 开启 WinRM 远程控制完整教程
手把手教你让 Win7 支持远程 PowerShell/WinRM 管理,告别 IPC$ 共享失败
背景
在实际工作中,我遇到一个场景:展厅里有一台 Windows 7 系统的拼接屏控制主机,需要通过我的 Windows 11 电脑远程关机。
传统的远程关机方式是用 net use 建立 IPC$ 共享连接,然后执行 shutdown /s /f /m \\\\IP。但这台 Win7 机器一直报错:
发生系统错误 67。
找不到网络名。
尝试了 WMI(wmic)、计划任务(schtasks)等方式也都失败。最终通过 WinRM(Windows Remote Management) 成功实现了远程关机,秒级响应,稳定可靠。
下面是完整的配置过程。
环境说明
| 项目 | 值 |
|---|---|
| 被控端 | Windows 7 SP1 |
| 控制端 | Windows 10/11 |
| 远程用户 | remoteadmin(新建管理员用户) |
| WinRM 端口 | 5985(HTTP) |
第一步:被控端(Win7)配置
所有命令均在 Win7 上以管理员身份打开 PowerShell 执行。
1.1 启用 WinRM 服务
winrm quickconfig
系统会提示需要更改:
– 将 WinRM 服务类型设置为延迟自动启动
– 启动 WinRM 服务
– 配置 LocalAccountTokenFilterPolicy
输入 y 确认。你可能会看到防火墙报错,没关系:
WSManFault
由于此计算机上的网络连接类型之一设置为公用,因此 WinRM 防火墙例外将不运行。
继续往下走即可,后面会手动配置防火墙。
1.2 手动添加防火墙规则(绕过公用网络限制)
Win7 的 PowerShell 2.0 没有 Set-NetFirewallRule cmdlet,用 netsh 代替:
netsh advfirewall firewall add rule name="WinRM-HTTP" dir=in protocol=tcp localport=5985 action=allow
1.3 创建 WinRM 监听器(关键步骤)
winrm quickconfig 默认可能没有创建监听器。先检查一下:
winrm enumerate winrm/config/listener
如果没有任何输出,说明监听器未创建,需要手动创建:
winrm create winrm/config/Listener?Address=*+Transport=HTTP
成功后会返回类似:
ResourceCreated
Address = http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
1.4 配置允许 Basic 认证
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\Service\auth /v Basic /t REG_DWORD /d 1 /f
1.5 配置允许未加密通信
两端都需要配置!
被控端(Win7):
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\Service /v allow_unencrypted /t REG_DWORD /d 1 /f
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\Client /v allow_unencrypted /t REG_DWORD /d 1 /f
控制端(Win10/11):
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\Client /v allow_unencrypted /t REG_DWORD /d 1 /f
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\Service /v allow_unencrypted /t REG_DWORD /d 1 /f
1.6 配置 TrustedHosts(信任列表)
两端都需要配置!
被控端(Win7): 将控制端的 IP 加入信任列表
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\Client /v trusted_hosts /t REG_SZ /d "192.168.1.x" /f
控制端(Win10/11): 将被控端的 IP 加入信任列表
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\Client /v trusted_hosts /t REG_SZ /d "192.168.1.x" /f
将
192.168.1.x替换为对方的实际 IP 地址。如果有多个 IP,用逗号分隔(如"192.168.1.10,192.168.1.11"),使用通配符"*"则信任所有主机(仅测试环境建议)。
1.7 确认 LocalAccountTokenFilterPolicy
确保该注册表值为 1,否则非内置 Administrator 用户会被拒绝远程登录:
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 1 /f
1.8 重启 WinRM 服务
net stop winrm
net start winrm
1.9 验证 WinRM 正在监听
netstat -ano | findstr 5985
正常输出:
TCP 0.0.0.0:5985 0.0.0.0:0 LISTENING 4
TCP [::]:5985 [::]:0 LISTENING 4
第二步:创建远程管理用户
WinRM 要求登录用户必须属于 Administrators 组。如果使用现有用户,先确认:
net localgroup administrators
如果该用户不在管理员组中:
net localgroup administrators <用户名> /add
建议新建一个专用管理用户(避免影响现有账号):
net user remoteadmin <你的密码> /add
net localgroup administrators remoteadmin /add
第三步:控制端测试连接
3.1 使用 Python + pywinrm(推荐)
安装 pywinrm:
pip install pywinrm
测试连接:
import winrm
s = winrm.Session('192.168.1.x', auth=('remoteadmin', '<密码>'))
r = s.run_cmd('hostname', [''])
print(r.std_out.decode('gbk', errors='replace'))
如果输出目标机器的主机名,说明连接成功!
远程关机:
import winrm
s = winrm.Session('192.168.1.x', auth=('remoteadmin', '<密码>'))
r = s.run_cmd('shutdown', ['/s', '/f', '/t', '0'])
print('关机成功' if r.status_code == 0 else f'失败: {r.status_code}')
3.2 使用 PowerShell
# 设置信任列表(需管理员权限)
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "192.168.1.x" -Force
# 测试连接
Test-WSMan -ComputerName 192.168.1.x -Authentication Default
# 远程关机
Stop-Computer -ComputerName 192.168.1.x -Credential remoteadmin
踩坑记录
坑 1:IPC$ 连接失败,错误 67 “找不到网络名”
原因: 目标机器(Win7)的 Server 服务或文件共享功能被禁用,无法建立 IPC$ 连接。
解决: 放弃 IPC$,改用 WinRM。
坑 2:winrm 命令在 Win7 上报语法错误
Win7 自带的 winrm 版本不支持 @{Key="Value"} 这种 PowerShell 语法。Win7 上的正确用法:
# ✅ 正确
winrm set winrm/config/Listener?Address=*+Transport=HTTP ...
# ❌ 错误——Win7 不支持
Set-Item WSMan:\localhost\Listener\...
如果 winrm set 仍然报错,直接通过注册表修改对应键值。
坑 3:认证被拒绝 “the specified credentials were rejected”
可能原因及排查顺序:
- 用户不在 Administrators 组 → 检查
net localgroup administrators LocalAccountTokenFilterPolicy未设置或为 0 → 设置为 1- Basic 认证未开启 → 检查注册表
WSMAN\Service\auth\Basic
坑 4:客户端报 “当前在客户端配置中禁用未加密通讯”
原因: 控制端的 allow_unencrypted 也没开。
解决: 控制端也要执行 reg add 设置 allow_unencrypted = 1(见 1.5 节)。
坑 5:PowerShell 没有管理员权限
现象: Win10/11 上运行 Set-Item WSMan:\localhost\Client\TrustedHosts 报”拒绝访问”。
原因: PowerShell 未以管理员身份运行。
解决: 改用注册表方式添加 TrustedHosts,或者右键 PowerShell → 以管理员身份运行。
完整配置清单速查
被控端(Win7)完整命令
# 1. 启用 WinRM
winrm quickconfig
# 2. 防火墙规则
netsh advfirewall firewall add rule name="WinRM-HTTP" dir=in protocol=tcp localport=5985 action=allow
# 3. 创建监听器
winrm create winrm/config/Listener?Address=*+Transport=HTTP
# 4. 注册表配置
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\Service\auth /v Basic /t REG_DWORD /d 1 /f
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\Service /v allow_unencrypted /t REG_DWORD /d 1 /f
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\Client /v allow_unencrypted /t REG_DWORD /d 1 /f
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\Client /v trusted_hosts /t REG_SZ /d "192.168.1.x" /f
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 1 /f
# 5. 创建管理员用户
net user remoteadmin <密码> /add
net localgroup administrators remoteadmin /add
# 6. 重启服务
net stop winrm && net start winrm
控制端(Win10/11)完整命令
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\Client /v allow_unencrypted /t REG_DWORD /d 1 /f
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\Service /v allow_unencrypted /t REG_DWORD /d 1 /f
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\Client /v trusted_hosts /t REG_SZ /d "192.168.1.x" /f
总结
Win7 的 WinRM 配置虽然比 Win10/11 麻烦一些——命令版本老、PowerShell 2.0 限制多——但通过注册表直改 + 手动创建监听器的方式,完全可以跑通。一旦配置好,远程管理就非常丝滑了。
这套方案特别适合管理那些不方便随时去现场按电源的机房服务器、展厅演示机、工控机等场景。相比传统的 IPC$ 方式,WinRM 对网络环境要求更低,配置完成后稳定可靠,配合 Python 脚本还能实现批量管理。
如果你有其他 Win7 远程管理的踩坑经验,欢迎补充交流 🙌