Powershell IP manipulation problem

Hi, 

I am having a problem reversing my public IP address that I retrieve from a website. I wrote a script that finds my computer's public IP address and then tries to reverse it so I can use it for other things. The script is as follows: 

$IPAddress = (Invoke-WebRequest ifconfig.me/ip).Content

# Now lets split the address into its parts by using the dot ('.')
$IPAddressParts = $IPAddress.Split('.')

$RIPAddress = $IPAddressParts[3] + "." + $IPAddressParts[2] + "." + $IPAddressParts[1] + "." + $IPAddressParts[0]

"This is reversed $RIPAddress"

The problem is that the output it gives me is this:

This is reversed 143
.88.151.81

There seems to be a line break after 143.  and I don't know why. If I put the IP address manually as a string "81.151.88.143" without using the website, everything works fine.

When if I print the IP right after the website resolves it, it also looks fine and there are no line breaks or anything. As you can see, it reverses it just fine and the problem is only with 143. This happens with any website I have used for retrieving my IP.

The line break seems to be added by the reversing process but it only happens when the IP is parsed from any website.

It seems to be related to sanitising the HTML part to plain text. I have tried parsing to string but I get more of the same. There doesn't seem to be any HTML code contained in the variable after fetching the IP. Can anyone help me with this?

August 4th, 2014 10:13pm

You can use .Trim() to trim off the whitespace before you do the split:

$IPAddress = (Invoke-WebRequest ifconfig.me/ip).Content

# Now lets split the address into its parts by using the dot ('.')
$IPAddressParts = $IPAddress.Trim().Split('.')

$RIPAddress = '{3}.{2}.{1}.{0}' -f $IPAddressParts

$RIPAddress

20.161.255.199
Free Windows Admin Tool Kit Click here and download it now
August 4th, 2014 10:28pm

Hi,

This seems to work for me:

$IPAddress = Invoke-WebRequest ifconfig.me/ip

$IPAddressParts = $IPAddress.Content.Split('.')

$RIPAddress = "$($IPAddressParts[3].Trim()).$($IPAddressParts[2]).$($IPAddressParts[1]).$($IPAddressParts[0])"

"This is reversed $RIPAddress"
August 4th, 2014 10:31pm

Worked like a dream. I really appreciate it.
  • Marked as answer by headamage Monday, August 04, 2014 10:35 PM
  • Unmarked as answer by headamage Monday, August 04, 2014 10:35 PM
Free Windows Admin Tool Kit Click here and download it now
August 4th, 2014 10:33pm

This works too! Thanks. I have been struggling with it all day.
August 4th, 2014 10:35pm

Cheers, you're very welcome.
Free Windows Admin Tool Kit Click here and download it now
August 4th, 2014 10:38pm

ran it from Windows 8 and it didn't work for me, how is it ran?

July 27th, 2015 9:27pm

Topic is closed - please open your own topic with a clear question.
Free Windows Admin Tool Kit Click here and download it now
July 27th, 2015 10:29pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics