locked
Re-writing this bit of code to output host name RRS feed

  • Question

  • Hi All,

    I have the code below, it allows me to get IP addresses of host names. The issue I have is that i need the hostname and ip address on thesame line. At the moment, I can only get it on seperate lines. So what I want is

    Hostname | IPaddress

    So its pipe delimeted.

    $a = get-content "C:\temp\list.txt"
    
    foreach ($i in $a )
        {
    			[System.Net.Dns]::GetHostAddresses($i)  | select IPAddressToString -ExpandProperty IPAddressToString
    $i
    			 
    }
    
    

    Tuesday, October 2, 2018 2:46 PM

All replies

  • Here's one way, although it's not the best way.

    $a = 'ibm.com','microsoft.com'
    foreach ($i in $a){
        $i + "`t" + ([System.Net.Dns]::GetHostAddresses($i) | select IPAddressToString -ExpandProperty IPAddressToString)
    }


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Tuesday, October 2, 2018 2:56 PM
  • I would do it this way:


    $a | resolve-dnsname | foreach { "$($_.name)|$($_.ipaddress)" }

    • Edited by JS2010 Tuesday, October 2, 2018 5:36 PM
    Tuesday, October 2, 2018 3:14 PM
  • Or …

    Get-Content C:\temp\list.txt | resolve-dnsname | select name,ipaddress


    \_(ツ)_/

    Tuesday, October 2, 2018 3:26 PM