powershellnetwork-printers

Powershell Invoke-WebRequest - Gather specified data


I am trying to collect Total Pages Printed from a printer's web page. The script can...

  1. Load the login page.
  2. Navigate to the maintenance sub-page
  3. Select the column with the desired data

However, I don't know how to adjust the script to only capture the numbers I need.

$R=Invoke-WebRequest http://1.2.3.4/status.html -SessionVariable session1

#B553 - name w Html
$R.Forms[0].Fields["B553"]="login"
$R.Forms[0].Fields["password"]="password"

$Invoke1=Invoke-WebRequest -Uri ("http://1.2.3.4/status.html" + $R.Forms[0].Action) -WebSession $session1 -Method POST -Body $R.Forms[0].Fields

Start-Sleep -s 5

$Invoke2=Invoke-WebRequest 'http://1.2.3.4/Maintance-sub-page' -WebSession $session1

$Invoke2.AllElements | where tagname -EQ "dd" | Select innerText

The result of the following:

$Invoke2.AllElements | where tagname -EQ "dd" | Select innerText

Total Pages in web-page

Total pages result in PS

HTML


Solution

  • It looks like the results in Powershell are in the same order as those on the HTML page. It looks kind of like those results are an array, and (if I counted right), it looks like the field you want is 23 (the array starts at 0).

    Give the following a try...

    $($Invoke2.AllElements | where tagname -EQ "dd" | Select innerText)[23]