Disable-PSRemoting

阻止远程用户在本地计算机上运行命令。

语法

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

说明

Disable-PSRemoting cmdlet 可阻止其他计算机上的用户在本地计算机上运行命令。

Disable-PSRemoting 阻止对本地计算机上的所有会话配置的远程访问。 这可以阻止远程用户创建到本地计算机的临时或持久性会话。 Disable-PSRemoting 不会阻止本地计算机的用户在本地计算机或远程计算机上创建会话 (PSSessions) 。

若要重新启用对所有会话配置的远程访问,请使用 Enable-PSRemoting cmdlet。 若要启用对选定会话配置的远程访问,请使用 Set-PSSessionConfiguration cmdlet 的 AccessMode 参数。 还可使用 Enable-PSSessionConfiguration 和 Disable-PSSessionConfiguration cmdlet 为所有用户启用和禁用会话配置。 有关会话配置的详细信息,请参阅 about_Session_Configurations

在 Windows PowerShell 2.0 中,Disable-PSRemoting 阻止所有用户创建到本地计算机的用户托管会话。 在 Windows PowerShell 3.0 中,Disable-PSRemoting 阻止其他计算机上的用户在本地计算机上创建用户管理的会话,但允许本地计算机的用户创建用户管理的环回会话。

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

警告:在同时具有 Windows PowerShell 3.0 和 Windows PowerShell 2.0 引擎的系统上,请勿使用 Windows PowerShell 2.0 运行 Enable-PSRemotingDisable-PSRemoting cmdlet。 这些命令可能看起来是成功的,但并未正确配置远程处理。 远程命令以及之后启用和禁用远程访问的尝试都很可能会失败。

示例

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

PS C:\> Disable-PSRemoting

此命令可阻止对计算机上的所有会话配置的远程访问。

示例 2:禁止在没有确认提示的情况下远程访问所有会话配置

PS C:\> Disable-PSRemoting -Force

此命令可阻止远程访问计算机上的所有会话配置,而不进行提示。

示例 3:运行此 cmdlet 的效果

PS C:\> Disable-PSRemoting -Force


[ADMIN] PS C:\> New-PSSession -ComputerName localhost


Id Name       ComputerName    State    Configuration         Availability
-- ----       ------------    -----    -------------         ------------
1 Session1   Server02...     Opened   Microsoft.PowerShell     Available
# On Server02 remote computer:
PS C:\> New-PSSession -ComputerName Server01

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

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

第一个命令使用 Disable-PSRemoting cmdlet 来禁用 Server01 计算机上的所有已注册的会话配置。

第二个命令使用 New-PSSession cmdlet 创建到本地计算机的远程会话(也称为“loopback”)。 该命令执行成功。

第三个命令是在 Server02 远程计算机上运行的。 该命令使用 New-PSSession cmdlet 来创建到 Server01 远程计算机的会话。 由于已禁用远程访问,该命令将会失败。

示例 4:运行此 cmdlet 和 Enable-PSRemoting 的影响

PS C:\> Disable-PSRemoting -force

[ADMIN] PS C:\> Get-PSSessionConfiguration | Format-Table -Property Name, Permission -Auto

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


[ADMIN] PS C:\> Enable-PSRemoting -Force
WinRM already is set up to receive requests on this machine.
WinRM already is set up for remote management on this machine.

[ADMIN] PS C:\> Get-PSSessionConfiguration | Format-Table -Property Name, Permission -Auto

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

此示例显示了对使用 Disable-PSRemotingEnable-PSRemoting cmdlet 的会话配置的影响。

第一个命令使用 Disable-PSRemoting cmdlet 来禁用对所有会话配置的远程访问。 Force 参数取消了所有用户提示。

此命令使用 Get-PSSessionConfiguration cmdlet 在计算机上显示会话配置。 该命令使用管道运算符将结果发送到 Format-Table 命令,该命令仅在表格中显示 Name 和 Permission 属性。

该输出显示仅拒绝远程用户对这些配置的访问。 允许本地计算机上 Administrators 组的成员使用会话配置。 输出还显示命令会影响包括用户创建的 WithProfile 会话配置的所有会话配置。

第三个命令使用 Enable-PSRemoting cmdlet 来重新启用对计算机上的所有会话配置的远程访问。 该命令使用 Force 参数取消所有用户提示并重新启动 WinRM 服务,而不进行提示。

第四个命令使用 Get-PSSessionConfigurationFormat-Table cmdlet 来显示会话配置的名称和权限。 结果显示 AccessDenied 安全描述符已从所有会话配置中删除。

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

PS C:\> Register-PSSessionConfiguration -Name Test -FilePath .\TestEndpoint.pssc -ShowSecurityDescriptorUI

[ADMIN] PS C:\> Get-PSSessionConfiguration | Format-Table -Property Name, Permission -Wrap

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

[ADMIN] PS C:\> Disable-PSRemoting -Force


[ADMIN] PS C:\> Get-PSSessionConfiguration | Format-Table -Property Name, Permission -Wrap

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

# Domain01\User01

PS C:\> New-PSSession -ComputerName Server01 -ConfigurationName Test
[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

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

第一个命令使用 Register-PSSessionConfiguration cmdlet 创建 Test 会话配置。 该命令使用 FilePath 参数来指定自定义会话的会话配置文件,并使用 ShowSecurityDescriptorUI 参数来显示为会话配置设置权限的对话框。 在“权限”对话框中,我们将为 Domain01\User01 用户创建自定义的完全访问权限。

第二个命令使用 Get-PSSessionConfigurationFormat-Table cmdlet 来显示会话配置及其属性。 该输出显示 Test 会话配置允许交互式访问和 Domain01\User01 用户的特殊权限。

第三个命令使用 Disable-PSRemoting cmdlet 来禁用对所有会话配置的远程访问。

第四个命令使用 Get-PSSessionConfigurationFormat-Table cmdlet 来显示会话配置及其属性。 输出显示,所有网络用户的 AccessDenied 安全描述符已添加到包括测试会话配置的所有会话配置中。 尽管不会更改其他安全描述符,但“network_deny_all”安全描述符优先。

第五个命令显示 ,Disable-PSRemoting 命令甚至阻止对测试会话配置具有特殊权限的 Domain01\User01 用户使用测试会话配置远程连接到计算机。

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

PS C:\> Disable-PSRemoting -Force


[ADMIN] PS C:\> Get-PSSessionConfiguration | Format-Table -Property Name, Permission -Auto

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

[ADMIN] PS C:\> Set-PSSessionConfiguration -Name Microsoft.ServerManager -AccessMode Remote -Force

[ADMIN] PS C:\> Get-PSSessionConfiguration | Format-Table -Property Name, Permission -Auto

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

此示例演示如何重新启用仅到选定会话配置的远程访问。

第一个命令使用 Disable-PSRemoting cmdlet 来禁用对所有会话配置的远程访问。

第二个命令使用 Get-PSSessionConfigurationFormat-Table cmdlet 来显示会话配置及其属性。 该输出显示已向所有会话配置添加了适用于所有网络用户的 AccessDenied 安全描述符。

第三个命令使用 Set-PSSessionConfiguration cmdlet。 该命令使用了值为 Remote 的 AccessMode 参数启用对 Microsoft.ServerManager 会话配置的远程访问。 还可以使用 AccessMode 参数来启用本地访问和禁用会话配置。

第四个命令使用 Get-PSSessionConfigurationFormat-Table cmdlet 来显示会话配置及其属性。 该输出显示已移除适用于所有网络用户的 AccessDenied 安全描述符,从而恢复了对 Microsoft.ServerManager 会话配置的远程访问。

参数

-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 所做的所有更改。 你可能需要手动撤消以下更改。

停止并禁用 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

在 Windows PowerShell 2.0 中,Disable-PSRemoting 是一个函数。 从 Windows PowerShell 3.0 开始,它是一个 cmdlet。