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

Ubuntu 20.04: Setting Static IP Address on an Interface

Short Version # Configure networking sudo vim /etc/netplan/*.yaml ### Sample content ### network: version: 2…

PowerShell: Set PasswordNeverExpires on SamAccountName

Quick 1-liner Code $accounts=@( 'orange', 'apple', 'pear', 'dog', 'cat', 'dinosaur', 'chicken', 'cow', 'yomama', 'yodada' )…

PowerShell: How To Set IP and Domain Restrictions to Specific IIS Sites

# Enable IP Filtering Feature in IIS using PowerShell Install-WindowsFeature Web-IP-Security Restart-Service W3SVC # Optional:…