Getting IP addresses for hostnames using nslookup in Powershell -


i new powershell scripting , need write 1 script. have list of server hostnames , need ip addresses of servers , write results in file.

the hostnames 1 column in excel spreadsheet can format them (csv, simple txt file 1 hostname per line etc.).

i format output way there hostname of server , ip address per line (so there multiple lines in case server has more 1 ip).

so far have been using simple text file hostname per line, output in ps unable distinguish server ip address for.

$servers = get-content "path_to_the_file"  foreach ($server in $servers) { [system.net.dns]::gethostaddresses($server) } 

so wondering loading hostnames csv file , printing hostnames , related ip addresses csv again, unsure how.

i investigating possibility capture required information (hostname , ip) running nslookup $server in foreach.

could give me hand?

thank you.

combining hostnames addresses can done within loop. host can have multiple ip addresses, need take accout too. create loop. so,

$servers = get-content "path_to_the_file" foreach ($server in $servers) {   $addresses = [system.net.dns]::gethostaddresses($server)   foreach($a in $addresses) {     "{0},{1}" -f $server, $a.ipaddresstostring   } } 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -