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

时间:2014-02-04 09:32:47

标签: windows powershell hosts winrm

要从远程计算机在计算机上运行powershell命令,我们必须将远程计算机添加到主机的可信主机列表中。

我使用以下命令将机器A添加到机器B的可信主机:

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

如何添加更多机器说机器C,机器D到机器B的可信主机列表?

4 个答案:

答案 0 :(得分:94)

我更喜欢使用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

答案 1 :(得分:58)

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

答案 2 :(得分:8)

Loïc MICHEL建议的答案盲目地将一个新值写入TrustedHosts条目 我相信,更好的方法是首先查询TrustedHosts 作为Jeffery Hicks posted in 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

答案 3 :(得分:0)

与@Altered-Ego 相同,但带有 txt.file:

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
    }

旧 PS 版本需要“-ErrorAction SilentlyContinue”以避免虚假错误消息:

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