Link to home
Create AccountLog in
Avatar of Jorge Ocampo
Jorge OcampoFlag 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
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of Jorge Ocampo

ASKER

is it possible to get both forward and reverse script?
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
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
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
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
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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
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
Ignore this bit: "If so, you should be able to swap in you", changed my mind about something :)
sounds good ill try to change the input file hostname what should i swap out promise thats last question
Hopefully it works. Either way the questions are not a problem :)

Chris