update ipaddresses to hostnames in existing csv

Welcome Forums General PowerShell Q&A update ipaddresses to hostnames in existing csv

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

 
Participant
2 months ago.

  • Author
    Posts
  • #166660

    Participant
    Topics: 1
    Replies: 4
    Points: 20
    Rank: Member

    Hi there, what I'd like to achieve is the following: I've an csv report with a column of ipaddresses. I'd like to resolve all the ipaddresses  to hostnames and update the existing report with that information.

     

    So far I've written this what gives me only the list with host names..:

    $data = Import-Csv "C:\temp\test.csv"| Select-Object "Host"
    ForEach ($item in $data)
    {
    $impactedhost = $item.("Host")
    Resolve-DnsName -Name $impactedhost -Server 8.8.8.8 |select NameHost
    
    }

    When I do a |export-csv afterwards I only get only the column of host names..

    What should I do next?

     

    Thanks in advance

     

  • #166669

    Participant
    Topics: 1
    Replies: 1530
    Points: 2,591
    Helping Hand
    Rank: Community Hero

    That should be enough actually ...

    Import-Csv -Path 'C:\temp\test.csv' | 
        Select-Object -Property *,@{ 
            Name = 'HostName';
            Expression = { Resolve-DnsName -Name $_.Host -Server 8.8.8.8 | Select-Object -ExpandProperty NameHost }
        }

    ... untested ... and depending on the amount of hosts that could take a while ... 😉

  • #166675

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

    You're really close.

     

    Also, wrap your code in

    
    

    tags, it makes it more readable.

    Once you have the IP, you want to pipe it to export-csv (or even add-content to blah.csv).  I tend to NOT want to add to the original file in case something goes wrong.  Something like this will work:

     

    ForEach ($item in $data)
    {
    $ip=(Resolve-DnsName $item |? {$_.type -eq "A"}).ipaddress
    "$item,$ip" |add-content "C:\test\test3.csv"
    }
  • #166702

    Participant
    Topics: 1
    Replies: 4
    Points: 20
    Rank: Member

    Great! Thanks all for suggestions!

  • #166753

    Participant
    Topics: 1
    Replies: 4
    Points: 20
    Rank: Member

    That should be enough actually ...

    PowerShell
    5 lines

     

    1
    2
    3
    4
    5
    Import-Csv Path 'C:\temp\test.csv' |
    Select-Object Property *,@{
    Name = 'HostName';
    Expression = { Resolve-DnsName Name $_.Host Server 8.8.8.8 | Select-Object ExpandProperty NameHost }
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    ... untested ... and depending on the amount of hosts that could take a while ... 😉

    This one does basically the trick but doesn't update the csv sheet though..

  • #166765

    Participant
    Topics: 1
    Replies: 1530
    Points: 2,591
    Helping Hand
    Rank: Community Hero

    This one does basically the trick but doesn't update the csv sheet though..

    I wanted to leave some space for you to shine. 😉 ... my code example is meant as a suggestion – feel free to tweak it to your needs. You could assign the whole code snippet to a variable and export it to a new CSV file or – if you like – to the same CSV file using the parameter -Force.

  • #166789

    Participant
    Topics: 1
    Replies: 4
    Points: 20
    Rank: Member

    You could assign the whole code snippet to a variable and export it to a new CSV file or – if you like – to the same CSV file using the parameter -Force.

    That's the whole idea..

    [/quote]

  • #166891

    Participant
    Topics: 1
    Replies: 4
    Points: 20
    Rank: Member

     

    DNS name contains an invalid character
    At line:4 char:6
    + $ip=(Resolve-DnsName $item |? {$_.type -eq "A"}).ipaddress

You must be logged in to reply to this topic.