Disable-PSRemoting

阻止 PowerShell 终结点接收远程连接。

语法

Disable-PSRemoting
       [-Force]
       [-WhatIf]
       [-Confirm]
       [<CommonParameters>]

说明

Disable-PSRemoting cmdlet 阻止在本地计算机上远程访问所有 Windows PowerShell 会话终结点配置。 这包括 PowerShell 6 或更高版本创建的任何终结点。

若要重新启用对所有会话配置的远程访问,请使用 Enable-PSRemoting cmdlet。 这包括 PowerShell 6 或更高版本创建的任何终结点。 若要启用对所选会话配置的远程访问,请使用 Set-PSSessionConfiguration cmdlet 的 AccessMode 参数。 还可以使用 Enable-PSSessionConfigurationDisable-PSSessionConfiguration cmdlet 为所有用户启用和禁用会话配置。 有关会话配置的详细信息,请参阅 about_Session_Configurations

注意

即使在运行 Disable-PSRemoting 之后,仍可以在本地计算机上建立环回连接。 环回连接是发源于且连接到同一本地计算机的 PowerShell 远程会话。 来自外部源的远程会话仍被阻止。 对于环回连接,必须使用隐式凭据以及 EnableNetworkAccess 参数。 有关环回连接的详细信息,请参阅 New-PSSession

若要运行此 cmdlet,请使用“以管理员身份运行”选项启动 Windows PowerShell。

示例

示例 1:阻止远程访问所有会话配置

此示例阻止远程访问计算机上的所有 PowerShell 会话终结点配置。

Disable-PSRemoting

WARNING: Disabling the session configurations does not undo all the changes made by the Enable-PSRemoting
 or Enable-PSSessionConfiguration cmdlet. You might have to manually undo the changes by following these
 steps:
    1. Stop and disable the WinRM service.
    2. Delete the listener that accepts requests on any IP address.
    3. Disable the firewall exceptions for WS-Management communications.
    4. Restore the value of the LocalAccountTokenFilterPolicy to 0, which restricts remote access to
       members of the Administrators group on the computer.

示例 2:阻止远程访问所有会话配置,而不提供确认提示

此示例阻止对计算机上的所有 PowerShell 会话终结点配置进行远程访问,而不会给出提示。

Disable-PSRemoting -Force

WARNING: Disabling the session configurations does not undo all the changes made by the Enable-PSRemoting
 or Enable-PSSessionConfiguration cmdlet. You might have to manually undo the changes by following these
 steps:
    1. Stop and disable the WinRM service.
    2. Delete the listener that accepts requests on any IP address.
    3. Disable the firewall exceptions for WS-Management communications.
    4. Restore the value of the LocalAccountTokenFilterPolicy to 0, which restricts remote access to
       members of the Administrators group on the computer.

示例 3:运行此 cmdlet 的效果

此示例显示了使用 Disable-PSRemoting cmdlet 的效果。 若要运行此命令序列,请使用“以管理员身份运行”选项启动 PowerShell。

禁用会话配置后,New-PSSession cmdlet 会尝试创建与本地计算机的远程会话(也称为“环回”)。 由于在本地计算机上禁用了远程访问,该命令会失败。

Disable-PSRemoting -Force
New-PSSession -ComputerName localhost

WARNING: Disabling the session configurations does not undo all the changes made by the Enable-PSRemoting
 or Enable-PSSessionConfiguration cmdlet. You might have to manually undo the changes by following these steps:
    1. Stop and disable the WinRM service.
    2. Delete the listener that accepts requests on any IP address.
    3. Disable the firewall exceptions for WS-Management communications.
    4. Restore the value of the LocalAccountTokenFilterPolicy to 0, which restricts remote access to
       members of the Administrators group on the computer.

New-PSSession : [localhost] Connecting to remote server localhost failed with the following error
 message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.
At line:1 char:1
+ New-PSSession -ComputerName localhost -ConfigurationName PowerShell.6
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OpenError: (System.Management.A\u2026tion.RemoteRunspace:RemoteRunspace)
 [New-PSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : AccessDenied,PSSessionOpenFailed

示例 4:运行此 cmdlet 和 Enable-PSRemoting 的效果

此示例演示使用 Disable-PSRemotingEnable-PSRemoting cmdlet 时对会话配置产生的效果。

Disable-PSRemoting 用于禁用对所有 PowerShell 会话终结点配置的远程访问。 Force 参数取消了所有用户提示。 Get-PSSessionConfigurationFormat-Table cmdlet 显示计算机上的会话配置。

该输出显示,具有网络令牌的所有远程用户都被拒绝访问终结点配置。 只要本地计算机上的 Administrators 组在本地连接(也称为环回)并使用隐式凭据,他们就可以访问终结点配置。

Disable-PSRemoting -Force
Get-PSSessionConfiguration | Format-Table -Property Name, Permission -AutoSize

Enable-PSRemoting -Force
Get-PSSessionConfiguration | Format-Table -Property Name, Permission -AutoSize

Name                          Permission
----                          ----------
microsoft.powershell          NT AUTHORITY\NETWORK AccessDenied, BUILTIN\Administrators AccessAllowed
microsoft.powershell.workflow NT AUTHORITY\NETWORK AccessDenied, BUILTIN\Administrators AccessAllowed
microsoft.powershell32        NT AUTHORITY\NETWORK AccessDenied, BUILTIN\Administrators AccessAllowed
microsoft.ServerManager       NT AUTHORITY\NETWORK AccessDenied, BUILTIN\Administrators AccessAllowed
WithProfile                   NT AUTHORITY\NETWORK AccessDenied, BUILTIN\Administrators AccessAllowed

Name                          Permission
----                          ----------
microsoft.powershell          BUILTIN\Administrators AccessAllowed
microsoft.powershell.workflow BUILTIN\Administrators AccessAllowed
microsoft.powershell32        BUILTIN\Administrators AccessAllowed
microsoft.ServerManager       BUILTIN\Administrators AccessAllowed
WithProfile                   BUILTIN\Administrators AccessAllowed

Enable-PSRemoting cmdlet 重新启用对计算机上所有 PowerShell 会话终结点配置的远程访问。 Force 参数禁止显示所有用户提示,并在不提示的情况下重启 WinRM 服务。 新输出显示已从所有会话配置中删除 AccessDenied 安全描述符。

示例 5:具有已禁用会话终结点配置的环回连接

此示例演示如何禁用终结点配置,并演示如何成功建立与已禁用终结点的环回连接。 Disable-PSRemoting 禁用所有 PowerShell 会话终结点配置。

Disable-PSRemoting -Force
New-PSSession -ComputerName localhost

WARNING: Disabling the session configurations does not undo all the changes made by the Enable-PSRemoting
 or Enable-PSSessionConfiguration cmdlet. You might have to manually undo the changes by following these steps:
    1. Stop and disable the WinRM service.
    2. Delete the listener that accepts requests on any IP address.
    3. Disable the firewall exceptions for WS-Management communications.
    4. Restore the value of the LocalAccountTokenFilterPolicy to 0, which restricts remote access to
       members of the Administrators group on the computer.

New-PSSession : [localhost] Connecting to remote server localhost failed with the following error message : Access is
denied. For more information, see the about_Remote_Troubleshooting Help topic.
At line:1 char:1
+ New-PSSession -ComputerName localhost
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotin
   gTransportException
    + FullyQualifiedErrorId : AccessDenied,PSSessionOpenFailed

New-PSSession -ComputerName localhost -EnableNetworkAccess

Id Name       Transport ComputerName  ComputerType   State   ConfigurationName   Availability
 -- ----       --------- ------------  ------------   -----   -----------------   ------------
 1  Runspace1  WSMan     localhost     RemoteMachine  Opened  powershell.6           Available

首次使用 New-PSSession 尝试创建到本地计算机的远程会话。 这种类型的连接通过网络堆栈,而不是环回。 因此,尝试连接到已禁用的终结点将会失败,并出现“访问被拒绝”错误。

第二次使用 New-PSSession 还会尝试创建与本地计算机的远程会话。 在这种情况下,它会成功,因为它是绕过网络堆栈的环回连接。

满足以下条件时会创建环回连接:

  • 要连接到的计算机名称为“localhost”。
  • 不会传入任何凭据。 当前登录的用户(隐式凭据)用于连接。
  • 使用了 EnableNetworkAccess 开关参数。

有关环回连接的详细信息,请参阅 New-PSSession 文档。

示例 6:阻止远程访问具有自定义安全描述符的会话配置

此示例演示了 Disable-PSRemoting cmdlet 禁用对所有会话配置(包括具有自定义安全描述符的会话配置)的远程访问。

Register-PSSessionConfiguration 创建 Test 会话配置。 FilePath 参数指定用来自定义会话的会话配置文件。 ShowSecurityDescriptorUI 参数显示一个对话框,用于设置会话配置的权限。 在“权限”对话框中,我们为指示的用户创建自定义完全访问权限。

Get-PSSessionConfigurationFormat-Table cmdlet 显示会话配置及其属性。 输出显示 Test 会话配置对指示的用户允许交互式访问和特殊权限。

Disable-PSRemoting 禁用对所有会话配置的远程访问。

Register-PSSessionConfiguration -Name Test -FilePath .\TestEndpoint.pssc -ShowSecurityDescriptorUI -Force
Get-PSSessionConfiguration | Format-Table -Property Name, Permission -Wrap

Disable-PSRemoting -Force
Get-PSSessionConfiguration | Format-Table -Property Name, Permission -Wrap
New-PSSession -ComputerName localhost -ConfigurationName Test

Name                          Permission
----                          ----------
microsoft.powershell          BUILTIN\Administrators AccessAllowed
Test                          NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Administrators AccessAllowed,
DOMAIN01\User01 AccessAllowed

WARNING: Disabling the session configurations does not undo all the changes made by the Enable-PSRemoting
 or Enable-PSSessionConfiguration cmdlet. You might have to manually undo the changes by following these steps:
    1. Stop and disable the WinRM service.
    2. Delete the listener that accepts requests on any IP address.
    3. Disable the firewall exceptions for WS-Management communications.
    4. Restore the value of the LocalAccountTokenFilterPolicy to 0, which restricts remote access to
       members of the Administrators group on the computer.

Name                          Permission
----                          ----------
microsoft.powershell          NT AUTHORITY\NETWORK AccessDenied, BUILTIN\Administrators AccessAllowed
Test                          NT AUTHORITY\NETWORK AccessDenied, NTAUTHORITY\INTERACTIVE AccessAllowed,
BUILTIN\Administrators AccessAllowed, DOMAIN01\User01 AccessAllowed


[Server01] Connecting to remote server failed with the following error message : Access is denied. For more information, see the about_Rem
ote_Troubleshooting Help topic.
+ CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [], PSRemotingTransportException
+ FullyQualifiedErrorId : PSSessionOpenFailed

现在,Get-PSSessionConfigurationFormat-Table cmdlet 显示所有网络用户的 AccessDenied 安全描述符已添加到所有会话配置,包括 Test 会话配置。 虽然其他安全描述符没有更改,但“network_deny_all”安全描述符优先。 这是通过尝试使用 New-PSSession 连接到 Test 会话配置来演示的。

示例 7:重新启用对所选会话配置的远程访问

此示例演示如何重新启用仅到选定会话配置的远程访问。 禁用所有会话配置后,我们将重新启用特定会话。

Set-PSSessionConfiguration cmdlet 用于更改 microsoft.ServerManager 会话配置。 值为 RemoteAccessMode 参数会重新启用对配置的远程访问。

Disable-PSRemoting -Force
Get-PSSessionConfiguration | Format-Table -Property Name, Permission -AutoSize

Set-PSSessionConfiguration -Name Microsoft.ServerManager -AccessMode Remote -Force
Get-PSSessionConfiguration | Format-Table -Property Name, Permission -AutoSize

WARNING: Disabling the session configurations does not undo all the changes made by the Enable-PSRemoting
 or Enable-PSSessionConfiguration cmdlet. You might have to manually undo the changes by following these steps:
    1. Stop and disable the WinRM service.
    2. Delete the listener that accepts requests on any IP address.
    3. Disable the firewall exceptions for WS-Management communications.
    4. Restore the value of the LocalAccountTokenFilterPolicy to 0, which restricts remote access to
       members of the Administrators group on the computer.

Name                          Permission
----                          ----------
microsoft.powershell          NT AUTHORITY\NETWORK AccessDenied, BUILTIN\Administrators AccessAllowed
microsoft.powershell.workflow NT AUTHORITY\NETWORK AccessDenied, BUILTIN\Administrators AccessAllowed
microsoft.powershell32        NT AUTHORITY\NETWORK AccessDenied, BUILTIN\Administrators AccessAllowed
microsoft.ServerManager       NT AUTHORITY\NETWORK AccessDenied, BUILTIN\Administrators AccessAllowed
WithProfile                   NT AUTHORITY\NETWORK AccessDenied, BUILTIN\Administrators AccessAllowed

Name                          Permission
----                          ----------
microsoft.powershell          NT AUTHORITY\NETWORK AccessDenied, BUILTIN\Administrators AccessAllowed
microsoft.powershell.workflow NT AUTHORITY\NETWORK AccessDenied, BUILTIN\Administrators AccessAllowed
microsoft.powershell32        NT AUTHORITY\NETWORK AccessDenied, BUILTIN\Administrators AccessAllowed
microsoft.ServerManager       BUILTIN\Administrators AccessAllowed
WithProfile                   NT AUTHORITY\NETWORK AccessDenied, BUILTIN\Administrators AccessAllowed

参数

-Confirm

提示你在运行 cmdlet 之前进行确认。

Type:SwitchParameter
Aliases:cf
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Force

强制运行命令而不要求用户确认。

Type:SwitchParameter
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-WhatIf

显示运行该 cmdlet 时会发生什么情况。 cmdlet 未运行。

Type:SwitchParameter
Aliases:wi
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

输入

None

不能通过管道将对象传递给此 cmdlet。

输出

None

此 cmdlet 不返回任何输出。

备注

  • 禁用会话配置不会撤消 Enable-PSRemotingEnable-PSSessionConfiguration cmdlet 所做的所有更改。 你可能需要手动撤消以下更改。

    1. 停止并禁用 WinRM 服务。
    2. 删除接受任何 IP 地址上的请求的侦听器。
    3. 禁用 WS-Management 通信的防火墙例外。
    4. 将 LocalAccountTokenFilterPolicy 的值还原为 0,这将限制对计算机上 Administrators 组成员的远程访问。

    会话配置是一组定义会话环境的设置。 连接到计算机的每个会话都必须使用一个在该计算机上注册的会话配置。 通过拒绝对所有会话配置的远程访问,你可以有效地阻止远程用户建立连接到计算机的会话。

    在 Windows PowerShell 2.0 中,Disable-PSRemoting 向所有会话配置的安全描述符添加了 Deny_All 条目。 此设置可阻止所有用户创建到本地计算机的用户托管的会话。 在 Windows PowerShell 3.0 中,Disable-PSRemoting 向所有会话配置的安全描述符添加了 Network_Deny_All 条目。 此设置可阻止其他计算机上的用户在本地计算机上创建用户托管的会话,但允许本地计算机的用户创建用户托管的环回会话。

    在 Windows PowerShell 2.0 中,Disable-PSRemoting 等效于 Disable-PSSessionConfiguration -Name *。 在 Windows PowerShell 3.0 及更高版本中,Disable-PSRemoting 等效于 Set-PSSessionConfiguration -Name \<Configuration name\> -AccessMode Local