Help with rdns powershell script

Jorge Ocampo
Jorge Ocampo used Ask the Experts™
on
get-content “C:\list\hnames.txt” | ForEach-Object {
[System.Net.Dns]::GetHostbyAddress($_) |
Add-Member -Name IP -Value $_ -MemberType
} | Select IP, HostName | Export-CSV C:\list\RDNS2.csv -NoTypeInformation

need to make it also list the ips that have no record
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chris DentPowerShell Developer
Top Expert 2010

Commented:
This doesn't discard and should give you what you need.
get-content “C:\list\hnames.txt” |
    Select-Object @{n='IP';e={ $_ }}, @{n='Hostname';e={ [System.Net.Dns]::GetHostbyAddress($_) }} |
    Export-CSV C:\list\RDNS2.csv -NoTypeInformation

Open in new window

Chris

Author

Commented:
is it possible to get both forward and reverse script?

Author

Commented:
i want to run a check of input file nomatter if its ip or hostname want this output

hostname/ip   FORWARD DNS CHECK
test1                 192.168.22.01
192.168            ts2
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Chris DentPowerShell Developer
Top Expert 2010

Commented:
Sure, another pass can add that one on.
get-content "C:\list\hnames.txt" |
   Select-Object `
       @{n='IP';e={ $_ }},
       @{n='ReverseLookup';e={ [System.Net.Dns]::GetHostbyAddress($_).HostName }} |
   Select-Object *, @{n='ForwardLookup';e={ if ($_.ReverseLookup) { [System.Net.Dns]::GetHostEntry($_.ReverseLookup).AddressList } }} |
   Export-CSV C:\list\RDNS2.csv -NoTypeInformation

Open in new window

Obviously we don't get to do the forward lookup on the result of the reverse unless the reverse lookup returned something.

Chris
Chris DentPowerShell Developer
Top Expert 2010

Commented:
Ahh sorry, you wish to to get reverse if it's an IP and forward if it's a name?
Get-content "C:\list\hnames.txt"  | ForEach-Object {
    try { $DnsCheck = [System.Net.Dns]::GetHostEntry($_) } catch { }
    if ($?) {
        if ([IPAddress]::TryParse($_, [Ref]$null)) {
           $DnsCheck = $DnsCheck.Hostname
        } else {
           $DnsCheck = $DnsCheck.AddressList
        }
    } else {
        $DnsCheck = $null
    }
      
    [PSCustomObject]@{
        'Hostname/IP' = $_
        'DNS Check'   = $DnsCheck
    }
} | Export-CSV C:\list\RDNS2.csv -NoTypeInformation

Open in new window

Chris

Author

Commented:
so i'm impuitng ip's in my hnames.txt it should work right?

can it returned no reverselookip and no reverse forward lookup instead of leaving blank? possible
PowerShell Developer
Top Expert 2010
Commented:
Yep, absolutely. You might get a slight delay for those that don't have anything, but it'll leave it blank otherwise.

Here's my little test set. The first is mine, the second is google and the third rather explains itself.
'www.indented.co.uk', '216.58.211.164', 'nothingatall' | ForEach-Object {
    try { $DnsCheck = [System.Net.Dns]::GetHostEntry($_) } catch { }
    if ($?) {
        if ([IPAddress]::TryParse($_, [Ref]$null)) {
           $DnsCheck = $DnsCheck.Hostname
        } else {
           $DnsCheck = $DnsCheck.AddressList
        }
    } else {
        $DnsCheck = $null
    }
    [PSCustomObject]@{
        'Hostname/IP' = $_
        'DNS Check'   = $DnsCheck
    }
}

Open in new window

Chris

Author

Commented:
i got the below

IsReadOnly","IsFixedSize","IsSynchronized","Keys","Values","SyncRoot","Count"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","2"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","2"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","2"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","2"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","2"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","2"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","2"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","2"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","2"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","2"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","2"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","2"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collect
Chris DentPowerShell Developer
Top Expert 2010

Commented:
Which version of PowerShell are you running? I rather assumed 4, but that isn't really consistent with 4.

This should be PowerShell 2 and above compatible. If so, you should be able to swap in you
Get-content "C:\list\hnames.txt" | ForEach-Object {
    try { $DnsCheck = [System.Net.Dns]::GetHostEntry($_) } catch { }
    if ($?) {
        if ([IPAddress]::TryParse($_, [Ref]$null)) {
           $DnsCheck = $DnsCheck.Hostname
        } else {
           $DnsCheck = $DnsCheck.AddressList
        }
    } else {
        $DnsCheck = $null
    }
    
    $Return = New-Object PSObject
    Add-Member 'Hostname/IP' -MemberType NoteProperty -Value $_ -InputObject $Return
    Add-Member 'DNS Check' -MemberType NoteProperty -Value $DnsCheck -InputObject $Return
    $Return
} | Export-CSV C:\list\RDNS2.csv -NoTypeInformation

Open in new window

Chris
Chris DentPowerShell Developer
Top Expert 2010

Commented:
Ignore this bit: "If so, you should be able to swap in you", changed my mind about something :)

Author

Commented:
sounds good ill try to change the input file hostname what should i swap out promise thats last question
Chris DentPowerShell Developer
Top Expert 2010

Commented:
Hopefully it works. Either way the questions are not a problem :)

Chris

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial