中文

Windows Server

通过 HTTPS 使用 PowerShell Remoting 管理 Windows 服务器

在 Windows Server 上通过 HTTPS 配置 PowerShell Remoting:证书、5986 端口上的 WinRM 侦听器、仅限您的 IP 地址的防火墙以及安全连接。

阅读约 7 分钟, 更新于 2026年11月18日

本页内容

您的参数

只需填写一次,指南中的命令会自动更新。

PowerShell Remoting 允许您从自己的工作站在服务器上执行命令,而无需打开图形会话。本指南将在 5986 端口上建立 HTTPS 加密访问,由客户端验证证书,并通过防火墙只接受您的 IP 地址。

工作原理

PowerShell Remoting 基于 WinRM 服务,该服务在 Windows Server 上默认启用。

端口 传输方式 本指南中的用途
5985 HTTP 仍可在本地使用,但对网络关闭
5986 HTTPS 唯一的远程访问方式,仅限您的 IP 地址

从不在域中的工作站连接时,身份验证使用 NTLM,而 NTLM 无法证明服务器的身份。HTTPS 证书弥补了这一缺陷:您的工作站在发送任何内容之前,会先验证它连接的确实是您的服务器。

前提条件

  • 一台运行 Windows Server 2019、2022 或 2025 的服务器,可使用管理员账户通过 RDP 访问。
  • 一台运行 Windows 且拥有管理员权限的管理工作站。Linux 和 macOS 上的 PowerShell 不支持 WinRM 传输。
  • 该工作站的公网 IP 地址,最好是固定地址。
  • 服务器的主机名,例如 srv.您的域名.com。

选择主机名

连接时使用的名称必须与证书中的名称一致。有两种方式:

  • 使用域名:创建一条 A 类型的 DNS 记录,将 srv.您的域名.com 指向您服务器的地址。
  • 不使用 DNS 记录:自行选择一个名称,并按照步骤 6中的说明,在工作站的 hosts 文件中声明它。

1. 检查服务器上的 WinRM

在服务器上以管理员身份运行 PowerShell:

PowerShell
Get-Service WinRM | Format-Table Name, Status, StartType
Test-WSMan

服务应处于 Running 状态,且 Test-WSMan 应返回有关协议的信息。否则,请重新启用该服务:

PowerShell
Enable-PSRemoting -Force

2. 创建证书

方案 A:自签名证书

此方案适合只有一名管理员的情况,因为您要自行在工作站上安装证书。此处证书有效期为两年,其私钥不可导出。

PowerShell
$hostName = 'srv.您的域名.com'
$cert = New-SelfSignedCertificate -DnsName $hostName -CertStoreLocation 'Cert:\LocalMachine\My' -KeyExportPolicy NonExportable -NotAfter (Get-Date).AddYears(2)
$cert | Format-List Subject, Thumbprint, NotAfter

New-Item -ItemType Directory -Path 'C:\WinRM' -Force | Out-Null
Export-Certificate -Cert $cert -FilePath 'C:\WinRM\winrm-https.cer'

.cer 文件只包含证书的公共部分。请记下指纹 (Thumbprint):稍后需要在工作站上进行比对。

方案 B:受认可的证书颁发机构签发的证书

如果您已有该名称的 PFX 格式证书,请将其导入。其 CN 字段必须与主机名一致,并且必须允许服务器身份验证。

PowerShell
$pfxPassword = Read-Host 'PFX password' -AsSecureString
Import-PfxCertificate -FilePath 'C:\WinRM\certificate.pfx' -CertStoreLocation 'Cert:\LocalMachine\My' -Password $pfxPassword

随后请从服务器上删除 PFX 文件。使用此方案时,您无需在工作站上导入证书。

3. 创建 HTTPS 侦听器

PowerShell
$hostName = 'srv.您的域名.com'
$cert = Get-ChildItem Cert:\LocalMachine\My |
    Where-Object { $_.Subject -like "CN=$hostName*" } |
    Sort-Object NotAfter | Select-Object -Last 1

New-WSManInstance -ResourceURI winrm/config/Listener `
    -SelectorSet @{ Address = '*'; Transport = 'HTTPS' } `
    -ValueSet @{ Hostname = $hostName; CertificateThumbprint = $cert.Thumbprint }

检查结果:

PowerShell
Get-WSManInstance -ResourceURI winrm/config/Listener -Enumerate |
    Format-Table Transport, Port, Hostname, Enabled, CertificateThumbprint
