Link to home
Start Free TrialLog in
Avatar of Erwin Pombett
Erwin PombettFlag for Switzerland

asked on

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_
Avatar of Dan McFadden
Dan McFadden
Flag of United States of America image

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

Dan
ASKER CERTIFIED SOLUTION
Avatar of Dan McFadden
Dan McFadden
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Erwin Pombett

ASKER

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
Can you post some of the data in your hashtable?

Dan
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
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
thank you !!!!!!

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

toshi