8

I am using the following suggestion provided in this link: Experts-Exchange

I am trying to take a server (host name) list and save the host name and IP address in a .csv file.

Using the following Powershell code, I do see the host name but the same IP address, for every server, in the results pane.

$servers = get-content "C:\TEMP\servers.txt"
$serversAndIps = "C:\TEMP\List_of_servers_with_ips.csv"


$results =@()
foreach ($server in $servers) {
  $results =@()
    $result = "" | Select ServerName , ipaddress
    $result.ipaddress = [System.Net.Dns]::GetHostAddresses($server)
  foreach($a in $addresses) {
    "{0},{1}" -f $server, $a.IPAddressToString
  }
    $result.servername = $server
    $results += $result
}
$results | export-csv -NoTypeInformation $serversandips

When I open the .csv file, I get this:

"ServerName","ipaddress"
"Server_name_1","System.Net.IPAddress[]"

If I run this PowerShell script, I can get the host name and the correct IP address in the results pane. I just need to know how to transfer the results to a .csv file.

$servers = get-content "C:\TEMP\servers.txt"
foreach ($server in $servers) {
  $addresses = [System.Net.Dns]::GetHostAddresses($server)
  foreach($a in $addresses) {
    "{0},{1}" -f $server, $a.IPAddressToString
  }
}

Any suggestions?

3
  • Try taking that $results =@() out of the inside of the loop. Commented Apr 24, 2015 at 17:12
  • @mjolinor I took that out and now I get all of the host names, but I still get "Server_Name_1","System.Net.IPAddress[]" Commented Apr 24, 2015 at 17:21
  • 1
    You're storing the [System.Net.Dns]::GetHostAddresses($server) object in an array. If you do a Get-Member, you'll see that your array is storing an object of type System.Net.IPAddress. You will probably need to do something like this instead: $result.ipaddress = ([System.Net.Dns]::GetHostAddresses($server)).IPAddressToString Commented Apr 24, 2015 at 17:55

1 Answer 1

12

Looks like some simple typos at work.

  • $result was being reset inside the in the loop
  • $addresses inside the loop wasn't assigned.
  • $result.ipaddress was not assigned to $a.IPAddressToString for the output object.

Try this:

$servers = get-content "X:\servers.txt"
$serversAndIps ="X:\test.csv"

$results = @()
foreach ($server in $servers)
{
    $result = "" | Select ServerName , ipaddress
    $result.ipaddress = [System.Net.Dns]::GetHostAddresses($server)
    $addresses = [System.Net.Dns]::GetHostAddresses($server)

    foreach($a in $addresses) 
    {
        "{0},{1}" -f $server, $a.IPAddressToString
        $result.ipaddress = [System.Net.Dns]::GetHostAddresses($server)
    }

    $result.servername = $server
    $result.ipaddress = $a.IPAddressToString
    $results += $result
}

$results | export-csv -NoTypeInformation $serversandips
Sign up to request clarification or add additional context in comments.

2 Comments

We all have to start somewhere. Best of luck.
If the IP is not reachable then script throws exception on lines 8,15 and 25, so they should be wrapped in try-catch like here

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.