Functions/RemotePS/Get-TrustedHosts.ps1

<#
.SYNOPSIS
    Returns the registered trustedhosts from the local machine or validate the existance of an address if the parameter computer is used.
.DESCRIPTION
    Returns the registered trustedhosts from the local machine as an array or as string or validate the existance of an address if the parameter computer is used.
    Default a stringarray is returned with registered hosts.
.EXAMPLE
    Get-TrustedHosts
 
    result:
    @(
        '192.168.1.100'
        '*.domain.nl'
        'servername'
    )
.EXAMPLE
    Get-TrustedHosts -AsString
 
    result:
    '192.168.1.100,*.domain.nl,servername'
 
.EXAMPLE
    Get-TrustedHosts -Computer '192.168.1.100'
 
    result:
    $true
#>

function Get-TrustedHosts {

    [CmdletBinding(DefaultParameterSetName='Default')]   

    Param (
        # Search pattern. Returns true if the pattern matches.
        [Parameter(ParameterSetName='Validate')]
        [string[]]
        $Computer,

        # Returns result in one string with a comma seperator.
        [Parameter(ParameterSetName='AsString')]
        [switch]
        $AsString,

        # Starts service WinRM when the service is stopped. Requires elivation.
        [switch] 
        $Force
    )
    
    if ((Get-Service 'WinRM').Status -ne 'running'){
        if($Force -eq $false){
            Write-Error 'Service WinRM should be running to execute Get-TrustedHosts.'
            break
        } 
        
        Write-Warning 'Service WinRM should be running to execute Get-TrustedHosts. Starting WinRM...'

        if ((Confirm-PowershellSessionHasAdministratorRole) -eq $false) {
            $Message = 'Administrator rights are required to start service WinRM.'
            Write-Error $Message
            break
        }  
        
        Set-Service 'WinRM' -Status Running
        
    }

    if ($Computer) {
        foreach($Comp in $Computer){        
            $UniqueComputer = $Comp.Split(',').Trim()
            
            foreach ($UniqueComp in $UniqueComputer){
                if (((Get-Item WSMan:\localhost\Client\TrustedHosts).Value) -notmatch $UniqueComp) {
                    return $false
                }
            }
        }

        return $true
    }

    $TrustedHosts = (Get-Item WSMan:\localhost\Client\TrustedHosts).Value.Split(',').Trim()

    if($AsString){
        return [string]::Join(',',$TrustedHosts)
    } 
    
    $TrustedHosts
}

Export-ModuleMember -Function Get-TrustedHosts