Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 41
  • 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
 
QlemoC++ DeveloperCommented:
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
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
QlemoC++ DeveloperCommented:
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
 
QlemoC++ DeveloperCommented:
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

Join & Write a Comment

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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