如何使用winrm将多台计算机添加到受信任主机列表中

103

在从远程机器上运行PowerShell命令时,我们需要将远程机器添加到主机机器的受信任主机列表中。

我正在使用以下命令将机器A添加到机器B的受信任主机列表中:

winrm set winrm/config/client ‘@{TrustedHosts="machineA"}’

如何将更多的机器(例如机器C、机器D)添加到机器B的可信主机列表中?

6个回答

155

我更喜欢使用PSDrive WSMan:\

获取 TrustedHosts

Get-Item WSMan:\localhost\Client\TrustedHosts

设置 TrustedHosts

提供一个由单个计算机名称用逗号隔开的字符串。

Set-Item WSMan:\localhost\Client\TrustedHosts -Value 'machineA,machineB'

或者(危险的)通配符

Set-Item WSMan:\localhost\Client\TrustedHosts -Value '*'

要将内容追加到列表中,可以使用 -Concatenate 参数。

Set-Item WSMan:\localhost\Client\TrustedHosts -Value 'machineC' -Concatenate

有没有一种方法可以将主机添加到列表中?因为我找不到任何用于追加的API。 - Snow
12
您可以使用-Value“machineB”和-Concatenate进行附加。 - SxMT
1
@dhcgm 这个解决方案不适用于依赖Kerberos进行身份验证的域控制服务器。你能确认一下吗?所以,尽管添加了显式的受信任主机,只要我在服务器上拥有管理员权限,我仍然可以使用非受信任的主机访问服务器。我认为这只适用于工作组计算机。谢谢。 - objectNotFound
@objectNotFound 在我的环境中,我只在工作组计算机上使用PowerShell远程控制,因此我无法确认您的论点。但是这听起来是有道理的。 - hdev
@objectNotFound 对我来说在域成员服务器之间运行良好,包括跨越域。但它不能向您添加的服务器添加权限,因此,如果您连接的用户没有访问远程服务器的权限,它仍然无法工作,但原因不同。一旦我在目标服务器上授予了我正在连接的用户所需的权限,我就能够成功连接。 - Keith Langmead

77
winrm set winrm/config/client '@{TrustedHosts="machineA,machineB"}'

22
如果有人收到“Error: Invalid use of command line ...”的响应,请尝试去掉单引号。请注意,翻译后不能添加解释或其他内容,需要保持原意和简洁性。 - svarog
这对我来说根本行不通,无论是单引号还是双引号。我仍然会收到“错误:命令使用无效”的提示。 - Hylle
@svarog 对我来说恰恰相反。我必须添加单引号。之前我遇到了同样的错误 Error: Invalid use of command - Bruno Bieri

15

建议答案是由Loïc MICHEL提供的,他盲目地向TrustedHosts条目中写入了一个新值。
我认为更好的方法是首先查询TrustedHosts。
正如Jeffery Hicks在2010年发布的文章中所述,首先查询TrustedHosts条目:

PS C:\> $current=(get-item WSMan:\localhost\Client\TrustedHosts).value
PS C:\> $current+=",testdsk23,alpha123"
PS C:\> set-item WSMan:\localhost\Client\TrustedHosts –value $current

9

我创建了一个模块,使得处理受信任主机变得更加容易,psTrustedHosts。您可以在GitHub上找到这个仓库,链接为这里。它提供了四个函数,使得处理受信任主机变得轻松: Add-TrustedHost, Clear-TrustedHost, Get-TrustedHost, 和 Remove-TrustedHost。您可以使用以下命令从PowerShell Gallery安装该模块:

Install-Module psTrustedHosts -Force

在您的例子中,如果您想要添加主机'machineC'和'machineD',您只需使用以下命令:
Add-TrustedHost 'machineC','machineD'

需要明确的是,此命令会将“machineC”和“machineD”添加到已有的主机中,而不是覆盖现有的主机。

Add-TrustedHost 命令也支持管道处理(Remove-TrustedHost 命令也是如此),因此您还可以执行以下操作:

'machineC','machineD' | Add-TrustedHost

@HerbM 域名正常工作。带通配符的范围似乎只适用于单个值,即您可以有一个逗号分隔的机器列表或包含通配符的字符串,但不能有一个逗号分隔的列表,其中列表中的一个值具有通配符。这看起来像是WinRM的问题。它会让您添加一个带有子网掩码的值,但当您尝试连接到范围内的计算机时,它似乎不将其解释为网络范围,因此无法正常工作。 - Jason Boyd
2
显然,您必须使用贫民窟的“子网划分”(在八位边界上)而不是CIDR或MASK表示法:192.168.230.* 而不是:192.168.224.0/19 #或其他 - HerbM

0

和 @Altered-Ego 一样,但是使用 txt 文件:

Get-Content "C:\ServerList.txt"
machineA,machineB,machineC,machineD


$ServerList = Get-Content "C:\ServerList.txt"
    $currentTrustHost=(get-item WSMan:\localhost\Client\TrustedHosts).value
    if ( ($currentTrustHost).Length -gt "0" ) {
        $currentTrustHost+= ,$ServerList
        set-item WSMan:\localhost\Client\TrustedHosts –value $currentTrustHost -Force -ErrorAction SilentlyContinue
        }
    else {
        $currentTrustHost+= $ServerList
        set-item WSMan:\localhost\Client\TrustedHosts –value $currentTrustHost -Force -ErrorAction SilentlyContinue
    }

"-ErrorAction SilentlyContinue"在旧版PS中是必需的,以避免虚假错误信息。
PS C:\Windows\system32> get-item WSMan:\localhost\Client\TrustedHosts


   WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Client

Type            Name                           SourceOfValue   Value
----            ----                           -------------   -----
System.String   TrustedHosts                                   machineA,machineB,machineC,machineD

0

winrm set winrm/config/client '@{TrustedHosts="ServerA"}'

会产生以下错误:

语法错误:输入必须是形式为 {KEY="VALUE"[;KEY="VALUE"]} 的格式

这个方法对我有用(在 Server 2016 上):

winrm set winrm/config/client @{TrustedHosts="ServerA"}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接