当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] Enter-PSSession:在交互式 Shell 中运行远程命令

作者:精品下载站 日期:2024-12-14 23:17:04 浏览:14 分类:玩电脑

Enter-PSSession:在交互式 Shell 中运行远程命令


Enter-PSSession cmdlet 允许您与远程计算机建立持久的交互式 PowerShell 会话。您在命令提示符中输入的所有命令都将在远程计算机上执行。在本文中,我们将解释 Enter-PSSession 的主要功能以及如何使用它来远程管理运行 Windows 10/11 和 Windows Server 2022/2019/2016 的计算机。

Enter-PSSession

cmdlet 由 PowerShell Remoting 堆栈提供支持。 PSRemoting 基于Web 管理服务(WS-Management)和WinRM 服务(Windows 远程管理)。计算机之间的流量在协议级别进行加密(您可以选择为 PSRemoting WinRM 流量启用 SSL 加密)。您可以使用各种身份验证方法,包括 NTLM 和 Kerberos。

在简单的情况下。要与远程计算机建立交互式 PowerShell 会话,您只需指定要连接的计算机名称(ComputerName 选项)。要连接到远程计算机,只需运行以下命令:

Enter-PSSession hq-srv01.a-d.site

[玩转系统] Enter-PSSession:在交互式 Shell 中运行远程命令

如果当前用户有权连接到远程主机,您将连接到远程计算机上的交互式 shell。

您可以在连接之前提示输入用户凭据:

Enter-PsSession -ComputerName hq-srv01.a-d.site -Credentials a-d\maxbak

或者 :

$creds = Get-Credential
Enter-PSSession -ComputerName hq-srv01 -Credential $creds

请注意,远程计算机的名称现在显示在 PowerShell 提示符开头的方括号中 ([hq-srv01.a-d.site])。这样您就可以确定您是在本地还是远程 shell 会话中运行。

所有远程运行命令的输出都显示在本地控制台中。您可以运行

hostname

命令并确保在远程计算机上运行它。

您可以在此交互式命令提示符中运行任何命令(根据您的权限)。

例如,让我们使用 PowerShell 显示 Windows 网络设置:

Get-NetIPConfiguration

您可以更改远程计算机上的 DNS 设置:

Set-DNSClientServerAddress -InterfaceIndex 6 -ServerAddresses 192.168.13.4, 192.168.100.4

[玩转系统] Enter-PSSession:在交互式 Shell 中运行远程命令

要退出交互式远程 shell 会话,请运行

Exit-PSSession

或者

exit

。 PS 提示符将变得正常,您将返回到本地 PowerShell 控制台:

[玩转系统] Enter-PSSession:在交互式 Shell 中运行远程命令

以前,管理员主要使用PsExec工具在远程Windows计算机上运行交互式命令提示符。然而,当 Enter-PSSession 出现后,他们不再需要使用外部工具了。

在 Windows Server 2016/2019/2022 中,默认启用 PowerShell Remoting(您可以在服务器管理器 -> 本地服务器 -> 远程管理=启用中看到它)。

[玩转系统] Enter-PSSession:在交互式 Shell 中运行远程命令

Get-PSSessionConfiguration

此命令还用于获取允许通过 WinRM 连接的用户和组的列表。要使用 PSRemoting,用户帐户必须是管理员组或远程管理用户组的成员。您可以详细了解如何为非管理员用户启用 WinRM PowerShell 远程处理。

[玩转系统] Enter-PSSession:在交互式 Shell 中运行远程命令

您可以测试是否能够通过 PowerShell Remoting 在本地连接到计算机:

Test-WSMan -ComputerName localhost

如果该命令返回 WSMan 架构版本,则允许使用 PS Remoting 远程连接到计算机。

[玩转系统] Enter-PSSession:在交互式 Shell 中运行远程命令

如果禁用或未配置 PowerShell 远程处理,则会出现以下错误:

Test-WSMan : <f:WSManFaultxmlns:f="http://schemas.microsoft.com/wbem/wsman/1/wsmanfault" Code="2150858770" Machine="srv02"><f:Message>The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig".

