reverse dns lookup get ip and name results together...

Welcome Forums General PowerShell Q&A reverse dns lookup get ip and name results together...

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
1 month, 4 weeks ago.

  • Author
    Posts
  • #167197

    Participant
    Topics: 5
    Replies: 6
    Points: 57
    Rank: Member

    I'm performing a reverse dns lookup but I only get the name and not the IP used in my results.  I would like to have both so I can match one to one.  Can you help?

    #Reverse IP Lookup
    $IP1 = "10.11.162.15",
    "172.29.65.228"

    $PC = foreach ($p in $IP1)
    {
    [System.Net.Dns]::GetHostEntry($p).HostName | select-object $p, $p.hostname
    }

    $PC

    My select object statement does not appear to be bringing in the data I need and it appears to be from the reverse dns lookup command I'm using.

     

  • #167272

    Participant
    Topics: 1
    Replies: 59
    Points: 328
    Helping Hand
    Rank: Contributor

    you're close...

    foreach ($p in $ip1){
    $pc=([System.Net.Dns]::GetHostEntry($p).HostName)
    write-host "IP = $P, DNS =$PC"}
  • #167278

    Participant
    Topics: 5
    Replies: 6
    Points: 57
    Rank: Member

    Nice and thank you but...

    Is there a way to get it collected for foreach so that I can export to a csv?

    • #167341

      Participant
      Topics: 1
      Replies: 59
      Points: 328
      Helping Hand
      Rank: Contributor

      Yes there is.  There's a cmdlet called export-csv.  Look for some examples online, and then give it a shot.  If you're still having issues post your script and the error(s).

    • #167488

      Participant
      Topics: 5
      Replies: 6
      Points: 57
      Rank: Member

      I appreciate all the help and what was first provided works great but wanted to say that I'm fully aware of the export-csv function but if you look at my first command this is usually how I get all the collected data to then be able to export it.

      I can't do the export inside the foreach because then it will just write over the file with every IP so I usually put the entire foreach to a variable and then pipe to export-csv.  My issue is in getting these variables out are causing me the trouble.  I have done this before with no problem but for some reason this command is giving me trouble.  I'm sure I'm close but for some reason just not getting it.

      $pc=foreach ($p in $ip1){
      $pc1=([System.Net.Dns]::GetHostEntry($p).HostName)
      
      $pc1 | select-object $p, $pc1
      write-host "IP = $P, DNS =$PC1"}
      $PC | export-csv -path c:\file.csv$ 
    • #167521

      Participant
      Topics: 1
      Replies: 59
      Points: 328
      Helping Hand
      Rank: Contributor
      foreach ($p in $ip1){
      $pc=([System.Net.Dns]::GetHostEntry($p).HostName)
      "$pc,$p"|add-content myout3.csv
      }
  • #167557

    Participant
    Topics: 4
    Replies: 269
    Points: 115
    Helping Hand
    Rank: Participant
    $IP1 = '10.11.162.15','172.29.65.228'
    $result = 
    foreach ($ip in $IP1){
        Write-Verbose "Resolving $ip" -Verbose
        $dns = [System.Net.Dns]::GetHostEntry($ip)
        $dns | Select-Object HostName,
        @{n='AddressList';exp={$_.AddressList.IPAddressToString}}
    }
    
    $result | export-csv .\dnsresult.csv -NoTypeInformation
    

You must be logged in to reply to this topic.