windows 如何使用 winrm 将多台机器添加到受信任主机列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21548566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add more than one machine to the trusted hosts list using winrm
提问by cmm user
To run powershell commands on a machine from a remote machine we have to add the remote machine to the trusted hosts list of the host machine.
要从远程机器在机器上运行 powershell 命令,我们必须将远程机器添加到主机的受信任主机列表中。
I am adding machine A to machine B's trusted hosts using the following command :
我正在使用以下命令将机器 A 添加到机器 B 的受信任主机:
winrm set winrm/config/client ‘@{TrustedHosts="machineA"}'
How to add more machines say machine C, machine D to trusted hosts list of machine B?
如何将更多机器(例如机器 C、机器 D)添加到机器 B 的受信任主机列表中?
回答by dhcgn
I prefer to work with the PSDrive WSMan:\
.
我更喜欢使用 PSDrive WSMan:\
。
Get TrustedHosts
获取 TrustedHosts
Get-Item WSMan:\localhost\Client\TrustedHosts
Set TrustedHosts
设置可信主机
provide a single, comma-separated, string of computer names
提供单个以逗号分隔的计算机名称字符串
Set-Item WSMan:\localhost\Client\TrustedHosts -Value 'machineA,machineB'
or (dangerous) a wild-card
或(危险的)通配符
Set-Item WSMan:\localhost\Client\TrustedHosts -Value '*'
to append to the list, the -Concatenate
parameter can be used
要附加到列表,-Concatenate
可以使用该参数
Set-Item WSMan:\localhost\Client\TrustedHosts -Value 'machineC' -Concatenate
回答by Lo?c MICHEL
winrm set winrm/config/client '@{TrustedHosts="machineA,machineB"}'
回答by Altered-Ego
The suggested answer by Lo?c MICHELblindly writes a new value to the TrustedHosts entry.
I believe, a better way would be to first query TrustedHosts.
As Jeffery Hicks posted in 2010, first query the TrustedHosts entry:
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
回答by Jason Boyd
I created a module to make dealing with trusted hosts slightly easier, psTrustedHosts. You can find the repo hereon GitHub. It provides four functions that make working with trusted hosts easy: Add-TrustedHost
, Clear-TrustedHost
, Get-TrustedHost
, and Remove-TrustedHost
. You can install the module from PowerShell Gallery with the following command:
我创建了一个模块psTrustedHosts使处理受信任的主机更容易一些。你可以找到回购这里GitHub上。它提供了使与信任主机方便工作四个功能:Add-TrustedHost
,Clear-TrustedHost
,Get-TrustedHost
,和Remove-TrustedHost
。您可以使用以下命令从 PowerShell Gallery 安装模块:
Install-Module psTrustedHosts -Force
In your example, if you wanted to append hosts 'machineC' and 'machineD' you would simply use the following command:
在您的示例中,如果您想附加主机“machineC”和“machineD”,您只需使用以下命令:
Add-TrustedHost 'machineC','machineD'
To be clear, this addshosts 'machineC' and 'machineD' to any hosts that already exist, it does not overwrite existing hosts.
需要明确的是,这会将主机 'machineC' 和 'machineD' 添加到任何已经存在的主机中,它不会覆盖现有的主机。
The Add-TrustedHost
command supports pipeline processing as well (so does the Remove-TrustedHost
command) so you could also do the following:
该Add-TrustedHost
命令也支持管道处理(Remove-TrustedHost
命令也是如此),因此您还可以执行以下操作:
'machineC','machineD' | Add-TrustedHost