PowerShell 配置winrm使用HTTPS 11


之前写过一篇文章:PowerShell 远程管理Windows Azure 上面的虚拟机 。其中提到了配置WinRM使用HTTPS连接时一笔带过:

winrm quickconfig -transport:https

问题描述和分析

但是今天@zhgj007留言说他执行这一步骤时,遇到了错误:

WinRM service is already running on this machine.
 WSManFault
 Message
 ProviderFault
 WSManFault
 Message = Cannot create a WinRM listener on HTTPS because this machine does not have an appropriate cert
 ificate. To be used for SSL, a certificate must have a CN matching the hostname, be appropriate for Server Authenticatio
 n, and not be expired, revoked, or self-signed.
 Error number: -2144108267 0×80338115
 Cannot create a WinRM listener on HTTPS because this machine does not have an appropriate certificate. To be used for SS
 L, a certificate must have a CN matching the hostname, be appropriate for Server Authentication, and not be expired, rev
 oked, or self-signed.

经过分析,既然使用HTTPS连接,必然需要一个ssl自签名证书。

ssl自签名证书

这里强调一下,证书的名称Subject必须为hostName。具体到域环境中,比如机器test加入了域pstips.net,那这个HostName应当为test.pstips.net。具体到Azure环境中,应当就是host service name 域名,比如pstips.cloudapp.net。

如何生成一个ssl自签名整数

一般有三种途径:

  • 使用第三方工具
  • 使用visual studio tool
  • 使用IIS

因为这不是本文的重点,就不予演示了。我使用的第三种,可以参考在 IIS 7 中创建自签名的服务器证书

证书生成完毕后,会自动保存在Local Computer->Personal->Certificates下面,这个位置刚好。

使用PowerShell 查看证书的信息

PS> ls Cert:\LocalMachine\My\

    Directory: Microsoft.PowerShell.Security\Certificate::LocalMachine\My

Thumbprint                                Subject
----------                                -------
F9CF1301BA9C2486EDEE0886FF5294555E44084B  CN=test.pstips.net

配置winrm使用HTTPS

使用下面这条命令:

winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Port="5985" ;Hostname="test.pstips.net" ;CertificateThumbprint="F9 CF 13 01 BA 9C 24 86 ED EE 08 86 FF 52 94 55 5E 44 08 4B"}

注意因为命令中包含了@和一些花括号。PowerShell会有参数解析错误,请切换至cmd.exe运行即可。

你运行时,可能还会碰到这个错误

Message = The function: "HttpSetServiceConfiguration" failed unexpectedly. Error=183.

WSManFault
    Message
        ProviderFault
            WSManFault
                Message = The function: "HttpSetServiceConfiguration" failed unexpectedly. Error=183.

Error number:  -2147024713 0x800700B7
Cannot create a file when that file already exists.

因为5985端口默认配置了HTTP连接,所以换个端口即可,大家一般用5986

C:> winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Port="5986" ;Hostname="test.pstips.net" ;CertificateThumbprint="F9 CF 13 01 BA 9C 24 86 ED EE 08 86 FF 52 94 55 5E 44 08 4B"}
ResourceCreated
    Address = http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
    ReferenceParameters
        ResourceURI = http://schemas.microsoft.com/wbem/wsman/1/config/listener
        SelectorSet
            Selector: Address = *, Transport = HTTPS

已经配置成功,我们再来检测一下:

PS> winrm e winrm/config/listener
Listener
    Address = *
    Transport = HTTP
    Port = 5985
    Hostname
    Enabled = true
    URLPrefix = wsman
    CertificateThumbprint
    ListeningOn = 10.218.188.30, 127.0.0.1, 169.254.155.205, ::1, 2001:4898:e0:1019:9469:d446:eb35:4b22, fe80::5efe:10.2
18.188.30%13, fe80::30fc:3bed:e9de:9bcd%14, fe80::9469:d446:eb35:4b22%12

Listener
    Address = *
    Transport = HTTPS
    Port = 5986
    Hostname = test.pstips.net
    Enabled = true
    URLPrefix = wsman
    CertificateThumbprint = F9 CF 13 01 BA 9C 24 86 ED EE 08 86 FF 52 94 55 5E 44 08 4B
    ListeningOn = 10.218.188.30, 127.0.0.1, 169.254.155.205, ::1, 2001:4898:e0:1019:9469:d446:eb35:4b22, fe80::5efe:10.2
18.188.30%13, fe80::30fc:3bed:e9de:9bcd%14, fe80::9469:d446:eb35:4b22%12
本文链接: https://www.pstips.net/configure-winrm-under-https-transport.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

回复 荔非苔 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

