Disable-PSRemoting

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

语法

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

说明

此 cmdlet 仅在 Windows 平台上可用。

Disable-PSRemoting cmdlet 阻止对本地计算机上所有 PowerShell 版本 6 及更高版本的会话终结点配置的远程访问。 它不会影响 Windows PowerShell 终结点配置。 若要禁用 Windows PowerShell 会话终结点配置,请从 Windows PowerShell 会话中运行 Disable-PSRemoting 命令。

若要重新启用对所有 PowerShell 版本 6 及更高版本的会话终结点配置的远程访问,请使用 Enable-PSRemoting cmdlet。 若要重新启用对所有 Windows PowerShell 会话终结点配置的远程访问,请从 Windows PowerShell 会话中运行 Enable-PSRemoting 命令。

注意

如果要禁用对本地 Windows 计算机的所有 PowerShell 远程访问,则必须从 PowerShell 版本 6 或更高版本的会话和 Windows PowerShell 会话中运行此命令。 所有 Windows 计算机上均默认安装了 Windows PowerShell。

若要禁用和重新启用对特定会话终结点配置的远程访问,请使用 Enable-PSSessionConfigurationDisable-PSSessionConfiguration cmdlet。 若要设置各个终结点的特定访问配置,请使用 Set-PSSessionConfiguration cmdlet 以及 AccessMode 参数。 有关会话配置的详细信息,请参阅 about_Session_Configurations

注意

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

此 cmdlet 仅在 Windows 平台上可用。 它在 Linux 或 macOS 版本的 PowerShell 上不可用。 若要运行此 cmdlet,请使用“以管理员身份运行”选项启动 PowerShell。

示例

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

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

Disable-PSRemoting

WARNING: PowerShell remoting has been disabled only for PowerShell 6+ configurations and does not affect
 Windows PowerShell remoting configurations. Run this cmdlet in Windows PowerShell to affect all PowerShell
 remoting configurations.

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 会话配置远程访问而不会给出确认提示

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

Disable-PSRemoting -Force

WARNING: PowerShell remoting has been disabled only for PowerShell 6+ configurations and does not affect
 Windows PowerShell remoting configurations. Run this cmdlet in Windows PowerShell to affect all PowerShell
 remoting configurations.

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 -ConfigurationName PowerShell.6

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
----               ----------
PowerShell.6       NT AUTHORITY\NETWORK AccessDenied, NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Administrators AccessAllowed ...
PowerShell.6.2.0   NT AUTHORITY\NETWORK AccessDenied, NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Administrators AccessAllowed ...

Name               Permission
----               ----------
PowerShell.6       NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Administrators AccessAllowed ...
PowerShell.6.2.0   NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Administrators AccessAllowed ...

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

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

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

Disable-PSRemoting -Force

WARNING: PowerShell remoting has been disabled only for PowerShell 6+ configurations and does not affect
 Windows PowerShell remoting configurations. Run this cmdlet in Windows PowerShell to affect all PowerShell
 remoting configurations.

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 -ComputerName localhost -ConfigurationName powershell.6 -Credential (Get-Credential)

PowerShell credential request
Enter your credentials.
User: UserName
Password for user UserName: ************

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.

New-PSSession -ComputerName localhost -ConfigurationName powershell.6 -EnableNetworkAccess

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

首次使用 New-PSSession 将会尝试创建与本地计算机的远程会话。 ConfigurationName 参数用于指定已禁用的 PowerShell 终结点。 凭据通过 Credential 参数显式传递给命令。 这种类型的连接将通过网络堆栈,并且不是环回。 因此,尝试连接到已禁用的终结点将会失败,并出现“访问被拒绝”错误。

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

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

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

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

示例 6:禁用所有 PowerShell 远程处理终结点配置

此示例演示运行 Disable-PSRemoting 命令如何未影响 Windows PowerShell 终结点配置。 在 Windows PowerShell 中运行的 Get-PSSessionConfiguration 显示所有终结点配置。 我们看到,Windows PowerShell 终结点配置未禁用。

Disable-PSRemoting -Force
powershell.exe -command 'Get-PSSessionConfiguration'

WARNING: PowerShell remoting has been disabled only for PowerShell 6+ configurations and does not affect
 Windows PowerShell remoting configurations. Run this cmdlet in Windows PowerShell to affect all PowerShell
 remoting configurations.

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          : microsoft.powershell
PSVersion     : 5.1
StartupScript :
RunAsUser     :
Permission    : NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Administrators AccessAllowed, BUILTIN\Remote
                Management Users AccessAllowed

Name          : microsoft.powershell.workflow
PSVersion     : 5.1
StartupScript :
RunAsUser     :
Permission    : BUILTIN\Administrators AccessAllowed, BUILTIN\Remote Management Users AccessAllowed

Name          : microsoft.powershell32
PSVersion     : 5.1
StartupScript :
RunAsUser     :
Permission    : NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Administrators AccessAllowed, BUILTIN\Remote
                Management Users AccessAllowed

