# setDnsEntries.ps1

$computernames=@(
    "$env:computername"
)
$dnsServers=@(
    "8.8.8.8",
    "4.4.2.2"
)

$results=[hashtable]@{}
foreach ($computername in $computernames){
    $psSession=new-pssession $computername
    if($psSession.State -eq 'Opened'){
        $result=invoke-command -session $pssession {
            param($dnsServers)
            try{
                $defaultInterfaceIndex=(Get-NetRoute -DestinationPrefix "0.0.0.0/0").IfIndex
                Set-DnsClientServerAddress -InterfaceIndex $defaultInterfaceIndex -ServerAddresses $dnsServers
                write-host "$env:computername`:`r`n$((Get-DnsClientServerAddress -interfaceindex $defaultInterfaceIndex|?{$_.AddressFamily -eq 'IPv4'}|out-string).trim())"
                return $true
            }catch{
                write-warning $_ 
                return $false
            }            
        } -ArgumentList (,$dnsServers)
        $results+=[hashtable]@{$computername=$result}
        remove-pssession $psSession
    }else{
        write-warning "Unable to connect to $computername"
        $results+=[hashtable]@{$computername='unableToConnect'}
    }
    pause
}