Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to programatically query DNS for records soon to expire / be scavenged?

Posted on 2008-06-11
4
Medium Priority
?
887 Views
Last Modified: 2013-12-01
I would like to know if there is a way I can programatically query a windows based DNS server for records that have failed to be reregistered in a certain time frame and thus up for expiration and scavenging?

Is there a query-language for DNS? How does one use it to find this sort of information?

An example would be wonderful in c# or vb or a scripting language... anything, really.
0
Comment
Question by:DefyFate05
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 71

Expert Comment

by:Chris Dent
ID: 21773436

Hey there,

It's all exposted in WMI.

I have VbScript or PowerShell versions to find this kind of thing. The PowerShell version is a hell of a lot neater / more flexible / more reliable.

Copy and paste this, fixing the Domain Name and Server Name.

Listing All Stale Records within a given Domain:

$DNSServer = "YourServerName"
$Domain = "YourDomain"

Get-WMIObject -Class "MicrosoftDNS_AType" -Namespace `
    "root\MicrosoftDNS" -Comp $DNSServer -Filter "DomainName='$Domain'" | `
    Select-Object `
    OwnerName,@{n="TimeStamp";e={(Get-Date("01/01/1601")).AddHours($_.TimeStamp)}} `
    | ?{($_.TimeStamp -lt ((Get-Date).AddDays(-14))) -and ($_.TimeStamp -ne (Get-Date("01/01/1601")))}

Note the use of "AddDays(-14)", that means we're only looking for entries where the TimeStamp is more than 14 days old, it assumes you're using 7 Days No-Refresh and 7 Days Refresh, change as appropriate.

HTH

Chris
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 21773447

Oh yeah, just for interest, this is a modification of it to show Static Records:

$DNSServer = "YourServerName"
$Domain = "YourDomain"

Get-WMIObject -Class "MicrosoftDNS_AType" -Namespace `
    "root\MicrosoftDNS" -Comp $DNSServer -Filter "DomainName='$Domain'" | `
    Select-Object `
    OwnerName,@{n="TimeStamp";e={(Get-Date("01/01/1601")).AddHours($_.TimeStamp)}} `
    | ?{$_.TimeStamp -eq (Get-Date("01/01/1601"))}

TimeStamp is always 0 for Static Records, so the adjusted time always ends up being 01/01/1601.

Chris
0
 

Author Comment

by:DefyFate05
ID: 21775939
Can you throw the vbscript version up?

0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 1000 total points
ID: 21776839

Certainly, here we go.

Chris
' No-Refresh + Refresh
Const MAXIMUM_AGE = 4
 
' DNS Server Name ("." if run locally)
Const SERVER_NAME = "."
 
' Domain Name to check 
' WMI query returns A records from all zones without WHERE filter
' Domain Name should be lower case and should not include trailing "."
Const DOMAIN_NAME = "domain.local"
 
Set objWMIService = GetObject("winmgmts:\\" & SERVER_NAME & "\root\MicrosoftDNS")
 
Set colItems = objWMIService.ExecQuery("SELECT * FROM MicrosoftDNS_AType WHERE " &_
        "DomainName='" & DOMAIN_NAME & "'")
 
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objFile = objFileSystem.OpenTextFile("StaleRecords.csv", 2, True, 0)
 
objFile.WriteLine "FQDN,IPAddress,TimeStamp"
 
For Each objItem In colItems
	' If InStr(1, objItem.DomainName, DOMAIN_NAME, VbTextCompare) > 0 Then
	        If objItem.TimeStamp > 0 Then
        	        dtmTimeStamp = DateAdd("h", objItem.TimeStamp, "1/1/1601 00:00:00 AM")
 
 
 	               If dtmTimeStamp <= (Date - MAXIMUM_AGE) Then
                	        objFile.WriteLine objItem.OwnerName & "," & objItem.IPAddress &_
        	                        "," & dtmTimeStamp
	                End If
        	End If
	' End If
Next
 
Set objFile = Nothing
Set objFileSystem = Nothing
 
Set colItems = Nothing
Set objWMIService = Nothing

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Remote Apps is a feature in server 2008 which allows users to run applications off Remote Desktop Servers without having to log into them to run the applications.  The user can either have a desktop shortcut installed or go through the web portal to…
The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question