要启用 PowerShell 远程处理,请运行以下命令:

Enable-PSRemoting -Force

这个命令:

  • 启用WinRM服务并将其启动类型设置为自动;

  • 在默认 WinRM 端口(用于 HTTP 流量的 TCP/5985)上创建连接点;

  • 将 WS-Management 的例外添加到 Windows 防火墙(如果您手动配置 PSRemoting,请使用 PowerShell 或 GPO 添加防火墙规则)

  • 允许远程 PowerShell 会话

  • 重新启动 WinRM 服务

确保 WinRM 服务正在运行并设置为自动启动:

Get-Service WinRM | Select MachineName,Name,Status, StartType

[玩转系统] Enter-PSSession:在交互式 Shell 中运行远程命令

Enable-PSRemoting 命令仅适用于域和专用 Windows 网络配置文件。如果要在公共网络中的计算机上启用 PSRemoting,请将网络位置从“公共”更改为“私有”,或使用以下命令:

Enable-PSRemoting -SkipNetworkProfileCheck -Force

在 Active Directory 域中,在服务器和计算机上集中配置 Windows 远程管理 (PSRemoting) 的最简单方法是通过组策略。

现代 PowerShell 版本(v6 或 v7)支持安全 Shell 协议 (SSH),以通过 PowerShell 远程处理连接到远程计算机。远程计算机上必须有可用的 SSH 连接点(如何在 Windows 10 上启用内置 OpenSSH 服务器?)。您可以使用以下命令通过 SSH 启动交互式 PSRemoting 会话:

Enter-PSSession -HostName [email protected]

或者使用 RSA 密钥通过 SSH 进行身份验证:

Enter-PSSession -HostName [email protected]:22 -KeyFilePath c:\PS\max_rsa_key

您可以将 Enter-PSSession 与 New-PSSession 一起使用:

$s = New-PSSession -ComputerName hq-srv01.a-d.site
Enter-PSSession -Session $s

Enter-PSSession 支持多种身份验证方法。您可以使用 -Authentication 参数设置您想要的身份验证。支持 Basic、Digest、Kerberos、CredSSP、NegotiateWithImplicitCredential、Negotiate Challenge 身份验证方法。

在上面的示例中,我们展示了如何在同一 Windows 域中的计算机之间创建交互式 Enter-PSSession 连接(为连接指定 FQDN 或短名称就足够了,使用 Kerberos 身份验证)。如果您尝试使用 IP 地址或 CNAME 连接到远程计算机,您将无法通过身份验证:

Enter-PSSession : Connecting to remote server 192.168.31.12 failed with the following error message: The WinRM client cannot process the request. Default authentication may be used with an IP address under the following conditions: the transport is HTTPS or the destination is in the TrustedHosts list, and explicit credentials are provided. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated.

[玩转系统] Enter-PSSession:在交互式 Shell 中运行远程命令

要使用其IP地址连接到远程计算机,您可以将主机添加到受信任主机(Trusted Hosts)列表或使用WinRM的SSL(更安全)。

要将 IP 地址添加到受信任的主机,请运行以下命令:

Set-Item WSMan:\localhost\Client\TrustedHosts -Value 192.168.13.5

您可以使用通配符掩码添加受信任的主机:

Set-Item WSMan:\localhost\Client\TrustedHosts -Value *.a-d.site

显示受信任主机列表:

Get-Item WSMan:\localhost\Client\TrustedHosts

以同样的方式,您可以将您的主机添加到远程计算机上的受信任主机列表中。

重新启动服务:

Restart-Service WinRM

要使用 IP 地址连接到远程计算机,请运行以下命令:

Enter-PSSession -ComputerName 192.168.13.5 -Credential (Get-Credential -UserName a-d\maxbak)

Enter-PSSession

New-PSSession

cmdlet 创建持久的一对一远程会话,主要用于交互场景。如果要自动运行脚本或作业或同时在多台远程计算机上执行某些操作,请使用 Invoke-Command 命令。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