John
John

Reputation: 41

Exception calling "Parse" for IP address - Why?

So i'm updating something. It had previously used whoami.akamai.net but that's being replaced by whoami.ds.akahelp.net (and/or the protocol specific whoami.ipv4.akahelp.net and whoami.ipv6.akahelp.net). So need to update. Anyway when I do this

$ips = (((Resolve-DnsName 'whoami.ds.akahelp.net' -QuickTimeout -Type txt).Strings) -replace 'ns','').Trim();
$ips = [System.Net.IPAddress]::Parse($ips)

It generates an error - Exception calling "Parse" with "1" argument(s): "An invalid IP address was specified." - but the IP is valid. Then I noticed there is what seems to be an extra line before the IP so that would account for the exception but I can not get rid of the line. I expected the output to be just the IP address. Anyway, I can not parse the IP, and the extra line before the IP renders the output useless where it feeds some other code because its being detected as an invalid IP in the rest of the code also when its really seeing that extra line (I think). So what am I doing wrong here?

Upvotes: 1

Views: 482

Answers (2)

Jan
Jan

Reputation: 25

I think this should do it
$ips = (Resolve-DnsName 'whoami.ds.akahelp.net' -QuickTimeout -Type txt).Strings[1] $ips = [System.Net.IPAddress]::Parse($ips)

Upvotes: 0

Jacob
Jacob

Reputation: 1192

The returned object is an array, you need to specify which item in the array you need.

PS C:\Users\jacob> $ips

81.134.99.118

PS C:\Users\jacob> $ips.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                           
-------- -------- ----                                     --------                                                                                                           
True     True     Object[]                                 System.Array                                                                                                       



PS C:\Users\jacob> $ips[1]
81.134.99.118

PS C:\Users\jacob> $ips[1].GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                           
-------- -------- ----                                     --------                                                                                                           
True     True     String                                   System.Object                                                                                                      

So with the code you have, to specify the item in the array you need, you could write it as follows:

$ips = (Resolve-DnsName 'whoami.ds.akahelp.net' -QuickTimeout -Type txt).Strings[1].Trim()
$ips = [System.Net.IPAddress]::Parse($ips)

Upvotes: 2

Related Questions