Getting External IP via PowerShell

2

I would just like to Get External IP via Powershell Just IP address no header or anything.

I try many things such as

(Invoke-WebRequest ifconfig.me/ip).Content

but it has this extra second line which doesn't do good for me.

I also tried.

(Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE) | %{$_.ipaddress[0]}

But it doesn't work for me because I am behind the router.

Thanks.

Function IPV()
{
$IPCHK = ((Invoke-WebRequest ifconfig.me/ip).Content.Trim())
$IPCHK | Out-FIle 'CHKIP.txt'
}
$CurrentIP = ((Invoke-WebRequest ifconfig.me/ip).Content.Trim())
$PreviousIP = Get-Content 'CHKIP.txt'

IF($PreviousIP -eq ((Invoke-WebRequest ifconfig.me/ip).Content.Trim()))
    {
        $PreviousIP
        }
ELSE {
       ##SEND EMAIL SCRIPT
        IPV #RUN CHECK IP COMMAND AGAIN.
}

user206168

Posted 2014-08-27T19:12:29.197

Reputation: 376

Can you explain why you need this info? Trying to find a server's external IP address is really tricky. – splattered bits – 2014-08-28T03:33:51.567

2I want to run this script which will check if my external IP changed. if it does then it will send me notification on my iPhone. – user206168 – 2014-08-28T11:31:19.283

Answers

4

You already have your answer. You just want to get rid of the additional line—nothing is forcing you to use

(Invoke-WebRequest ifconfig.me/ip).Content

as is. Instead, you could use this:

(Invoke-WebRequest ifconfig.me/ip).Content.Trim()

The String.Trim method “removes all leading and trailing white-space characters from the current String object.”

Daniel B

Posted 2014-08-27T19:12:29.197

Reputation: 40 502

It still doesn't compare it correctly. Plz check my posted script, if i run this script it still has extra line. thanks – user206168 – 2014-08-27T20:13:30.720

Trim() the output of Get-Content, just to be sure. Also, use your $CurrentIP variable. ;) – Daniel B – 2014-08-27T20:17:59.777

so it would be Get-Content.Trim()? – user206168 – 2014-08-27T20:19:30.667

1No, it works like Invoke-WebRequest. You put parentheses around it and the .Trim() after that. But I recommend posting a new question, because all this deviates too much from this question’s original focus. – Daniel B – 2014-08-27T20:21:31.787

Plz See My solution. – user206168 – 2014-08-28T15:48:52.630

2

One method using OpenDNS.

With Resolve-DnsName CmdLet available in Powershell 4 in Windows 8.1 / Server 2012 or later

$(Resolve-DnsName -Name myip.opendns.com -Server 208.67.222.220).IPAddress

Or in earlier Windows versions with just plain nslookup

$my_ip = ((& "nslookup" "myip.opendns.com" "208.67.222.220") |select -last 2)[0].Trim("Address:").Trim()

Zoredache

Posted 2014-08-27T19:12:29.197

Reputation: 18 453

Sorry? Why did you add a comment with only the command I suggested? – Zoredache – 2014-08-27T20:19:00.723

Sorry Pasted wrong Resolve-DnsName : The term 'Resolve-DnsName' is not recognized as the name of a cmdlet, function, – user206168 – 2014-08-27T20:23:01.760

Ah, I think it is one of the Win 8+ Commandlets. I am guessing you are on Windows 7? – Zoredache – 2014-08-27T20:24:21.303

No I am using Windows 8. – user206168 – 2014-08-27T20:26:34.207

0

Just one small (but important) addendum to Deniel's answer:

(Invoke-WebRequest -UseBasicParsing ifconfig.me/ip).Content.Trim()

Otherwise, on some machines you may face the error like this:

Explorer engine is not available, or Internet Explorer's first-launch configuration is not complete. Specify the UseBasicParsing parameter and try again.

Yury Schkatula

Posted 2014-08-27T19:12:29.197

Reputation: 150

0

Short answer

For the PowerShell newbs (where .NET methods may look a bit scary), try the following (case insensitive) code:

Invoke-WebRequest -Uri "http://icanhazip.com" | Select-Object -ExpandProperty "Content"

The above is the simplest (in my humble opinion) way to obtain one's external/public/WAN IP address via PowerShell (also known as "PoSh").

Long answer

This technique relies on a helpful website (there are several, in fact) that returns only the public IP address of the connection that you're connecting from.

We use the handy Invoke-WebRequest cmdlet, which gives us a bunch of info about the website. In this case though, we actually want the raw HTML of the webpage (which happens to be just your public IP address). The Select-Object cmdlet will allow us to do just that, and discard the rest of the information returned.

Having said that, as the other answerers have highlighted, this approach actually returns, not one, but two lines:

One line containing the IP address, and one line containing white space.

If this causes an issue (for simple requirements, such as printing the IP address to the screen, this is not likely to cause an issue), you will need to delve into the aforementioned .NET method world, ever so slightly.

Again, case insensitive:

( Invoke-WebRequest -Uri "http://icanhazip.com" | Select-Object -ExpandProperty "Content" ).Trim( "" )

The Trim() method will "trim" anything matching the characters specified in between the double-quote marks (which are actually optional in this case) from the beginning or the end of the returned data (or string, technically speaking). Being that in this example there is actually nothing between the double-quote marks, PowerShell will interpret this as "white space" and trim anything from the start or end that's considered white space.

The initial pair of brackets is required when using .NET methods with PowerShell cmdlets, and the second pair of brackets is part of the method itself.

This combination of PowerShell cmdlets and .NET method leaves you with just the IP address you seek.

Bren0man

Posted 2014-08-27T19:12:29.197

Reputation: 1

0

I was able to fix my problem by only selecting the first line from the text file. I was not successfuly able use TRIM and remove the empty line.

Using this link I did following.

Function IPV()
{
$IPCHK = ((Invoke-WebRequest ifconfig.me/ip).Content)
$IPCHK.TRIM() # Just Like tht.
$IPCHK > 'CHKIP.txt'
}
$CurrentIP = ((Invoke-WebRequest ifconfig.me/ip).Content.Trim())
$PreviousIP = Get-Content 'CHKIP.txt' | SELECT -First 1   #ONLY Selects First Line.

IF($PreviousIP -eq ((Invoke-WebRequest ifconfig.me/ip).Content.Trim()))
    {
        $PreviousIP
        }
ELSE {
        #DO YOUR STUFF :))
        IPV #RUN CHECK IP COMMAND AGAIN.
}

Hope it helps.

user206168

Posted 2014-08-27T19:12:29.197

Reputation: 376

You're still not using your $CurrentIP variable. :P – Daniel B – 2014-08-28T16:31:49.557