Author Posts

April 30, 2018 at 2:30 pm

I am working with a company that has a a number of parent and child domains in DNS. The layout is something like this:

parent.com
intra.parent.com
MI.intra.parent.com
IN.intra.parent.com
KY.intra.parent.com
etc...
meet.parent.com
meetHQ.parent.com
etc...
sip.parent.com
etc...
etc...

I am attempting to write a script that will search through all of DNS for A records. When I attempt to list all forward lookup zones, like so:

Get-DnsServerZone -ComputerName dnsServer01 |
    Where-Object {$_.IsReverseLookupZone -eq $false} | 
        Out-GridView

I get only the top level zones (intra.parent.com, meet.parent.com, etc.). I cannot return MI.intra.parent.com, or KY.intra.parent.com. This is leaving me unable to search for the A records in all areas.

As an alternative, I have tried searching for the records using the .Net method, like so:

$pc = "testPC1"
[System.Net.Dns]::GetHostByName($pc)

and this returns the HostName including Zone name, CNAME and IP address(es) as I would expect. But I am unsure how then to translate this into a method of modifying or deleting the record, as even a straight call to that zone is failing for me. For example, running the code above gives me the output of "testPC1.MI.intra.parent.com", however if I run this:

Get-DnsServerResourceRecord -ComputerName $DNSServer -ZoneName "mi.intra.parent.com" -Node "testPC1"

I get the error:

Get-DnsServerResourceRecord : The zone mi.intra.parent.com was not found on server $DNSServer.

I tried going up a level to just intra.parent.com, but get the response:

Get-DnsServerResourceRecord : Failed to get testPC1 record in intra.parent.com zone on DNSServer server.

So I am looking for suggestions on either querying through all levels of dns zones, or somehow using the other DNS functions on the information returned from the .Net call that seems to be working.

April 30, 2018 at 8:35 pm

To get A records in all zones, this will work:

Get-DnsServerZone | Where-Object {$_.ZoneName -like '*contoso.com'} | Get-DnsServerResourceRecord -RRType A

The output probably isn't very helpful though as it's just a list of all your A records. I'd be inclined to stick each zone in its own text file:

$zones = Get-DnsServerZone | Where-Object {$_.ZoneName -like '*contoso.com'}

foreach ($zone in $zones) {

    Get-DnsServerResourceRecord -ZoneName $zone.ZoneName -RRType A | Out-File "E:\Temp\$($zone.ZoneName).txt" -append

}

May 1, 2018 at 12:34 pm

Thanks, Matt. I ended up doing something similar to find the record:

$oRecord = Get-DnsServerZone -ComputerName $DNSServer | 
                Where-Object {$_.IsReverseLookupZone -eq $false} | 
                    Get-DnsServerResourceRecord -ComputerName $DNSServer |
                        Where {$_.RecordType -eq "A" -and $_.HostName -like "TestPC1*"}

It returns the record, but with different information than the .Net way. For example, the .Net return shows the HostName as "TestPC1.MI.intra.parent.com" (which is correct) whereas the way I have it written above gives me just "TestPC1.MI". Either way, trying to do a straight call to Get-DNSServerResourceRecord I get an error that "the zone was not found on the DNS Server". It is looking more and more like something borked in their whole configuration of DNS.

At this point, while I can get the record through a couple of different methods, I am unable to delete it because the DNS server is saying it cannot see that zone (obviously not the case as I can browse to and delete the record through the Snap-In)

May 1, 2018 at 4:32 pm

I ended up going this route, seems to work

function _removeFromDNS {
    [CmdLetBinding()]

    Param (
        [Parameter(Mandatory, Position=1)]
            [string] $DNSServer,
        [Parameter(Mandatory, Position=2)]
            [string] $PC
    )

    $aRecord = $null
    $ptr = $null
    
        $AZones = Get-DnsServerZone -ComputerName $DNSServer |
            Where-Object {$_.IsReverseLookupZone -eq $false}

        $PTRZones = Get-DnsServerZone -ComputerName $DNSServer |
            Where-Object {$_.IsReverseLookupZone -eq $true}

            foreach ($fwdZone in $AZones) {
                if ($aRecord = Get-DnsServerResourceRecord -ComputerName $DNSServer -ZoneName $fwdZone.ZoneName -RRType A | 
                    Where-Object {$_.HostName -like "*$($PC)*"}) {
                        try {
                            Remove-DnsServerResourceRecord -InputObject $aRecord -ZoneName $fwdZone.ZoneName -ComputerName $DNSServer -Force -Confirm:$false
                            [System.Windows.MessageBox]::Show("Successfully deleted A record for $($PC)")
                        } catch {
                            [System.Windows.MessageBox]::Show("Unable to find A record for $($PC) in DNS")
                        }
                }
            }

            foreach ($zone in $PTRZones) {
                if ($ptr = Get-DnsServerResourceRecord -ComputerName $DNSServer -ZoneName $zone.ZoneName -RRType Ptr | 
                    Where-Object {$_.RecordData.PtrDomainName -like "*$($PC)*"}) {
                        try {
                            Remove-DnsServerResourceRecord -InputObject $ptr -ZoneName $zone.ZoneName -ComputerName $DNSServer -Force -Confirm:$false
                            [System.Windows.MessageBox]::Show("Successfully deleted PTR record for $($PC)")
                        } catch {
                            [System.Windows.MessageBox]::Show("Unable to find PTR record for $($PC) in DNS")
                        }
                }
            }
}