0

I ran the cmdlet on the site ifconfig.me:

Invoke-WebRequest ifconfig.me

For the content, I only got my IP address.

Why don't I get the other HTML content on the page, such as the user agent fields, language, etc?

For example,

Invoke-WebRequest runescape.com

returned all of the HTML on the page.

1 Answer 1

3

It appears that ifconfig.me is sensitive to the User-Agent string in a request.

By default, per Microsoft Learn on Invoke-WebRequest, the User-Agent string sent is “similar to Mozilla/5.0 (Windows NT 10.0; Microsoft Windows 10.0.15063; en-US) PowerShell/6.0.0 with slight variations for each operating system and platform”.

I tried this using Chrome on Windows, and copied the User-Agent string from the result (Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36).

I then did Invoke-WebRequest -URI https://ifconfig.me -UserAgent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36", and I got a full webpage, not just the IP address.

4
  • Basically, different browsers return different things. Commented Nov 15, 2022 at 20:58
  • It's not the browser that returns different things; it's the site, based on the browser's User-Agent string. If the site recognizes 'powershell' as a console scripting system, it's reasonable to assume that the script calling the Invoke-WebRequest wants the public IP address, unless you use one of the "paths" (see the 'Command Line Interface' section when you visit it with a browser) to retrieve some of the other information. Commented Nov 16, 2022 at 11:53
  • For example, when I didn't set -UserAgent, but did Invoke-WebRequest -URI https://ifconfig.me/ua, the returned .Content was Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) WindowsPowerShell/5.1.20348.859. Commented Nov 16, 2022 at 11:55
  • Thanks for that @Jeff. Commented Oct 4, 2023 at 16:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .