Powershell 一键开启Winrm

winrm 前言

WinRM(Windows Remote Management)是微软基于 WS-Management 协议实现的Windows 远程管理标准,类比 Linux 系统的 SSH 协议,核心用于跨网络远程操作 Windows 主机(执行命令、部署软件、配置系统等),也是 Ansible/AWX 管理 Windows 主机的唯一官方通信方式

前提条件

  • 开启wmi,配置网卡,参考 

创建一键开启脚本

powershell-winrm.ps1
<# powershell winrm
+++++++++++++++++++++++++++++++++++++++++++++++++
+______                      _____  
<think> 我们被要求提供在Windows服务器上批量启用WinRM服务的方法。根据引用资料,我们可以总结出以下步骤: 1. 单机启用WinRM服务的基本命令是: winrm quickconfig 2. 配置认证方式和加密方式: - 启用基本认证(Basic authentication): winrm set winrm/config/service/auth @{Basic="true"} - 允许非加密通信(为了兼容性,但生产环境建议使用加密): winrm set winrm/config/service @{AllowUnencrypted="true"} 3. 但是,这些命令需要在每台服务器上执行,如何批量执行呢? 批量执行方法: 方法1:使用组策略(Group Policy)部署 - 在域控制器上创建组策略对象(GPO) - 在计算机配置->策略->管理模板->Windows组件->Windows远程管理(WinRM)中配置相关设置 - 包括启用WinRM服务、设置监听端口、认证方式等 - 将GPO链接到需要启用WinRM的OU(组织单位)上 方法2:使用PowerShell远程执行(需要已有远程执行通道,如PSRemoting已开启或使用其他远程管理工具) - 假设我们已经在所有目标服务器上启用了PSRemoting(或者使用域管理员账户可以通过WMI远程执行命令),那么我们可以使用Invoke-Command来批量执行。 示例脚本: ``` $servers = Get-Content -Path "C:\servers.txt" # 假设这个文件里是每行一个服务器名或IP $credential = Get-Credential # 输入管理员凭据 Invoke-Command -ComputerName $servers -Credential $credential -ScriptBlock { # 启用WinRM服务 winrm quickconfig -quiet # 使用-quiet参数避免交互确认 # 设置基本认证 winrm set winrm/config/service/auth '@{Basic="true"}' # 允许非加密通信(可选,根据安全要求调整) winrm set winrm/config/service '@{AllowUnencrypted="true"}' } ``` 方法3:使用Ansible等自动化工具 - 如果已经配置了WinRM,则可以使用Ansible的win_ping模块测试连接,然后使用win_command模块执行命令。但第一次配置时,通常需要先通过其他方式(如SSH或者使用Ansible的raw模块通过PSRemoting)来启用WinRM。 方法4:使用PSTools工具包中的psexec - 下载Sysinternals PSTools - 使用以下命令在每台服务器上执行: psexec \\remotecomputer -u domain\admin -p password winrm quickconfig -quiet psexec \\remotecomputer -u domain\admin -p password winrm set winrm/config/service/auth '@{Basic="true"}' psexec \\remotecomputer -u domain\admin -p password winrm set winrm/config/service '@{AllowUnencrypted="true"}' 注意事项: - 启用基本认证和允许非加密通信存在安全风险,建议在安全的内网环境中使用,或者使用HTTPS(WinRM over SSL)来加密通信。 - 若需要启用HTTPS,则需要为每台服务器配置证书。 根据引用[3]中的提示,在PowerShell中执行上述winrm set命令可能会报错,此时可以尝试在CMD中执行。因此,在批量脚本中,我们可以通过powershell调用cmd来执行,或者直接写在批处理文件中。 例如,在方法2的Invoke-Command中,我们可以这样写: ``` Invoke-Command -ComputerName $servers -Credential $credential -ScriptBlock { cmd.exe /c "winrm set winrm/config/service/auth @{Basic='true'}" cmd.exe /c "winrm set winrm/config/service @{AllowUnencrypted='true'}" } ``` 当然,更推荐使用PowerShell来配置,因为winrm命令实际上是配置WinRM的,PowerShell也有对应的配置命令(如Set-Item),但使用winrm命令更直接。 引用: [^1]: [WinRM官方文档](https://learn.microsoft.com/en-us/windows/win32/winrm/installation-and-configuration-for-windows-remote-management) [^2]: 引用中提到了基础配置步骤 [^3]: 引用中提到在CMD中执行设置命令可以避免报错 [^4]: 引用中介绍了WinRM的作用 相关问题: §§相关问题§§ 1. 如何为WinRM配置HTTPS以提高安全性? 2. 如何使用组策略批量配置WinRM? 3. 如何验证WinRM服务已经成功启用并可以远程连接? 4. WinRM服务启用失败的可能原因有哪些? </think> ### Windows服务器批量启用WinRM服务方法 #### 方法1:使用PowerShell远程执行(推荐) ```powershell # 服务器列表文件(每行一个IP或主机名) $servers = Get-Content -Path "C:\servers.txt" # 凭据设置(需管理员权限) $cred = Get-Credential -Message "输入管理员凭据" Invoke-Command -ComputerName $servers -Credential $cred -ScriptBlock { # 启用WinRM服务 winrm quickconfig -quiet -force # 配置基本认证 winrm set winrm/config/service/auth '@{Basic="true"}' # 允许非加密通信(测试环境) winrm set winrm/config/service '@{AllowUnencrypted="true"}' # 配置防火墙规则 netsh advfirewall firewall add rule name="WinRM-HTTP" dir=in action=allow protocol=TCP localport=5985 # 验证服务状态 Get-Service -Name WinRM | Select-Object Status, StartType } ``` > **说明**: > - `-quiet`参数避免交互提示 > - 首次执行需在每台服务器手动启动一次WinRM服务以生成配置文件[^3] #### 方法2:通过组策略部署(域环境) 1. **创建GPO策略**: ```powershell # 配置WinRM服务自动启动 Set-GPRegistryValue -Name "WinRM_Enable" -Key "HKLM\SYSTEM\CurrentControlSet\Services\WinRM" -ValueName "Start" -Type DWord -Value 2 # 启用基本认证 Set-GPRegistryValue -Name "WinRM_Enable" -Key "HKLM\SOFTWARE\Policies\Microsoft\Windows\WinRM\Service" -ValueName "AllowBasic" -Type DWord -Value 1 ``` 2. **配置监听端口**: ```powershell # 创建监听器 $command = 'winrm create winrm/config/Listener?Address=*+Transport=HTTP' Set-GPPrefRegistryValue -Name "WinRM_Enable" -Context Computer -Action Update -Key "HKLM\SOFTWARE\Policies\Microsoft\Windows\WinRM" -ValueName "AutoConfig" -Type String -Value $command ``` 3. **应用到目标OU**: ```powershell Get-GPO -Name "WinRM_Enable" | New-GPLink -Target "OU=Servers,DC=novice,DC=com" ``` #### 方法3:使用Ansible批量配置 ```yaml # winrm_enable.yml --- - name: 批量启用WinRM hosts: windows_servers tasks: - name: 启动WinRM服务 win_service: name: WinRM start_mode: auto state: started - name: 配置认证方式 win_shell: | winrm set winrm/config/service/auth '@{Basic="true"}' winrm set winrm/config/service '@{AllowUnencrypted="true"}' - name: 开放防火墙端口 win_firewall_rule: name: WinRM-HTTP localport: 5985 action: allow direction: in protocol: tcp state: present enabled: yes ``` 执行命令:`ansible-playbook -i hosts winrm_enable.yml` #### 故障排查要点 1. **权限问题**: - 需使用**本地管理员**或**域管理员**权限执行 - 检查UAC限制:`Set-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System -Name ConsentPromptBehaviorAdmin -Value 0` 2. **端口冲突**: ```powershell # 检查端口占用 Get-Process -Id (Get-NetTCPConnection -LocalPort 5985).OwningProcess ``` 3. **服务启动失败**: ```powershell # 查看事件日志 Get-WinEvent -LogName "Microsoft-Windows-WinRM/Operational" -MaxEvents 20 ``` > **安全建议**:生产环境应启用HTTPS(端口5986)并配置证书加密传输[^1] ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CIAS

你的鼓励就是我的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值