Avatar of Jorge Ocampo
Jorge Ocampo
Flag for United States of America asked on

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
Powershell

Avatar of undefined
Last Comment
Chris Dent

8/22/2022 - Mon
Chris Dent

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 Ocampo

ASKER
is it possible to get both forward and reverse script?
Jorge Ocampo

ASKER
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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Chris Dent

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 Dent

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 Ocampo

ASKER
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Chris Dent

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jorge Ocampo

ASKER
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 Dent

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 Dent

Ignore this bit: "If so, you should be able to swap in you", changed my mind about something :)
Your help has saved me hundreds of hours of internet surfing.
fblack61
Jorge Ocampo

ASKER
sounds good ill try to change the input file hostname what should i swap out promise thats last question
Chris Dent

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

Chris