![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Hostname to IP translation Hey all, i've been looking around for a PS script that will resolve hostnames to IP addresses. I've found a couple of examples out on the net but can't seem to get them to work properly. I'm a newbie to PS and scripting in general. Essentially what i would like to accomplish would be for PS to read through my .txt list of hostnames, resolve their IP's, output to excel or ..txt file. I found an example written by Lee Desmond at: http://www.leedesmond.com/weblog/?p=189 However i can't get PS to run it, "Unexpected token '{' in expression or statement. At C:\....file.ps1:7 char:10 + $sb { <<<< Question being, how can I either fix this script to work for me, or create a new script with the cmdlet [System.Net.Dns]::GetHostAddresses("hostname") if that is a cmdlet.... I'm a newbie to PS and scripting in general. Any help is greatly appreciated! If i am way out of my league, please re-direct me to a better forum??! Thanks! |
My System Specs![]() |
| | #2 (permalink) | ||||||||||||
| Guest | Re: Hostname to IP translation "Joe Freedom" <JoeFreedom@xxxxxx> wrote in message news:BB6DF734-C0CB-4B23-9D54-E57790A6ABE2@xxxxxx
GetIpAddress.ps1 param($hostnames = $(throw "hostname is required")) foreach ($hostname in $hostnames) { $ips = [System.Net.Dns]::GetHostAddresses($hostname) $OFS = "," "Host: $hostname, address: $ips" } -- Keith | ||||||||||||
My System Specs![]() | |||||||||||||
| | #3 (permalink) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Guest | Re: Hostname to IP translation On Oct 8, 10:11 am, "Keith Hill [MVP]" <r_keith_h...@xxxxxx_no_spam_I> wrote:
get-content input.txt | % { [System.Net.Dns]::GetHostAddresses($_) | select IPAddressToString | export-csv ips.csv this assumes your input list is input.txt and you are exporting to ips.csv, which you could easily open in excel. Andy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | #4 (permalink) | ||||||||||||
| Guest | Re: Hostname to IP translation Keith gave you a script but you said something's I think you may want clarification on. "create a new script with the cmdlet [System.Net.Dns]::GetHostAddresses("hostname")" System.Net.Dns is not a cmdlet... it is a .NET class and GetHostAddress is a static method of that class. I would recommend googling System.Net.Dns and see the cool stuff you can do with it. At first .NET can be a bit daunting, but it does it get easier. "Joe Freedom" <JoeFreedom@xxxxxx> wrote in message news:BB6DF734-C0CB-4B23-9D54-E57790A6ABE2@xxxxxx
| ||||||||||||
My System Specs![]() | |||||||||||||
| | #5 (permalink) | ||||||||||||
| Guest | Re: Hostname to IP translation Joe Freedom wrote:
here's what I did. # remove slashes on UNC paths if present, does nothing if not $hostPartial = $_.'Host Address' -replace "^\\\\" If ( $hostPartial -match "^\d" ) { $DnsResult = [System.Net.Dns]::GetHostByAddress($hostPartial) } Else { $DnsResult = [System.Net.Dns]::GetHostByName($hostPartial) } The resulting object $DnsResult is of type IPHostEntry which contains both the name (HostName property) and the address or addresses (AddressList property--it's an array) # If you expect only one IP, this pulls it from the array as a string. $DnsResult.AddressList[0].IpAddressToString -- Hal Rottenberg Blog: http://halr9000.com Webmaster, Psi (http://psi-im.org) Co-host, PowerScripting Podcast (http://powerscripting.net) | ||||||||||||
My System Specs![]() | |||||||||||||
| | #6 (permalink) |
| Guest | RE: Hostname to IP translation All, Thanks for you timely responses! I appreciate you taking the time to help me out. I have accepted all of your solutions because I can't seem to choose one over the other. I ended up with: #testip.ps1 param($hostnames = $(get-content hostnames.txt)) foreach ($hostname in $hostnames) { $ips = [System.Net.Dns]::GetHostAddresses($hostname) "Host: $hostname, address: $ips" } This works great in powershell but now what & where do i add code (export command? with encoding parameter?) to properly output to an excel document without the first cell in my excel document reading: #TYPE System.Management.Automation.PSCustomObject? Thanks again for everyones help |
My System Specs![]() |
| | #7 (permalink) | ||||||||||||
| Guest | Re: Hostname to IP translation You'll need to prepare an object for exporting to a CSV using export-csv. In preperation for that, be aware that $ips will contain a _collection_ of System.Net.IPAddress objects, not an actual string with the IP address(es). Try the following code: param($hostnames = $(get-content hostnames.txt)) $hostnames | foreach-object { $ips = [System.Net.Dns]::GetHostAddresses($_) $obj = 1 | Select-Object Hostname,Address $obj.hostname = $_ $obj.Address = $ips | % { $_.ipaddresstostring } | join-string -sep "," write-output $obj } | export-csv "exportedfile.csv" -notype -notype removes the #type line. I modified the structure to use foreach-object instead of foreach. -- Gaurhoth http://thepowershellguy.com/blogs/gaurhoth/ "Joe Freedom" <JoeFreedom@xxxxxx> wrote in message news:313512AB-7D07-4FEE-BB41-7A7735A88D99@xxxxxx
| ||||||||||||
My System Specs![]() | |||||||||||||
| | #8 (permalink) |
| Guest | Re: Hostname to IP translation Gaurhoth, This is the error i receive from powershell. The excel document has "Length" in cell 1A, then "1" in 2A, "1" in 4A, "1" in cell 6A, etc. The term 'join-string' is not recognized as a cmdlet, function, operable progra m, or script file. Verify the term and try again. Any ideas? |
My System Specs![]() |
| | #9 (permalink) | ||||||||||||
| Guest | Re: Hostname to IP translation Try wrapping the foreach in an array... the process the whole thing and then returns an array. @(foreach ($hostname in $hostnames) { $ips = [System.Net.Dns]::GetHostAddresses($hostname) "$hostname,$ips" }) | out-file c:\temp\myfile.csv -enc ASCII "Joe Freedom" <JoeFreedom@xxxxxx> wrote in message news:313512AB-7D07-4FEE-BB41-7A7735A88D99@xxxxxx
| ||||||||||||
My System Specs![]() | |||||||||||||
| | #10 (permalink) | ||||||||||||
| Guest | Re: Hostname to IP translation My bad... join-string is from PSCX. I usually test my public posts to make sure I don't use 3rd party commands -- but I failed this time. You can use the .NET [string] class to do pretty much the same thing. Try this instead: param($hostnames = $(get-content hostnames.txt)) $hostnames | ForEach-Object { $ips = [System.Net.Dns]::GetHostAddresses($_) $obj = 1 | Select-Object Hostname,Address $obj.hostname = $_ $obj.Address = [string]::join(",",$ips) write-output $obj } | export-csv "exportedfile.csv" -notype Gaurhoth "Joe Freedom" <JoeFreedom@xxxxxx> wrote in message news:B7BFF338-7278-474A-8BA6-CBFF3B838852@xxxxxx
| ||||||||||||
My System Specs![]() | |||||||||||||
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| SMTP need fully qualified hostname | David | Vista mail | 0 | 03-26-2008 05:07 AM |
| Extract hostname from distinguished name | cmyers | PowerShell | 11 | 02-07-2008 08:44 AM |
| Invalid Hostname | Oscette | Vista networking & sharing | 0 | 01-28-2007 01:34 AM |
| Getting hostname in Windows Powershell | =?Utf-8?B?YnJhdG8=?= | PowerShell | 4 | 08-14-2006 04:16 PM |