Windows 如何使用winrm将多台计算机添加到受信任主机列表
要从远程计算机在计算机上运行powershell命令,我们必须将远程计算机添加到主机的受信任主机列表中 我使用以下命令将机器A添加到机器B的受信任主机:Windows 如何使用winrm将多台计算机添加到受信任主机列表,windows,powershell,hosts,winrm,Windows,Powershell,Hosts,Winrm,要从远程计算机在计算机上运行powershell命令,我们必须将远程计算机添加到主机的受信任主机列表中 我使用以下命令将机器A添加到机器B的受信任主机: winrm set winrm/config/client ‘@{TrustedHosts="machineA"}’ 如何将更多机器(如机器C、机器D)添加到机器B的受信任主机列表?winrm set winrm/config/client'@{TrustedHosts=“machineA,machineB”} 我更喜欢使用PSDriveWS
winrm set winrm/config/client ‘@{TrustedHosts="machineA"}’
如何将更多机器(如机器C、机器D)添加到机器B的受信任主机列表?winrm set winrm/config/client'@{TrustedHosts=“machineA,machineB”}
我更喜欢使用PSDriveWSMan:\
获得信任的主机
Get-Item WSMan:\localhost\Client\TrustedHosts
设置受信任的主机
Get-Item WSMan:\localhost\Client\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
建议的答案是盲目地将新值写入TrustedHosts条目。我认为,更好的方法是首先查询受信任的主机。
作为,首先查询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
我创建了一个模块,使处理受信任的主机稍微容易一点。你可以在GitHub上找到回购协议。它提供了四个功能,使使用受信任主机变得简单:
添加受信任主机
,清除受信任主机
,获取受信任主机
,以及删除受信任主机
。您可以使用以下命令从PowerShell Gallery安装模块:
Install-Module psTrustedHosts -Force
Add-TrustedHost 'machineC','machineD'
在您的示例中,如果要附加主机“machineC”和“machineD”,只需使用以下命令:
Install-Module psTrustedHosts -Force
Add-TrustedHost 'machineC','machineD'
需要明确的是,这会将主机“machineC”和“machineD”添加到任何已存在的主机,但不会覆盖现有主机
addtrustedhost
命令也支持管道处理(Remove TrustedHost
命令也支持管道处理),因此您还可以执行以下操作:
'machineC','machineD' | Add-TrustedHost
与@相同,但与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
有没有办法将主机附加到列表中?因为我找不到任何用于追加的API。您可以使用-Value“machineB”追加-Concatenate@dhcgm此解决方案不适用于依赖Kerberos进行身份验证的域控制服务器。你能确认一下吗?因此,尽管添加了明确的受信任主机,只要我对服务器拥有管理员权限,我仍然可以使用不受信任的主机访问服务器。我认为这只适用于工作组计算机。谢谢。@objectNotFound在我的环境中我只在工作组计算机上使用Powershell远程处理,所以我无法确认你的论文。如果有人收到
错误:命令行使用无效…
响应,请尝试删除单引号。无论单引号与否,这对我都不起作用。我得到错误:不管怎样,命令的使用都是无效的。@svarog对我来说是相反的。我不得不加上单引号。在此之前,我遇到了相同的错误错误:命令的无效使用
@HerbM域名工作正常。带有通配符的范围似乎只适用于单个值,即,可以有一个逗号分隔的机器列表,或一个包含通配符的字符串,但不能有一个逗号分隔的列表,其中列表中的一个值具有通配符。这看起来像是WinRM问题。它可以让你添加一个带有子网掩码的值,但当你试图连接到一个范围内的机器时,它似乎不会将其解释为一个网络范围,因此似乎不起作用。显然,你必须使用穷人的“子网”(在八位字节边界上),而不是CIDR或掩码符号:192.168.230。*不是:192.168.224.0/19#或其他任何东西