Script to check if DNS is set via DHCP across network.

Looking for a script that will go out and check if DNS is SET to DHCP. The script will run against all the computers in the network.  The output should only contain the name of Computers that are not using DHCP Option for DNS.

Thanks in advance
GGHCAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robberbaron (robr)Commented:
1. how are you going to get the script to run ?  GroupPolicy or PSExec against a list of PC's ?
2. have you checked the output of script from Get DHCP options  ?
3. I've run it and it works but is unsigned and I dont use the DHCP options on my home network so cant check all.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Is this about an Active Directory domain?
0
GGHCAuthor Commented:
@Robberbaron It will be against a list of computers which will basically have all the domain PCs.  I have to review the script in #2. But seems a bit much. Was thinking there is a simple script or command to see if it's using DHCP or not.

@qlemo Yes it's an AD domain
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
There is nothing simple about that request. You are asking for something (1)  you have to query each machine for, and (2) you get second to no info about.
Running
netsh interface ipv4 show dns | find "DHCP" >nul && echo %computername% - DHCP DNS || echo %computername% - static DNS

Open in new window

gets near, but the issue with that is that there are usually more than one virtual NICs (e.g. loopback, VPN interfaces, ...), and it is very difficult with netsh to find the active interface.
I'm now analysing the PS script linked to above, to see if we can simplify it for this specific request.
0
GGHCAuthor Commented:
@Qlemo
I have this code from another one of my scripts which selects the interface I want to work with.
But having trouble using it to check/set dhcp setting

$ips = Get-NetIPAddress -InterfaceAlias '*ethernet*' -IPAddress '172.120.*'

foreach ($ip in $ips) {
    if ($ip.IPAddress -like '172.120.8.*') {
        Set-DnsClientServerAddress -InterfaceAlias $ip.InterfaceAlias -ServerAddresses '172.120.8.150','172.120.9.150'
    }
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
The linked script is useless for this purpose. It only decodes the DHCP options received, without any relation to whether they are used or not.
This local script will show if the current machine runs with DHCP DNS or not (from my own research)
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled = 'True' AND DHCPEnabled ='True'" |
  % { Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\$($_.SettingID)"} |
  ? { $_.DHCPNameServer <# -and $_.NameServer -ne '' #> } |
  % { [PSCustomObject] @{computername = $env:computername; DHCPDNS = $_.NameServer -eq '' }}

Open in new window

It will return the computername and true/false. For getting a list of those with static DNS only, enable the condition commented out.

The issue now is that WMI works remotely, but remote registry access isn't easily implemented. Nevertheless, this is my approach:
Get-ADComputer -Filter {Enabled -eq $true} | Select -Expand Name |
  % {
    $pc = $_

    Write-Progress "Reading $pc"
    
    if (Test-Connection $pc -quiet -Count 1)
    {
      try {
        $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $pc)
        Get-WmiObject -ComputerName $pc Win32_NetworkAdapterConfiguration -Filter "IPEnabled = 'True' AND DHCPEnabled ='True'" |
        % {
          if ($key = $reg.OpenSubKey("SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\$($_.SettingID)"))
          {
            $NameServer     = $key.GetValue('NameServer')
            $DHCPNameServer = $key.GetValue("DHCPNameServer")
    
            [PSCustomObject] @{computername = $pc; Status = 'ok'; DNSisDHCP = $NameServer -eq '' -and $DHCPNameServer -ne ''}
          }
        }
        $reg.Close()
      }
      catch {
        [PSCustomObject] @{computername = $pc; Status = 'Error'; DNSisDHCP = $null}  
      }
    } else {
      [PSCustomObject] @{computername = $pc; Status = 'no response'; DNSisDHCP = $null}
    }
  }

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GGHCAuthor Commented:
Worked like charm!!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
DHCP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.