Link to home
Start Free TrialLog in
Avatar of David Sankovsky
David SankovskyFlag for Israel

asked on

Ascertain if DNS server on an interface is set by DHCP or not

Hi Experts.
I'm trying to write a PowerShell script that would tell me if all the physical Wired interfaces (ignoring virtual interfaces generated by VPNs and Physical Wireless Interfaces) are set to DHCP, not only on the IP Address, but also on the DNS Side, and set it to DHCP, if they are not.

I have the first part nailed down, It's fairly easy:
$adapters=$(Get-NetAdapter -Physical |Where {$_.MediaType -eq "802.3"})
foreach ($adapter in $adapters){
    if ( -not $(Get-NetIPInterface -InterfaceIndex $($adapter.ifIndex) -AddressFamily IPv4).Dhcp -eq "Enabled"){
        Set-NetIPInterface -InterfaceIndex $($adapter.ifIndex) -Dhcp Enabled
        Set-DnsClientServerAddress -InterfaceIndex $($adapter.ifIndex) -ResetServerAddresses
    }
}

Open in new window


That part was fairly easy to figure out.
The DNS part eludes me.
I can get the DNS Server address that is assigned to the interface like so:
Get-DnsClientServerAddress -InterfaceIndex $($adapter.ifIndex)

Open in new window


But I have no way of knowing if it was assigned via DHCP or not, even if I use the |fl switch to see all possible attributes, all I get is this:
InterfaceAlias  : Ethernet

InterfaceIndex  : 12
AddressFamily   : IPv4
ServerAddresses : {192.168.81.201}

InterfaceAlias  : Ethernet
InterfaceIndex  : 12
AddressFamily   : IPv6
ServerAddresses : {}

Any Ideas?
Avatar of yo_bee
yo_bee
Flag of United States of America image

Have you come across many in your environment?  
It is very rare for a network to have a client setup via DHCP and manually set DNS. You are sort of defeating the whole purpose of the DHCP setup.
Avatar of David Sankovsky

ASKER

I work in an Outsourcing IT company, and I've seen my fair share of... less than capable IT personnel who would rather find an "easy fix" rather than dealing with a possible configuration issue. In at least 3 of our customers, I've stumbled upon several computers who had their IP Set by DHCP, but the DNS was manually set to google's because the Network Admin they had before they got us, didn't know about conditional forwarders in the AD DNS Server...

So, not too many of them, but enough to have a deployable script handy..
Legit Question though :)
ASKER CERTIFIED SOLUTION
Avatar of yo_bee
yo_bee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help, it did help me complete my script.
Sorry for the delay, Sorta forgot I had the question open.