Parsing ipconfig /displaydns

 

A recent question on the forum asked how you could get the contents on Windows 7 machines and earlier.

On later machines – Windows 8 and above –  its easy because you can use Get-DnsClientCache from the DnsClient module. This module is based on CIM classes that aren’t present on Windows 7 and earlier systems.

You can use ipconfig /displaydns to display the data but it looks like this

Record Name . . . . . : ns-nw.noaa.gov
Record Type . . . . . : 1
Time To Live  . . . . : 81966
Data Length . . . . . : 4
Section . . . . . . . : Additional
A (Host) Record . . . : 161.55.32.2

so you need to parse the strings into a format that you can work with. 

This is one solution

$props = [ordered]@{
  RecordName = “”
  RecordType = “”
  Section    = “”
  TimeToLive = 0
  DataLength = 0
  Data       = “”
}

$recs = @()

$cache = ipconfig /displaydns
for($i=0; $i -le ($cache.Count -1); $i++) {
if ($cache[$i] -like ‘*Record Name*’){
    $rec = New-Object -TypeName psobject -Property $props
    $rec.RecordName = ($cache[$i] -split -split “: “)[1]
    $rec.Section = ($cache[$i+4] -split -split “: “)[1]
    $rec.TimeToLive = ($cache[$i+2] -split -split “: “)[1]
    $rec.DataLength = ($cache[$i+3] -split -split “: “)[1]
   
    $irec = ($cache[$i+5] -split “: “)
    $rec.RecordType = ($irec[0].TrimStart() -split ‘ ‘)[0]
    $rec.Data = $irec[1]

    $recs += $rec
}
else {
    continue
}
}

$recs | Format-Table –AutoSize

Create an ordered hash table of output properties and an empty array to hold the results.

Get the output of  ipconfig /displaydns into $cache which will be an array of strings

Loop through $cache

if the record is like *Record Name*’ then process that record and the next five records to give the results.  The actual data record is split twice to give the record type and the data – otherwise you’ll have to translate the numeric values in the Record Type line.

The results are put into an object which is added to the output array.

Continue looping through $cache until you meet the next line with a Record Name or end of file.

Finally display the results.

This works but is messy – I’m going to investigate alternatives

Advertisements
This entry was posted in DNS, Networking, PowerShellGet. Bookmark the permalink.

One Response to Parsing ipconfig /displaydns

  1. mdedeboer says:

    Here is something that is still messy, but just another way of tackling it without naming each parameter (I think your code is slightly faster though):

    $cache = ipconfig /displaydns
    $arrRecs = @()
    ForEach($strLine in $cache){
        If($strLine -match 'Record Name'){
            $hashProp = @{}
            $intLineIndex = $cache.indexOf($strLine)
            ForEach($strSubLine in $cache[$intLineIndex..($intLineIndex + 5)]){
                $arrProp = $strSubLine.split(':')
                If($arrProp[0]){
                    $hashProp.($arrProp[0].Replace('.','')) = $arrProp[1]
                }
            }
            if($hashProp){
                $objDNS = New-Object -TypeName PSObject -Property $hashProp
                $arrRecs += $objDNS
            }
        }
    }    
    $arrRecs | Format-Table -AutoSize
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s