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

[玩转系统] 为非域(工作组)计算机配置 PowerShell 远程处理 (WinRM)

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

为非域(工作组)计算机配置 PowerShell 远程处理 (WinRM)


PowerShell Remoting 是一个很棒的工具,可让您通过 WinRM 在远程计算机上连接并运行命令。如果计算机加入到 Active Directory 域,则 PSRemoting 使用 Kerberos 对远程主机进行身份验证。但是,如果您的计算机位于工作组中,则必须使用 NTLM (TrustedHosts) 或 SSL 证书进行身份验证。让我们看看如何在工作组(非域)环境中配置和使用 PSRemoting (WinRM)。

在此示例中,Windows 工作组中有两台主机:

  • 管理员工作站—

    192.168.13.100
  • 用户的计算机 —

    192.168.13.222

我们的任务是通过 PowerShell Remoting 远程连接到用户的计算机。

第一步是在远程主机上启用并配置 WinRM。您必须在本地或远程启用 WinRM(例如,使用 RDP、psexec 或 GPO)。

确保 WinRM 服务正在目标用户计算机上运行:

Get-Service -Name "*WinRM*" | select status

如果该服务未运行,请启用它:

Enable-PSRemoting
WinRM has been updated to receive requests.
WinRM service type changed successfully.
WinRM service started.
WinRM has been updated for remote management.
WinRM firewall exception enabled.
Configured LocalAccountTokenFilterPolicy to grant administrative rights remotely to local users.

[玩转系统] 为非域(工作组)计算机配置 PowerShell 远程处理 (WinRM)

如您所见,LocalAccountTokenFilterPolicy UAC 选项会自动启用,以允许管理员帐户下的远程访问。

如果计算机上设置了公共网络连接类型,则在启用 WinRM 时您将看到以下错误:

Set-WSManQuickConfig : ... WinRM firewall exception will not work since one of the network connection types on this machine is set to Public. Change the network connection type to either Domain or Private and try again.

将网络类型更改为专用(

Set-NetConnectionProfile -NetworkCategory Private

)或运行以下命令:

Enable-PSRemoting -SkipNetworkProfileCheck

在 Windows Defender 防火墙中打开端口 TCP/5985 以连接到 WinRM。最简单的方法是使用 PowerShell 打开 Windows 防火墙端口。在本例中,我们将仅对管理员计算机的 IP 地址开放远程访问(更安全),但您可以对所有人开放(指定 Any 而不是 IP 地址):

Set-NetFirewallRule -DisplayName "Windows Remote Management (HTTP-In)" -RemoteAddress 192.168.13.100
Enable-NetFirewallRule -DisplayName "Windows Remote Management (HTTP-In)"

在管理员计算机上,确保用户的计算机现在接受通过 PSRemoting 的远程连接:

Test-NetConnection 192.168.13.222 -Port 5985
Test-WsMan 192.168.13.222

[玩转系统] 为非域(工作组)计算机配置 PowerShell 远程处理 (WinRM)

但是,如果您尝试使用 Invoke-Command 或 Enter-PSSession cmdlet 远程连接到用户计算机,则会出现以下错误:

Enter-PSSession 192.168.13.222
Enter-PSSession : Connecting to remote server 192.168.13.222 failed with the following error message: The WinRM client cannot process the request. If the authentication scheme is different from Kerberos, or if the client computer is not joined to a domain, then HTTPS transport must be used or the destination machine must be added to the TrustedHosts configuration setting. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. PSRemotingTransportException.

远程计算机上的 WinRM HTTP 侦听器仅允许通过 Kerberos 身份验证进行连接。

Get-ChildItem -Path WSMan:\localhost\Service\Auth\

[玩转系统] 为非域(工作组)计算机配置 PowerShell 远程处理 (WinRM)

为了使用 NTLM 协商身份验证,您的计算机必须信任远程计算机。在域中,这是使用 Kerberos 实现的,而在工作组环境中,您必须将计算机的 IP 地址添加到 TrustedHosts。

将用户计算机添加到管理员计算机上的 TrustedHosts(您可以使用其 IP 地址或 FQDN 来完成此操作):

Set-Item wsman:\localhost\client\TrustedHosts -Value 192.168.13.222 -Force

列出 TrustedHosts 中的计算机:

get-Item WSMan:\localhost\Client\TrustedHosts

要清除 TrustedHosts 列表:

Set-Item WSMan:\localhost\Client\TrustedHosts -Value "" -Force

要将新计算机添加到 TrustedHosts 列表,请使用 -Concatenate 选项:

Set-Item WSMan:\localhost\Client\TrustedHosts -Value 192.168.13.200 -Concatenate

您还可以允许远程连接到所有计算机(通常不建议这样做,因为 NTLM 身份验证的主要缺点之一是容易受到各种恶意攻击)。

Set-Item wsman:\localhost\Client\TrustedHosts -value *

然后尝试通过 PSRemoting 连接到远程计算机:

Enter-PSSession -ComputerName 192.168.13.222 -Credential 192.168.13.222\root

输入远程计算机的管理员密码并确保连接已成功建立(远程计算机的主机名或 IP 地址显示在 PowerShell 提示符中)。

[玩转系统] 为非域(工作组)计算机配置 PowerShell 远程处理 (WinRM)

现在,您可以使用 Invoke-Command 在远程工作组计算机上执行命令和脚本。例如,远程重启计算机:

Invoke-Command -ComputerName 192.168.13.222 -Credential 192.168.13.222\root -ScriptBlock {Restart-Computer}

或者运行 PowerShell 脚本:

Invoke-Command -ComputerName 192.168.13.222 -Credential 192.168.13.222\root -FilePath c:\Scripts\GetComputerInfo.ps1

在 WinRM 中,您还可以使用 HTTPS 连接到远程计算机。为此,您需要在远程计算机上颁发 SSL 证书并将其导入到管理员计算机。在这种情况下,您不需要将远程计算机 IP 地址添加到 TrustedHosts 列表中。详细了解如何通过 HTTPS 配置 PowerShell 远程处理 (WinRM)。

请注意,为了在远程计算机上进行身份验证,您必须使用-凭据选项输入用户密码。如果网络中有多台计算机具有不同的本地管理员密码,则将连接密码存储在保管库中会很方便。它可以是 Windows Credential Manager 密码保管库,也可以是外部存储,例如 KeePass、LastPass、HashiCorp Vault、Azure Key Vault 或 Bitwarden。

您可以使用 PowerShell 机密管理模块来访问此类保管库中保存的密码。现在,为了通过 PSRemoting 连接到远程计算机,只需:

  1. 例如,将连接密码保存到凭据管理器:

     cmdkey /add:192.168.13.222 /user:root /pass:Password1
  2. 使用 CredentialManager 模块从保管库获取名称和密码:

    $psCred = Get-StoredCredential -Target "192.168.13.222"
  3. 使用 PSRemoting 和保存的密码连接到远程计算机:

    Enter-PSSession -ComputerName 192.168.13.222 -Credential $psCred

如果将密码存储在其他保管库类型中,请使用 Microsoft.PowerShell.SecretManagement 模块获取保存的凭据。

在新的 PowerShell 版本(v6 或 v7)中,您可以使用 Secure Shell (SSH) 协议通过 PowerShell 远程处理连接到远程计算机。为此,必须在 Windows 中启用内置 SSH 服务器。您甚至可以使用 SSH 密钥向 Windows 进行身份验证:

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

默认情况下,WinRM 仅允许与管理员进行远程连接。但是,您可以允许非管理员用户使用 PSRemoting 进行远程访问。

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

取消回复欢迎 发表评论:

关灯