Get-NetTCPConnection -LocalPort 5986 -State Listen

请勿对自签名证书使用 winrm quickconfig -transport:https:该命令会拒绝自签名证书。

4. 仅对您的 IP 地址开放 5986 端口

在您的 RDP 会话中,此命令会显示服务器所看到的公网地址(如果您使用其他 RDP 端口,请相应调整 3389):

PowerShell
(Get-NetTCPConnection -LocalPort 3389 -State Established).RemoteAddress

如果该地址确实是您管理工作站的地址,请创建规则:

PowerShell
New-NetFirewallRule -Name 'WinRM-HTTPS-Admin' -DisplayName 'WinRM HTTPS - admin workstation' `
    -Direction Inbound -Action Allow -Protocol TCP -LocalPort 5986 -RemoteAddress 您的IP -Profile Any

使用图形界面

  1. 按照 Windows 防火墙指南中的说明,为 TCP 5986 端口创建一条入站规则,操作选择允许连接 (Allow the connection),并应用于所有配置文件。
  2. 打开该规则的属性,然后切换到作用域 (Scope) 选项卡。
  3. 在远程 IP 地址 (Remote IP address) 下,选择下列 IP 地址 (These IP addresses),然后添加您的地址。

如果您的套餐提供可从控制台配置的防火墙,请同样在其中为您的地址放行此端口。

5. 关闭 HTTP 远程访问

Windows 默认允许本地网络访问 5985 端口,而公网地址可能与其他服务器共用同一子网。请禁用这些规则。HTTP 侦听器会保留,供本地工具使用。

PowerShell
Get-NetFirewallRule -Name 'WINRM-HTTP-In-TCP*' | Disable-NetFirewallRule
Get-NetFirewallRule -Name 'WINRM-HTTP-In-TCP*' | Format-Table Name, Enabled, Profile

如果您之后再次运行 Enable-PSRemoting,这些规则会被重新启用:届时请重新执行此步骤。

6. 准备管理工作站

在您的工作站上以管理员身份运行 PowerShell。

如果该名称在公共 DNS 中不存在,请在本地声明它:

PowerShell
Add-Content -Path "$env:SystemRoot\System32\drivers\etc\hosts" -Value '您的服务器IP srv.您的域名.com'

如果使用自签名证书,请将 C:\WinRM\winrm-https.cer 复制到您的工作站(例如在 RDP 会话中通过复制粘贴),然后在其所在文件夹中导入:

PowerShell
Import-Certificate -FilePath '.\winrm-https.cer' -CertStoreLocation 'Cert:\LocalMachine\Root'

7. 连接

账户名的格式为 服务器名称\您的用户名。命令 hostname 可以显示服务器名称。在域控制器上,请使用 域名\您的用户名。

PowerShell
$cred = Get-Credential -UserName '服务器名称\您的用户名' -Message 'Server administrator account'
Enter-PSSession -ComputerName srv.您的域名.com -UseSSL -Credential $cred

此时提示符会显示服务器名称。请测试后退出:

PowerShell
hostname
whoami
Exit-PSSession

如需在没有交互式会话的情况下运行命令,或者重用连接并复制文件:

PowerShell
Invoke-Command -ComputerName srv.您的域名.com -UseSSL -Credential $cred -ScriptBlock { Get-Service WinRM }

$session = New-PSSession -ComputerName srv.您的域名.com -UseSSL -Credential $cred
Copy-Item -Path '.\script.ps1' -Destination 'C:\WinRM\' -ToSession $session
Remove-PSSession $session

验证配置

在服务器上,检查防火墙规则以及是否不存在未加密的传输。最后两个参数的值必须为 false:

PowerShell
Get-NetFirewallRule -Name 'WinRM-HTTPS-Admin' | Get-NetFirewallAddressFilter | Format-Table RemoteAddress
Get-NetFirewallPortFilter -Protocol TCP |
    Where-Object { $_.LocalPort -contains '5986' } |
    Get-NetFirewallRule | Format-Table Name, Enabled, Action, Profile
Get-Item WSMan:\localhost\Service\AllowUnencrypted
Get-Item WSMan:\localhost\Service\Auth\Basic

应当只有一条处于启用状态的规则允许 5986 端口。在您的工作站上,HTTPS 端口应有响应,HTTP 端口则不应有响应:

PowerShell
Test-NetConnection srv.您的域名.com -Port 5986 -WarningAction SilentlyContinue | Select-Object TcpTestSucceeded
Test-NetConnection srv.您的域名.com -Port 5985 -WarningAction SilentlyContinue | Select-Object TcpTestSucceeded
Test-WSMan -ComputerName srv.您的域名.com -UseSSL

第一项测试应显示 True,第二项应显示 False。最后,请在另一个互联网连接下(例如手机热点共享的网络),确认 5986 端口同样显示 False。

更新配置

您的 IP 地址已更改

请通过 RDP 连接到服务器,并替换允许的地址:

PowerShell
Set-NetFirewallRule -Name 'WinRM-HTTPS-Admin' -RemoteAddress 您的IP

更新证书

请在证书到期之前,按照步骤 2创建新证书,然后将其与侦听器关联:

PowerShell
$hostName = 'srv.您的域名.com'
$cert = Get-ChildItem Cert:\LocalMachine\My |
    Where-Object { $_.Subject -like "CN=$hostName*" } |
    Sort-Object NotAfter | Select-Object -Last 1

Set-WSManInstance -ResourceURI winrm/config/Listener `
    -SelectorSet @{ Address = '*'; Transport = 'HTTPS' } `
    -ValueSet @{ CertificateThumbprint = $cert.Thumbprint }

随后在您的工作站上导入新的 .cer 文件,测试连接,再在两端删除旧证书。

全部移除

PowerShell
Remove-WSManInstance -ResourceURI winrm/config/Listener -SelectorSet @{ Address = '*'; Transport = 'HTTPS' }
Remove-NetFirewallRule -Name 'WinRM-HTTPS-Admin'

故障排除

端口没有响应

文本
WinRM cannot complete the operation. Verify that the specified computer name is valid, that the computer is accessible over the network, and that a firewall exception for the WinRM service is enabled.

请按以下顺序检查:

  1. 您当前的公网地址确实就是规则 WinRM-HTTPS-Admin 中的地址。
  2. 侦听器存在,并且服务器上的 5986 端口处于侦听状态。
  3. 主机名指向正确的地址:Resolve-DnsName srv.您的域名.com。
  4. 如果您使用套餐控制台中可配置的防火墙,请确认它已放行此端口。

未知的证书颁发机构

文本
The SSL certificate is signed by an unknown certificate authority.

您的工作站不信任该证书。请在管理员 PowerShell 中将 .cer 文件导入 Cert:\LocalMachine\Root,并确认它确实是侦听器所使用的证书。

名称与证书不匹配

文本
The SSL certificate contains a common name (CN) that does not match the hostname.

请使用证书中的确切名称进行连接,而不要使用 IP 地址或其他别名。

证书已过期或无法检查吊销状态

文本
The SSL certificate is expired.
The SSL certificate could not be checked for revocation.

请更新已过期的证书。对于第二条消息,请确保您的工作站能够访问证书颁发机构的吊销服务器。

客户端要求使用 HTTPS 或 TrustedHosts

文本
The WinRM client cannot process the request. If the authentication scheme is different from Kerberos, or if the client computer is not joined to a domain, then HTTPS transport must be used or the destination machine must be added to the TrustedHosts configuration setting.

您的命令缺少 -UseSSL 参数。请勿将服务器添加到 TrustedHosts:这只会掩盖未验证其身份这一事实。

访问被拒绝

文本
Access is denied.

请检查服务器名称、账户和密码。默认情况下,只有管理员 (Administrators) 组的成员才能连接。

内置账户以外的本地管理员账户也可能因 UAC 的远程限制而被拒绝。请优先使用内置的管理员账户或域账户。注册表值 LocalAccountTokenFilterPolicy 可以解除此限制,但它对该机器上的所有本地账户都生效:请在充分了解后果的前提下才启用它。

创建侦听器失败

文本
Cannot create a WinRM listener on HTTPS because this machine does not have an appropriate certificate.

此消息尤其会在使用 winrm quickconfig -transport:https 时出现:请按照步骤 3使用 New-WSManInstance。同时请检查证书是否已过期,以及其 CN 是否与 Hostname 完全一致。

如果 HTTPS 侦听器已存在,请使用 Remove-WSManInstance 将其删除,然后重新创建。

在会话中访问另一台机器时被拒绝

您的密码不会传递给远程服务器,因此该服务器无法代表您向网络共享或其他服务器进行身份验证。这就是所谓的“第二跳”问题。请直接在目标机器上运行该命令,或者为相关命令传递显式凭据。

Microsoft 文档