Powershell – Get server name and IP from text list

Question:

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.

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

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.

Any suggestions?

Answer:

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:

Source:

Powershell – Get server name and IP from text list by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply