Disable-PSRemoting

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

语法

Default (默认值)

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 创建到本地计算机的远程会话(也称为“环回”)。 命令成功。

第三个命令在 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 命令,该命令仅显示表中配置的名称和权限属性。

输出显示仅拒绝远程用户访问配置。 允许本地计算机上的 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 来创建测试会话配置。 该命令使用 FilePath 参数指定一个会话配置文件,用于自定义会话和 ShowSecurityDescriptorUI 参数,以显示设置会话配置权限的对话框。 在“权限”对话框中,我们为 Domain01\User01 用户创建自定义完全访问权限。

第二个命令使用 Get-PSSessionConfigurationFormat-Table cmdlet 来显示会话配置及其属性。 输出显示测试会话配置允许对 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 之前,提示你进行确认。

参数属性

类型:SwitchParameter
默认值:False
支持通配符:False
不显示:False
别名:cf

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Force

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

参数属性

类型:SwitchParameter
默认值:None
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-WhatIf

显示 cmdlet 运行时会发生什么情况。 命令脚本未运行。

参数属性

类型:SwitchParameter
默认值:False
支持通配符:False
不显示:False
别名:无线

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

CommonParameters

此 cmdlet 支持通用参数:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 有关详细信息,请参阅 about_CommonParameters

输入

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。