• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 75
  • Last Modified:

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
0
GGHC
Asked:
GGHC
  • 3
  • 3
1 Solution
 
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
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
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
 
GGHCAuthor Commented:
Worked like charm!!!!
0
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.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now