Help with rdns powershell script

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
LVL 2
Jorge OcampoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris DentPowerShell DeveloperCommented:
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
Jorge OcampoAuthor Commented:
is it possible to get both forward and reverse script?
Jorge OcampoAuthor 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
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Chris DentPowerShell DeveloperCommented:
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 DeveloperCommented:
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
Jorge OcampoAuthor 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
Chris DentPowerShell DeveloperCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jorge OcampoAuthor 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 DeveloperCommented:
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 DeveloperCommented:
Ignore this bit: "If so, you should be able to swap in you", changed my mind about something :)
Jorge OcampoAuthor Commented:
sounds good ill try to change the input file hostname what should i swap out promise thats last question
Chris DentPowerShell DeveloperCommented:
Hopefully it works. Either way the questions are not a problem :)

Chris
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.