How to add more than one machine to the trusted hosts list using winrm

前端 未结 4 1869
甜味超标
甜味超标 2020-12-02 05:33

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.

I am adding machine A

相关标签:
4条回答
  • 2020-12-02 05:51

    I prefer to work with the PSDrive WSMan:\.

    Get 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

    Set-Item WSMan:\localhost\Client\TrustedHosts -Value 'machineC' -Concatenate
    
    0 讨论(0)
  • 2020-12-02 06:01

    The suggested answer by Loïc MICHEL blindly 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:

    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
    
    0 讨论(0)
  • 2020-12-02 06:03
    winrm set winrm/config/client '@{TrustedHosts="machineA,machineB"}'
    
    0 讨论(0)
  • 2020-12-02 06:07

    I created a module to make dealing with trusted hosts slightly easier, psTrustedHosts. You can find the repo here on 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:

    Install-Module psTrustedHosts -Force
    

    In your example, if you wanted to append hosts 'machineC' and 'machineD' you would simply use the following command:

    Add-TrustedHost 'machineC','machineD'
    

    To be clear, this adds hosts 'machineC' and 'machineD' to any hosts that already exist, it does not overwrite existing hosts.

    The Add-TrustedHost command supports pipeline processing as well (so does the Remove-TrustedHost command) so you could also do the following:

    'machineC','machineD' | Add-TrustedHost
    
    0 讨论(0)
提交回复
热议问题