11 条评论 “PowerShell 配置winrm使用HTTPS

  • zhgj007

    多谢发帖回复。
    又根据提示尝试了一下,从PS进入cmd执行,可又发生如下错误:

    C:\Windows\system32>winrm create winrm/config/Listener?Address=*+Transport=HTTPS@{Port="5986";Hostname="p**.cloudapp.net
    ";CertificateThumbprint="96 e5 3a c9 f6 df ce e5 2f c9 ea 49 f3 f9 9f 24 2b 68 5d ae"}
    WSManFault
        Message
            ProviderFault
                WSManFault
                    Message = The WS-Management service cannot process the request. The value for the selector Transport is
    invalid.
    
    Error number:  -2144108451 0x8033805D
    The WS-Management service cannot process the request because a value for the selector is invalid.
    

    这个Azure VM是作为HPC headnode部署的,上面没设专门的WebService,
    用MMC查看了本地机器上的授权文件是有效的,奇怪的是winrm qc -transport:https一直说找不到有效授权,
    winrm create winrm/config/Listener?Address=*+Transport=HTTPS@应该是可行的,不知为何还是报错。

      • zhgj007

        站长高手,在HTTPS和@间加空格果然解决了selector valid问题,非常感谢!
        我的部署仍然回到了ca不匹配错误,自己再钻研一下。

        • Mooser Lee 文章作者

          在你能够100%确保证书的的CN名称没问题,比如:
          8B72DABA0BCEE9FA616D224F0760E012844D70E4 CN=youhost.cloudapp.net,后
          实在不行顺便把机器重命名一下,命名成youhost,试试。

    • Pale_Cheung

      在已经存在至少一张有效证书的情况下,直接执行“winrm quickconfig -transport:https”是没有问题的。
      他会在控制台启动一个向导,只要回答是否后就顺利了新增了https监听器。

      我整套环境已经有PKI了,也早有证书用户各种通信。
      下面是我的步骤,非常简单轻松,最后新增防火墙例外,外部顺利链接。

      C:\Users\Administrator.ZPALE>winrm qc
      在此计算机上,WinRM 已设置为接收请求。
      WinRM 已经进行了设置,以用于在此计算机上进行远程管理。

      C:\Users\Administrator.ZPALE>winrm e winrm/config/listener
      Listener
      Address = *
      Transport = HTTP
      Port = 5985
      Hostname
      Enabled = true
      URLPrefix = wsman
      CertificateThumbprint
      ListeningOn = 127.0.0.1, 192.168.0.101, ::1, fe80::5efe:192.168.0.101%15, fe80::ffff:ffff:fffe%12, fe80::31dc:f16:2cad:bcc6%14

      Listener [Source="Compatibility"]
      Address = *
      Transport = HTTP
      Port = 80
      Hostname
      Enabled = true
      URLPrefix = wsman
      CertificateThumbprint
      ListeningOn = 127.0.0.1, 192.168.0.101, ::1, fe80::5efe:192.168.0.101%15, fe80::ffff:ffff:fffe%12, fe80::31dc:f16:2cad:bcc6%14

      C:\Users\Administrator.ZPALE>winrm quickconfig -transport:https
      在此计算机上,WinRM 已设置为接收请求。
      WinRM 没有设置成为了管理此计算机而允许对其进行远程访问。
      必须进行以下更改:

      在 HTTPS://* 上创建 WinRM 侦听程序接受 WS-Man 对此机器上任意 IP 的请求。
      为服务配置 CertificateThumbprint 设置,该设置将用于 CredSSP 身份验证。

      进行这些更改吗[y/n]? y

      WinRM 已经进行了更新,以用于远程管理。

      在 HTTPS://* 上创建 WinRM 侦听程序接受 WS-Man 对此机器上任意 IP 的请求。
      已为该服务配置 CertificateThumbprint 设置。

      C:\Users\Administrator.ZPALE>winrm e winrm/config/listener
      Listener
      Address = *
      Transport = HTTP
      Port = 5985
      Hostname
      Enabled = true
      URLPrefix = wsman
      CertificateThumbprint
      ListeningOn = 127.0.0.1, 192.168.0.101, ::1, fe80::5efe:192.168.0.101%15, fe80::ffff:ffff:fffe%12, fe80::31dc:f16:2cad:bcc6%14

      Listener
      Address = *
      Transport = HTTPS
      Port = 5986
      Hostname = HV0.ZPALE.COM
      Enabled = true
      URLPrefix = wsman
      CertificateThumbprint = fc 80 99 3b 4a 0a 6a c8 2e 44 06 8e bd b4 1f 69d0 2d f3 78
      ListeningOn = 127.0.0.1, 192.168.0.101, ::1, fe80::5efe:192.168.0.101%15, fe80::ffff:ffff:fffe%12, fe80::31dc:f16:2cad:bcc6%14

      Listener [Source="Compatibility"]
      Address = *
      Transport = HTTP
      Port = 80
      Hostname
      Enabled = true
      URLPrefix = wsman
      CertificateThumbprint
      ListeningOn = 127.0.0.1, 192.168.0.101, ::1, fe80::5efe:192.168.0.101%15, fe80::ffff:ffff:fffe%12, fe80::31dc:f16:2cad:bcc6%14

    • wkl17

      不太理解,这配置好之后,就能用https协议(浏览器?)来(WinRM)远程管理服务器?
      另外,网站左下角那个统计需要注册吗?官网好像没看到注册按钮..统计数据完全公开,不能控制?
      谢谢.

      • Mooser Lee 文章作者

        是的配置好以后就可以了,所有的数据都走的是https了。
        那个不需要注册,直接填写上自己的网站地址就可以。统计数据的确是完全公开的,建议使用百度统计或者cnzz。