PowerShell Invoke-WebRequest Cmdlet

Introduction to Invoke-WebRequestWindows PowerShell Invoke-WebRequest

Invoke-WebRequest is an interesting PowerShell 3.0 cmdlet; it enables us to extract useful information from web pages.

Examples of Invoke-WebRequest

 ♣

List the Links on a Web Page

Let us see Invoke-WebRequest in action, this is how to display a list of links on a web page.

# PowerShell Invoke-WebRequest Example
$Site = "https://www.computerperformance.co.uk"
$Test = Invoke-WebRequest -URI $Site
$Test.Links | Foreach {$_.href }

Note 1: I chose my own website, please change the value of $Site for the url of your choice.

Research Properties of Invoke-WebRequest

Here is one of my favorite PowerShell techniques, pipe an object into Get-Member and thus expose a list of the methods and properties.

Clear-Host
$Site = "https://www.computerperformance.co.uk"
$Test = Invoke-WebRequest $Site
$Test | Get-Member

Name   MemberType
——-  ———-
Equals Method
GetHashCode Method
GetType Method
ToString Method
AllElements Property
BaseResponse Property
Content Property
Forms Property
Headers Property
Images Property
InputFields Property
Links Property
ParsedHtml Property
RawContent Property
RawContentLength Property
RawContentStream Property
Scripts Property
StatusCode Property
StatusDescription Property

Note 2: Interesting properties include AllElements and Links.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Invoke-WebRequest Example 1: Convert Miles to Feet

The task is to interrogate a website for information.  In this example PowerShell needs the .AllElements property and the .InnerHtml value

Clear-Host
$Site = "http://www.asknumbers.com/MilesToFeetConversion.aspx"
$Request = Invoke-WebRequest -URI $Site
$Request.AllElements | Where-Object {$_.InnerHtml -like "*=*"} |
Sort-Object { $_.InnerHtml.Length } | Select-Object InnerText -First 1

innerText
———
1 Mile = 5280 Feet

Note 3: This URI has an .aspx extension.

Note 4: You can discover more properties by applying Get-Member to $Request.AllElements.

Invoke-WebRequest Example 2: Find Lucky Numbers

The task is to interrogate a website for he latest lucky numbers.  In this example PowerShell needs the .AllElements property and the .InnerHtml value

Clear-Host
$Site = Invoke-WebRequest -URI "http://www.loto49.ro/arhiva-loto49.php"
$LuckyNumbers = $Site.AllElements | Where-Object {$_.tagName -eq "TD"} | `
Where-Object { $_.InnerText -match "^\d+$" } | `
Select-Object -property InnerText -Last 6
$LuckyNumbers

innerText
———
(Results will update each!)

Note 5: To see the webpage itself, you can append.

$IE=New-Object -com InternetExplorer.Application
$IE.Navigate2($Site)
$IE.Visible=$true
$IE.FullScreen=$true

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset now!

Download your fully functional trial copy of the Engineer’s Toolset v10

Research Invoke-WebRequest

Call for PowerShell's built-in help to learn more about it's parameters.  Be aware that this only works in version 3.0, previously in PowerShell 2.0 you would have to rely on .NET Framework.

Clear-Host
Get-Help Invoke-WebRequest -full

Technically, Invoke-WebRequest sends HTTP and HTTPS requests to a web page or web service. It parses the response and returns collections of forms, links, images, and other HTML elements.

Note 6: The -Method parameter is interesting, it takes values of:
Default, Delete, Get, Head, Options, Post, Put, and Trace.

Research Other Members of the Invoke Family

The verb 'Invoke' is rarely used in general English or in PowerShell, his is how to research other members of this family:

# PowerShell’s Invoke family of cmdlets
Get-Command -Verb invoke

Invoke-Command
Invoke-Expression
Invoke-History
Invoke-Item
Invoke-WebRequest
Invoke-WmiMethod
Invoke-WSManAction

Summary of PowerShell's Invoke-WebRequest

Invoke-WebRequest is an interesting cmdlet introduced in PowerShell 3.0.  I have examples showing how to investigate web pages.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tasks:

PowerShell Home   • Shell Application   • New-Object   • PowerShell Add Printer   • PowerShell -com

PowerShell Logon Script  • Map Network Drive  • PowerShell Create Shortcut  • Free CSV Import Tool

Invoke-Expression   • Invoke-Command   • Invoke-Item   • PowerShell Expression v Command Mode

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.