Functions/RemotePS/Set-TrustedHosts.ps1

<#
.SYNOPSIS
    Overwrites the registered trustedhosts with the supplied computers addresses through WSMan(WS-Management).
.DESCRIPTION
    Overwrites the registered trustedhosts with the supplied computers addresses through WSMan(WS-Management).
.EXAMPLE
    Set-TrustedHosts -Computer @('192.168.1.100','192.168.1.101')
.EXAMPLE
    @('serverA ','serverB.domain.nl','serverC', 'machine01, machine02', @('192.168.1.100', '192.168.1.101')) | Set-TrustedHosts
    Result:
    String 'serverA,serverB.domain.nl,serverC,machine01,machine02,192.168.1.100,192.168.1.101' is saved as TrustedHosts.
#>

function Set-TrustedHosts {
    [cmdletbinding(SupportsShouldProcess=$True, ConfirmImpact = 'High')]
    Param (
        # Hostnames, fully qualified domain names or ip-addresses to write to TrustedHosts.
        [Parameter(Mandatory=$true, 
                   ValueFromPipeline=$True,
                   ValueFromPipelinebyPropertyName=$True)]
        [AllowEmptyString()]
        [string[]]
        $Computer
    )

    begin{
        if ((Confirm-PowershellSessionHasAdministratorRole) -eq $false) {
            $Message = "Administrator rights are required to execute Set-TrustedHosts"
            Write-Error $Message
            break
        }

        if ((Get-Service 'WinRM').Status -ne 'running'){
            if($force -eq $false){
                Write-Error 'Service WinRM should be running to execute Set-TrustedHosts.'
                break
            } 
            Write-Warning 'Service WinRM should be running to execute Set-TrustedHosts. Starting WinRM...'
            
            Set-Service 'WinRM' -Status Running
        }

        $TrustedHosts = @()
    }

    process{
        # Splits values if string contains concatenated values and removes whitespaces.
        $Computer.Split(',').Trim() | ForEach-Object { 
            if($_ -notin $TrustedHosts) {
                $TrustedHosts += $_ 
            }
        }
    }

    end{
        # Rejoins the values back into a string.
        $TrustedHosts = [string]::Join(',',$TrustedHosts)

        $Message = "Setting TrustedHosts to: {0}" -f $TrustedHosts
        Write-Host $Message

        # Update the TrustedHosts
        Set-Item WSMan:\localhost\Client\TrustedHosts -Value $TrustedHosts -Force
    }

}

Export-ModuleMember -Function Set-TrustedHosts