In powershell i have a function that return me the Ip after an URL, if i use it an loop i always receive an error, what am i doing wrong ?

Hello, i have a function that return as expected the ip after an URL when i call it on it's own.

but if i call my function from within a foreach loop my code ended into the catch block.

here 's the function ---------------------------------------------------------------
 function GetIpAdresse
{
    Param([String] $aServerName)

    try{
        $ip = [System.Net.Dns]::GetHostAddresses($aServerName)

        $ipFinal = $ip[0].IPAddressToString

        return $ipFinal
    }
    catch{
        [system.exception]
        write-host "ko pour url -> ip" + $_
    }
    finally
    {}
}

---------------------------------------------------------------
when i call it from

......
foreach($theServerName in $allServers.Values)
{
      $tempIP = GetIpAdresse $theServerName.ToString()
}
.......


how can i deal with this, i can not find a solution.

thank you in advance for your help

toshi_
Erwin PombettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan McFaddenSystems EngineerCommented:
Where are you getting the info in "$allServers.Values" from?  What does the data look like?

Dan
0
Dan McFaddenSystems EngineerCommented:
The function works for me.  I have a text file with URLs in it.

Contents of allServers.txt
www.cnn.com
www.microsoft.com
tomshardware.com
bbc.co.uk

Open in new window


The script looks like this:
function GetIpAdresse()
{
	Param([String] $aServerName)
	try
	{
		$ip = [System.Net.Dns]::GetHostAddresses($aServerName)
		return $ip
	}
	catch
	{
		[system.exception]
		write-host "ko pour url -> ip" + $_
	}
	finally
	{}
}

$allServers = Get-Content allservers.txt

foreach($theServerName in $allServers)
{
	Write-Host "working on hostname : " $theServerName
	$tempIP = GetIpAdresse $theServerName
	Write-Host $tempIP
}

Open in new window


I dropped the $ipFinal because some hostnames can return more than 1 IP.  For example, the BBC returns 3 IPs.

Also, why do you need to "ToString" multiple times?

Dan
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Erwin PombettAuthor Commented:
hello Dan !
thank you for your reply,

$allServers is a hashtable

$allServers.Values is because i want to loop only over the ...values ;)

my values are string. to force that i added the "toString()" method....but nothing changes...

thank you for further help
0
Top Threats of Q1 & How to Defend Against Them

WEBINAR: Join WatchGuard CTO and our Threat Research Team on Aug. 2nd to hear the findings from our Q1 Internet Security Report! Learn more about the top threats detected in the first quarter and how you can defend your business against them!

Dan McFaddenSystems EngineerCommented:
Can you post some of the data in your hashtable?

Dan
0
Erwin PombettAuthor Commented:
this is when i'm debugging :

i have the correct value

[DBG]: PS C:\Users\pombette\Documents\WindowsPowerShell\Modules>> $aServerName
"thetargetserer.domi.dom.do"  <- replaced for the question

[DBG]: PS C:\Users\pombette\Documents\WindowsPowerShell\Modules>> [System.Net.Dns]::GetHostAddresses($aServerName)
Exception lors de l'appel de « GetHostAddresses » avec « 1 » argument(s) : « Hôte inconnu »
Au caractère Ligne:1 : 1
+ [System.Net.Dns]::GetHostAddresses($aServerName)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SocketException
0
Erwin PombettAuthor Commented:
Name                           Value                                                                                      
----                           -----                                                                                      
eidIdp02                       "anidp02.aspfr.cl"                                                                      
eidAgentEdu                 "aserverid01.edu.tt.cl"                                                                    
eidAgentAdNet02        "another02.xt.net.tt.cl"  

i changed the values, you wont find anything for these servers...but it's the display i have for the hashtable
0
Erwin PombettAuthor Commented:
thank you !!!!!!

by reading your file i noticed the difference.  without quoting things everything is OK !!

toshi
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.