Validate IP Entry in DNS

This topic contains 4 replies, has 2 voices, and was last updated by  Kreston Yates 1 week, 1 day ago.

  • Author
    Posts
  • #100620

    Kreston Yates
    Participant

    I'm having a hard time checking to see if an IP exists in DNS. (not that a server is up, just that the IP exists)

    So the short of it is to import a csv file that contains a header for "Zone" and "IP", check DNS if a HOST A Record Exists (by IP); if it does, do nothing, if it doesnt create it

    Seems simple in my head but im getting lost on how to manipulate the objects from the initial get-dnsserverresourcerecord query

    $DNSCheck = import-csv -path C:\test.csv
    foreach ($row in $DNSCheck)
    {
    $GetDNSEntry = $null
    $GetDNSEntry = get-dnsserverreourcerecord -zonename $row.Zone -RRType "A" | select-object RecordData -expand-Property RecordData
    If ($getDNSEntry -match $row.IP)
    {
     add-dnsresourcerecordA -Name "." -zoneName $row.zone -ipv4address $row.IP
    }
    }
    

    i dont have a problem doing this for the zone check and creation

    $checkzone = $null
    $checkzone = (get-dnsserverzone -name $row.zone
    if($checkzone -ne $null)
     {Write-host " $($row.Zone) DNS Zone Already Exists"
    Else 
     {Write-Host "creating zone $($row.URL); Add-dnsserverprimaryzone -name $row.zone -replicationscope Domain}
    

    Any pointers would be appreciated!

  • #100635

    Olaf Soyk
    Participant

    I'm not completely sure what you mean with "to manipulate the objects from the initial get-dnsserverresourcerecord query" but the following might be helpful. With this snippet you get a complete list of all A records from all zones existing on the server.

    $AllZonesAllARecords = Get-DnsServerZone | 
        ForEach-Object {
            $Zone = $_.ZoneName
            Get-DnsServerResourceRecord -RRType A -ZoneName $_.ZoneName | 
                Select-Object -Property HostName,
                                        @{Name = 'IP'; Expression = {$_.RecordData.IPv4Address}},
                                        @{Name = 'Zone'; Expression = {$Zone}}
        }

    Now you could use Compare-Object to compare this list to your scv file and use the output accordingly.

    • #100638

      Kreston Yates
      Participant

      Olaf,

      Thanks for the response. I'm just having a hard time getting the data I need and then comparing, to see if it exists. I have a File with Zone Names and IP's. I figured out how to check the existing zones and create a new one if it hasnt been created. I then want to go further and check if an IP has been created in a zone.

      So i have a zone im.not.getting.it.com with IP's that should be associated with it of 172.16.28.10 and 172.16.28.11
      If the zones not there i create it. If it is created then i want to check if the IP's have been created under that zone, and if they havent, create them.

      Sorry if i'm not conveying the question clearly, its frustrating because i can get the data in some form or fashion but I am having a hard time using it properly.

    • #100639

      Olaf Soyk
      Participant

      Did you try the code I posted? You can use Compare-Object to get the difference between your csv file and the created list of DNS records. Then you use this "list of differences" to create the DNS zones and the DNS records you need.

    • #100644

      Kreston Yates
      Participant

      I did, and I appreciate the pointers. I went about it a little different but you got me where i needed to be.

      Thanks again!

You must be logged in to reply to this topic.