Reputation: 11
The subject is my ultimate goal. Using Powershell I have managed to return the contents of "ipconfig /displaydns" formatted into a table but I used some publicly posted code and I am new to powershell so I don't fully understand how it all works. The problem with what I am using is that it only returns the results that begin with the text "Record Name". This means that any entry that has no A record or where a record doesn't exist will not be returned. I'm using this code to try and get these records:
Function Get-FailedDNSClientCache{
Invoke-Expression "IPConfig /DisplayDNS" | Select-String -Pattern "-----" -Context 2,1 |
ForEach-Object{
"" + $_.Context.PreContext[1]
"" + $_.Line
"" + $_.Context.Postcontext[0]
"" + $_.Context.PreContext[2]
}
}
The code above gives me some output that looks like this:
pagead.l.doubleclick.net
----------------------------------------
No records of type AAAA
s0-2mdn-net.l.google.com
----------------------------------------
Record Name . . . . . : s0-2mdn-net.l.google.com
www.redditstatic.com
----------------------------------------
No records exist
s0-2mdn-net.l.google.com
----------------------------------------
Record Name . . . . . : s0-2mdn-net.l.google.com
I've been trying all the different filtering methods I have found but I end up returning no data, null fields or I filter out just the text line that starts with "Record Name" but not the entire record. I think the problem is that I need to create each returned record as an object rather than each line but my attempts at that have failed as well. I'd basically like for my output to look like this:
pagead.l.doubleclick.net
----------------------------------------
No records of type AAAA
www.redditstatic.com
----------------------------------------
No records exist
I've seen bits and pieces of my question answered throughout the forum and I have tried most of the solutions provided but I think I am not understanding something fundamental allowing me to put it all together correctly. Please keep in mind I am truly trying to understand what I am doing so the more information the better. TIA
Upvotes: 1
Views: 878
Reputation: 73746
Using a simple regex:
oneliner
([regex]'(?s)(\S+)\n\s+-+\s+(.+?)\n').matches((ipconfig /displayDNS) -join "`n") | ?{ $_.groups[2].value -notlike 'Record Name*' } | %{ $orphans = @{} } { $orphans[$_.groups[1].value] = $_.Groups[2].value } { $orphans }
more readable, using PS2 SortedDictionary
$output = (ipconfig /displayDNS) -join "`n"
$rx = [regex]'(?s)(\S+)\n\s+-+\s+(.+?)\n'
$orphansSorted = [Collections.Generic.SortedDictionary[string,string]]::new()
forEach ($match in $rx.matches($output)) {
$url = $match.groups[1].value
$msg = $match.groups[2].value
if ($msg -notlike 'Record Name*') {
$orphansSorted[$url] = $msg
}
}
$orphansSorted
Output:
Key Value
--- -----
mpa.one.microsoft.com No records of type AAAA
my.router No records exist
onlineconfigservice.ubi.com No records of type AAAA
static3.cdn.ubi.com No records of type AAAA
ubisoft-orbit.s3.amazonaws.com No records of type AAAA
ubisoft-orbit-savegames.s3.amazonaws.com No records of type AAAA
Upvotes: 0
Reputation: 174815
Use Where-Object
to filter on matches where the PostContext[0]
value contains the string "No records":
Function Get-FailedDNSClientCache{
Invoke-Expression "IPConfig /DisplayDNS" | Select-String -Pattern "-----" -Context 2,1 | Where-Object {$_.Context.Postcontext[0] -like "*No records*"} |ForEach-Object {
"" + $_.Context.PreContext[1]
"" + $_.Line
"" + $_.Context.Postcontext[0]
"" + $_.Context.PreContext[2]
}
}
Upvotes: 0
Reputation: 2312
You can use the Get-DnsClientCache cmdlet and pass in the status flag you wish to search for. For example:
Get-DnsClientCache -Status NotExist
Or
Get-DnsClientCache -Status NoRecords
If you don't have this function, you can create your own based on https://gallery.technet.microsoft.com/scriptcenter/ad12dc1c-b0c7-44d6-97c7-1a537b0b4fef
The code for the above cited function is:
Function Get-DNSClientCache{
$DNSCache = @()
Invoke-Expression "IPConfig /DisplayDNS" |
Select-String -Pattern "Record Name" -Context 0,5 |
%{
$Record = New-Object PSObject -Property @{
Name=($_.Line -Split ":")[1]
Type=($_.Context.PostContext[0] -Split ":")[1]
TTL=($_.Context.PostContext[1] -Split ":")[1]
Length=($_.Context.PostContext[2] -Split ":")[1]
Section=($_.Context.PostContext[3] -Split ":")[1]
HostRecord=($_.Context.PostContext[4] -Split ":")[1]
}
$DNSCache +=$Record
}
return $DNSCache
}
Either of these methods will return an object that you can manipulate further as needed.
Upvotes: 1