0

I have a function to get my external IP from ifconfig.me. It returns what looks like a plain string from "http://ifconfig.me/ip"

function Get-ExternalIP
    {
    $url = "http://ifconfig.me/ip"
    $webClient = new-object System.Net.WebClient 
    $ip = $webClient.downloadstring($url)
    return $ip
    }

This successfully returns what looks like an IP Address, however its not encoded in a way that it is properly handled by powershell.

$ip = Get-ExternalIP
$ip -as [ipaddress]

Fails.

All the functions I'm attempting to do with this string are failing. I'm importing a different IP as a string from a text file and comparing the two is failing even if they are 'identical'.

It gets worse, when I attempt to write it out to a text file I'm getting extra line breaks and a hex heditor shows loads of extra data thats not getting shown.

enter image description here Top is the 'bad data', bottom is an example of how I would expect it to look.

I'm assuming that this is due to the encoding of the text object I'm extracting, but I'd like to understand how i can find out what the encoding is and re-encode it in a way that I can work with. I'm sure there is a simple cast(?) I can do? But I don't know how I would go about finding that out.

4
  • 1
    The "extra" data are byte order marks. Commented Oct 6, 2014 at 12:01
  • 1
    $ip.trim() -as [ipaddress] returns TRUE ! The value has an extra 'empty' cahr at the end. Commented Oct 6, 2014 at 12:09
  • @vonPryz the two characters at the beginning of the string are BOM, but if you look at the ret of the data there are extra Commented Oct 6, 2014 at 13:32
  • 1
    @Patrick The BOM as well as the additional null characters between the dots and digits come from saving the string to a file. Unless you specify the encoding explicitly, writing to a file in PowerShell defaults to Unicode (little endian UTF-16 encoding to be precise), which uses 2 bytes per character. Commented Oct 6, 2014 at 20:23

2 Answers 2

1

I would use ASCII for your text output format

$ip = "255.255.255.255"
$ip | out-file -encoding ascii -Filepath C:\ascii_ip.txt

fyi, your test.txt image is likely in unicode format. Compare that against this and you'll see. The secret is in the BOM (byte order mark) "FF FE" which is like a file header for text files.

$ip | out-file -encoding unicode -Filepath C:\unicode_ip.txt
Sign up to request clarification or add additional context in comments.

1 Comment

That you, I'd have liked to mark both answers as correct but @Ansgar's answer fixed the most annoying issue, but your answer has also been very helpful, thank you.
0

Your problem is caused by a trailing newline in the string returned by the website. The error becomes apparent when you cast the string to [ipaddress] instead of using the -as operator:

PS C:\> $ip = Get-ExternalIP
PS C:\> $ip
192.168.23.42

PS C:\> $ip -as [ipaddress]
PS C:\> [ipaddress]$ip
Cannot convert value "192.168.23.42
" to type "System.Net.IPAddress". Error: "An invalid IP address was specified."
At line:1 char:1
+ [ipaddress]$ip
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastParseTargetInvocation

Note that the closing double quote after the IP address is on the next line in the error message.

Trim() the string before returning it and the problem will disappear:

function Get-ExternalIP {
  $url = "http://ifconfig.me/ip"
  $webClient = new-object System.Net.WebClient 
  $ip = $webClient.downloadstring($url)
  return $ip.Trim()
}

1 Comment

I could have sworn I'd tried with .trim() already but I've tested your version of that function and its worked properly this time. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.