Home > Software design >  Powershell - Get total jobs printed from list of printers
Powershell - Get total jobs printed from list of printers

Time:09-09

Trying to get total jobs printed. It works fine for one printer:

cls
$WebResponse = Invoke-WebRequest "10.240.16.156/server/JOBLOG.htm"
$Total = $WebResponse.AllElements | Select innerText | Select -Index '12'
$Total

Output:

innerText             
---------             
Total Jobs Printed:737

But I need to get that information from five hundred printers, that IP's I have in .csv file:

"PortName"
"10.240.8.27"
"10.240.9.87"
"10.240.19.81"
...

Tried to import IP from CSV, but it only uses last IP from .csv and cannot build a valid url:

cls
$Printers = Import-Csv -Path C:\path\to\csv\_printers.csv
$Url = "http://$PortName/server/JOBLOG.htm"
$WebResponse = ForEach ($PortName in $Printers) {Invoke-WebRequest $Url}
$Total = $WebResponse.AllElements | Select innerText | Select -Index '12'
ForEach ($PortName in $Printers) {
$Total
}

Output:

Invoke-WebRequest : Cannot bind parameter 'Uri'. Cannot convert value "http://@{PortName=10.240.11.86}/server/JOBLOG.htm" to type "System.Uri". Error: "Invalid URI: The hostname could not 
be parsed."
At line:4 char:68
  ... bResponse = ForEach ($PortName in $Printers) {Invoke-WebRequest $Url}
                                                                      ~~~~
      CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
      FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Any help? Thanks

CodePudding user response:

I'm guessing you're looking for something like this:

Import-Csv -Path C:\path\to\csv\_printers.csv | ForEach-Object {
    $Url = "http://{0}/server/JOBLOG.htm" -f $_.PortName

    [pscustomobject]@{
        URL      = $Url
        Response = (Invoke-WebRequest $Url).AllElements.InnerText[12]
    }
}

The error message is actually showing you the object being converted to a string (@{PortName=10.240.11.86}):

.. Cannot convert value "http://@{PortName=10.240.11.86}/server/JOBLOG.htm" ..

You need to expand the PortName property from your object to get the value (only the IP), hence the use of $_.PortName in my example.

  • Related