Skip to content
阿德的博客
Go back

DSC使用https的winrm

时至二零一八农历戊戌(狗)年春节,奚有米先来拜个年啦!

新年长假后会有一个拖延了很久的任务须要完成,于是着手研究怎么使用DSC,其中包含了winrm的配置,这篇博客简单记录下相关经验:

winrm默认配置(http 5985)

目前winrm在windows2012r2之后版本默认开启了http listener 5985端口,可以通过下面命令测试winrm连接,须要注意:

PS C:\Users\xiyoumi> Enter-PSSession -ComputerName server001.lab.com
[server001.lab.com]: PS C:\Users\xiyoumi\Documents> winrm e winrm/config/listener
Listener
    Address = *
    Transport = HTTP
    Port = 5985
    Hostname
    Enabled = true
    URLPrefix = wsman
    CertificateThumbprint
    ListeningOn = 192.168.32.12, 127.0.0.1, ::1, fe80::5efe:10.196.32.74%15, fe80::8de8:50db:9107:696a%13

[server001.lab.com]: PS C:\Users\xiyoumi\Documents> hostname
server001

[server001.lab.com]: PS C:\Users\xiyoumi\Documents> (Get-WmiObject -class Win32_OperatingSystem).Caption
Microsoft Windows Server 2012 R2 Standard

上面例子连接到server001.lab.com,并使用winrm命令查看listener配置。由于这是一台windows 2012 R2的服务器,所以winrm已经默认开启。
下面命令从头开启一台服务器的winrm默认配置:

PS C:\Users\xiyoumi> winrm e winrm/config/listener
PS C:\Users\xiyoumi> Enable-PSRemoting

WinRM Quick Configuration
Running command "Set-WSManQuickConfig" to enable remote management of this computer by using the Windows Remote
Management (WinRM) service.
 This includes:
    1. Starting or restarting (if already started) the WinRM service
    2. Setting the WinRM service startup type to Automatic
    3. Creating a listener to accept requests on any IP address
    4. Enabling Windows Firewall inbound rule exceptions for WS-Management traffic (for http only).

Do you want to continue?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):
WinRM is already set up to receive requests on this computer.
WinRM has been updated for remote management.
Created a WinRM listener on HTTP://* to accept WS-Man requests to any IP on this machine.

PS C:\Users\xiyoumi> winrm e winrm/config/listener
Listener
    Address = *
    Transport = HTTP
    Port = 5985
    Hostname
    Enabled = true
    URLPrefix = wsman
    CertificateThumbprint
    ListeningOn = 10.196.32.110, 127.0.0.1, ::1, fe80::100:7f:fffe%13

总的来说比较简单,一条命令解决,如果是没有加入域的机器,可以通过加-SkipNetworkProfileCheck参数,开启public zone的防火墙。
当然也可以不用这个cmdlet,直接使用winrm命令:

C:\> winrm quickconfig
WinRM is not set up to receive requests on this machine.
The following changes must be made:
 
Set the WinRM service type to delayed auto start.
 
Make these changes [y/n]? y
 
WinRM has been updated to receive requests.
 
WinRM service type changed successfully.
WinRM is not set up to allow remote access to this machine for management.
The following changes must be made:
 
Create a WinRM listener on HTTP://* to accept WS-Man requests to any IP on this machine.
Enable the WinRM firewall exception.
Configure LocalAccountTokenFilterPolicy to grant administrative rights remotely to local users.
 
Make these changes [y/n]? y
 
WinRM has been updated for remote management.
 
Created a WinRM listener on HTTP://* to accept WS-Man requests to any IP on this machine.
WinRM firewall exception enabled.
Configured LocalAccountTokenFilterPolicy to grant administrative rights remotely to local users.

另外,关闭winrm可以使用下面步骤:

上述就是一般情况的winrm使用方法,下面继续配置https的方法


winrm配置https

首先,可以先通过默认的winrm配置测试连接已经没有问题。和http相似,https方式大致也包括下面几个配置:

在具体开始配置之前,这里推荐一下Ansible使用的winrm配置脚本,可以直接github下载,或者从ansible的windows文档提供的链接下载examples/scripts/ConfigureRemotingForAnsible.ps1

这个脚本直接可以生成自签名证书,然后开启服务和防火墙,并完成listener配置。如果实在想要自己完成这些操作的话,通过下面这些命令实现:

完成上面几部后,https的winrm就已经完成配置了,可选的我们可以删除原有的http listener:

winrm delete winrm/config/Listener?Address=*+Transport=HTTP

须要测试https连接可以使用下面命令:

Enter-PSSession -ComputerName HP840G1 -Port 5986 -SessionOption (New-PSSessionOption -SkipCACheck) -UseSSL

狗年旺起来!_(੭_ˊᵕˋ)੭*ଘ


Share this post on:

Previous Post
使用DSCEA和BaselineManagement做合规检查
Next Post
PowerShell 学习笔记