Posted On September 12, 2022

PowerShell: Set DNS Records on Remote Computers

kimconnect 0 comments
blog.KimConnect.com >> Codes , Networking >> PowerShell: Set DNS Records on Remote Computers
# 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
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Add Root Domain to Trusted Sites

Windows Servers often have 'hard admin' modes, where accesses to external websites are painstakingly restricted.…

JavaScript: Random Quote Generator

Demo Link: https://blog.kimconnect.com/wp-content/projects/randomQuoteGenerator.html HTML code: <body> <div class="container fluid"> <div> <div class="text-center"> <h1> Random Quote…

PowerShell: Error While Invoking Functions Containing 2 Parameters

Issue: This function will raise an error when being invoked: function localFunc ($x, $y){ begin{}…