PowerShell: Set DNS Server IPs on Default Network Interface

$dns1=.{
    Import-Module ActiveDirectory
    $fsmoRoles=Get-ADDomainController -Filter *|Select-Object Name, Domain, Forest, OperationMasterRoles|Where-Object {$_.OperationMasterRoles}|select Name,OperationMasterRoles
    $pdcServer=($fsmoRoles|?{'PDCEmulator' -in $_.OperationMasterRoles}).Name
    return [system.net.dns]::GetHostByName($pdcServer).AddressList.IpaddressToString|select -first 1
}
$dns2='8.8.8.8'

$defaultInterface=get-wmiobject win32_networkadapterconfiguration -filter "ipenabled='true'"|?{$_.DefaultIpGateway -ne $null}
Set-DnsClientServerAddress -InterfaceIndex $defaultInterface.Index -ServerAddresses @($dns1,$dns2)
Register-DnsClient

Leave a Reply

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