Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Hostname to IP translation

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 10-08-2007   #1 (permalink)
Joe Freedom
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 SpecsSystem Spec
Old 10-08-2007   #2 (permalink)
Keith Hill [MVP]
Guest


 

Re: Hostname to IP translation

"Joe Freedom" <JoeFreedom@xxxxxx> wrote in message
news:BB6DF734-C0CB-4B23-9D54-E57790A6ABE2@xxxxxx
Quote:

> 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??!
You're in the right place. Try this in a script file called something like
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 SpecsSystem Spec
Old 10-08-2007   #3 (permalink)
ASCHNEIDER146
Guest


 

Re: Hostname to IP translation

On Oct 8, 10:11 am, "Keith Hill [MVP]"
<r_keith_h...@xxxxxx_no_spam_I> wrote:
Quote:

> "Joe Freedom" <JoeFree...@xxxxxx> wrote in message
>
> news:BB6DF734-C0CB-4B23-9D54-E57790A6ABE2@xxxxxx
>
>
>
>
>
Quote:

> > 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.
>
Quote:

> > I found an example written by Lee Desmond at:
> >http://www.leedesmond.com/weblog/?p=189
>
Quote:

> > However i can't get PS to run it, "Unexpected token '{' in expression or
> > statement. At C:\....file.ps1:7 char:10
> > + $sb { <<<<
>
Quote:

> > 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.
>
Quote:

> > Any help is greatly appreciated!
> > If i am way out of my league, please re-direct me to a better forum??!
>
> You're in the right place. Try this in a script file called something like
> GetIpAddress.ps1
>
> param($hostnames = $(throw "hostname is required"))
>
> foreach ($hostname in $hostnames) {
> $ips = [System.Net.Dns]::GetHostAddresses($hostname)
> $OFS = ","
> "Host: $hostname, address: $ips"
>
> }
>
> --
> Keith- Hide quoted text -
>
> - Show quoted text -
You can also do something like this:


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 SpecsSystem Spec
Old 10-08-2007   #4 (permalink)
Brandon Shell
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
Quote:

> 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 SpecsSystem Spec
Old 10-08-2007   #5 (permalink)
Hal Rottenberg
Guest


 

Re: Hostname to IP translation

Joe Freedom wrote:
Quote:

> 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
For grins, if you ever find you need to accept IP addresses AND hostnames,
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 SpecsSystem Spec
Old 10-08-2007   #6 (permalink)
Joe Freedom
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 SpecsSystem Spec
Old 10-08-2007   #7 (permalink)
Gaurhoth
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
Quote:

> 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 SpecsSystem Spec
Old 10-08-2007   #8 (permalink)
Joe Freedom
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 SpecsSystem Spec
Old 10-08-2007   #9 (permalink)
Brandon Shell
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
Quote:

> 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 SpecsSystem Spec
Old 10-08-2007   #10 (permalink)
Gaurhoth
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
Quote:

>
>
> 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 SpecsSystem Spec
Closed Thread

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


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51