Name          : PowerShell.6
PSVersion     : 6.2
StartupScript :
RunAsUser     :
Permission    : NT AUTHORITY\NETWORK AccessDenied, NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Administrators
                AccessAllowed, BUILTIN\Remote Management Users AccessAllowed

Name          : PowerShell.6.2.2
PSVersion     : 6.2
StartupScript :
RunAsUser     :
Permission    : NT AUTHORITY\NETWORK AccessDenied, NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Administrators
                AccessAllowed, BUILTIN\Remote Management Users AccessAllowed

powershell.exe -command 'Disable-PSRemoting -Force'
powershell.exe -command 'Get-PSSessionConfiguration'

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          : microsoft.powershell
PSVersion     : 5.1
StartupScript :
RunAsUser     :
Permission    : NT AUTHORITY\NETWORK AccessDenied, NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Administrators
                AccessAllowed, BUILTIN\Remote Management Users AccessAllowed

Name          : microsoft.powershell.workflow
PSVersion     : 5.1
StartupScript :
RunAsUser     :
Permission    : NT AUTHORITY\NETWORK AccessDenied, BUILTIN\Administrators AccessAllowed, BUILTIN\Remote Management
                Users AccessAllowed

Name          : microsoft.powershell32
PSVersion     : 5.1
StartupScript :
RunAsUser     :
Permission    : NT AUTHORITY\NETWORK AccessDenied, NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Administrators
                AccessAllowed, BUILTIN\Remote Management Users AccessAllowed

Name          : PowerShell.6
PSVersion     : 6.2
StartupScript :
RunAsUser     :
Permission    : NT AUTHORITY\NETWORK AccessDenied, NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Administrators
                AccessAllowed, BUILTIN\Remote Management Users AccessAllowed

Name          : PowerShell.6.2.2
PSVersion     : 6.2
StartupScript :
RunAsUser     :
Permission    : NT AUTHORITY\NETWORK AccessDenied, NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Administrators
                AccessAllowed, BUILTIN\Remote Management Users AccessAllowed

若要禁用这些终结点配置,必须从 Windows PowerShell 会话中运行 Disable-PSRemoting 命令。 现在,从 Windows PowerShell 中运行的 Get-PSSessionConfiguration 显示所有终结点配置都处于禁用状态。

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

此示例演示了 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
----               ----------
PowerShell.6       NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Administrators AccessAllowed,
                   BUILTIN\Remote Management Users AccessAllowed
PowerShell.6.2.0   NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Administrators AccessAllowed,
                   BUILTIN\Remote Management Users AccessAllowed
Test               NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Administrators AccessAllowed,
                   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
----               ----------
PowerShell.6       NT AUTHORITY\NETWORK AccessDenied, NT AUTHORITY\INTERACTIVE AccessAllowed,
                   BUILTIN\Administrators AccessAllowed, BUILTIN\Remote Management Users AccessAllowed
PowerShell.6.2.0   NT AUTHORITY\NETWORK AccessDenied, NT AUTHORITY\INTERACTIVE AccessAllowed,
                   BUILTIN\Administrators AccessAllowed, BUILTIN\Remote Management Users AccessAllowed
Test               NT AUTHORITY\NETWORK AccessDenied, NT AUTHORITY\INTERACTIVE AccessAllowed,
                   BUILTIN\Administrators AccessAllowed, User01 AccessAllowed

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 Test
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OpenError: (System.Management.A\u2026tion.RemoteRunspace:RemoteRunspace)
 [New-PSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : AccessDenied,PSSessionOpenFailed

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

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

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

Set-PSSessionConfiguration cmdlet 用于更改 PowerShell.6 会话配置。 值为 Remote 的 AccessMode 参数会重新启用对配置的远程访问。

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

Set-PSSessionConfiguration -Name PowerShell.6 -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
----                 ----------
PowerShell.6         NT AUTHORITY\NETWORK AccessDenied, NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Adm ...
PowerShell.6.2.0     NT AUTHORITY\NETWORK AccessDenied, NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Adm ...

Name                 Permission
----                 ----------
PowerShell.6         NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Administrators AccessAllowed, BUILTIN\ ...
PowerShell.6.2.0     NT AUTHORITY\NETWORK AccessDenied, NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Adm ...

参数

-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 不返回任何输出。

备注

此 cmdlet 仅在 Windows 平台上可用。

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

    1. 停止并禁用 WinRM 服务。
    2. 删除接受任何 IP 地址上的请求的侦听器。
    3. 禁用 WS-Management 通信的防火墙例外。
    4. 将 LocalAccountTokenFilterPolicy 的值还原为 0,这将限制对计算机上 Administrators 组成员的远程访问。
  • 会话终结点配置是一组用于定义会话环境的设置。 连接到该计算机的每个会话都必须使用一个在该计算机上注册的会话终结点配置。 通过拒绝对所有会话终结点配置的远程访问,可以有效地阻止远程用户建立连接到该计算机的会话。