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 PowerShell 安装在所有 Windows 计算机上。
若要禁用和重新启用对特定会话终结点配置的远程访问,请使用 Enable-PSSessionConfiguration
和 Disable-PSSessionConfiguration
cmdlet。 若要设置各个终结点的特定访问配置,请使用 Set-PSSessionConfiguration
cmdlet 以及 AccessMode 参数。 有关会话配置的详细信息,请参阅 about_Session_Configurations。
备注
即使在运行 Disable-PSRemoting
仍可以在本地计算机上建立环回连接。 环回连接是一个源自并连接到同一本地计算机的 PowerShell 远程会话。 来自外部源的远程会话仍被阻止。 对于环回连接,必须使用 EnableNetworkAccess 参数的隐式凭据。 有关环回连接的详细信息,请参阅 New-PSSession。
此 cmdlet 仅在 Windows 平台上可用。 它在 Linux 或 macOS 版本的 PowerShell 上不可用。 若要运行此 cmdlet,请使用 以管理员身份运行 选项启动 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.
此示例阻止远程访问计算机上的所有 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.
此示例显示使用 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
此示例显示了使用 Disable-PSRemoting
和 Enable-PSRemoting
cmdlet 的会话配置的影响。
Disable-PSRemoting
用于禁用对所有 PowerShell 会话终结点配置的远程访问。
Force 参数禁止所有用户提示。
Get-PSSessionConfiguration
和 Format-Table
cmdlet 在计算机上显示会话配置。
输出显示,具有网络令牌的所有远程用户都被拒绝访问终结点配置。 只要本地计算机(也称为环回)并使用隐式凭据,就允许本地计算机上的管理员组访问终结点配置。
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 安全描述符。
此示例演示如何禁用终结点配置,并演示如何成功建立到已禁用终结点的环回连接。
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 switch 参数。
有关环回连接的详细信息,请参阅 New-PSSession 文档。
此示例演示如何运行 Disable-PSRemoting
命令不会影响 Windows PowerShell 终结点配置。
Get-PSSessionConfiguration
在 Windows PowerShell 中运行会显示所有终结点配置。 我们看到 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
命令。 现在,Get-PSSessionConfiguration
从 Windows PowerShell 中运行,显示禁用了所有终结点配置。
此示例演示 Disable-PSRemoting
cmdlet 禁用对包含具有自定义安全描述符的会话配置的所有会话配置的远程访问。
Register-PSSessionConfiguration
创建 测试 会话配置。
FilePath 参数指定自定义会话的会话配置文件。
ShowSecurityDescriptorUI 参数显示一个对话框,用于设置会话配置的权限。 在“权限”对话框中,我们为指示的用户创建自定义完全访问权限。
Get-PSSessionConfiguration
和 Format-Table
cmdlet 显示会话配置及其属性。 输出显示 测试 会话配置允许对指示的用户进行交互式访问和特殊权限。
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-PSSessionConfiguration
和 Format-Table
cmdlet 显示所有网络用户的 AccessDenied 安全描述符已添加到所有会话配置,包括 测试 会话配置。 虽然其他安全描述符没有更改,但“network_deny_all”安全描述符优先。 尝试使用 New-PSSession
连接到 测试 会话配置来说明这一点。
此示例演示如何仅重新启用对所选会话配置的远程访问。 禁用所有会话配置后,我们将重新启用特定会话。
Set-PSSessionConfiguration
cmdlet 用于更改 PowerShell.6 会话配置。
AccessMode 参数,其值为 Remote 重新启用对配置的远程访问。
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 ...
在运行 cmdlet 之前,提示你进行确认。
类型: | SwitchParameter |
别名: | cf |
Position: | Named |
默认值: | False |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
强制命令运行而不要求用户确认。
类型: | SwitchParameter |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
显示 cmdlet 运行时会发生什么情况。 cmdlet 未运行。
类型: | SwitchParameter |
别名: | wi |
Position: | Named |
默认值: | False |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
None
不能通过管道将对象传递给此 cmdlet。
None
此 cmdlet 不返回任何输出。
此 cmdlet 仅在 Windows 平台上可用。
禁用会话配置不会撤消
Enable-PSRemoting
或Enable-PSSessionConfiguration
cmdlet 所做的所有更改。 可能需要手动撤消以下更改。- 停止并禁用 WinRM 服务。
- 删除接受任何 IP 地址请求的侦听器。
- 禁用 WS-Management 通信的防火墙例外。
- 将 LocalAccountTokenFilterPolicy 的值还原为 0,这会限制对计算机上的 Administrators 组成员的远程访问。
会话终结点配置是一组设置,用于定义会话的环境。 连接到计算机的每个会话都必须使用计算机上注册的会话终结点配置之一。 通过拒绝对所有会话终结点配置的远程访问,可以有效地阻止远程用户建立连接到计算机的会话。