title: Powershell - Get server name and IP from text list
body: <p>I am using the following suggestion provided in this link:
  <a href="http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_27722701.html" rel="noreferrer">Experts-Exchange</a></p>
  
  <p>I am trying to take a server (host name) list and save the host name and IP address in a .csv file.</p>
  
  <p>Using the following Powershell code, I do see the host name but the same IP address, for every server, in the results pane. </p>
  
  <pre><code>$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
  </code></pre>
  
  <p>When I open the .csv file, I get this:</p>
  
  <pre><code>"ServerName","ipaddress"
  "Server_name_1","System.Net.IPAddress[]"
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>$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
    }
  }
  </code></pre>
  
  <p>Any suggestions?</p>