Solved

Powershell Script to AutoMail Printer Server Statistics

Posted on 2009-05-11
4
1,032 Views
Last Modified: 2012-05-06
Hi,

i need some Powershell script which shows me something like that:

###################################################
C:\>splinfo
Number Local Printers   1
Windows Version         5.1 Build 2600 (Service Pack 3) FREE
Number of Processors    2 PROCESSOR_INTEL Level 6
Total Jobs Spooled      0
Total Bytes Printed     0
Average Bytes/Job       0
Browse List Requested   0
Browse Printer Added    0
Spooler Up Time         3 Days 19:16:30
Server Up Time          3 Days 19:17:14
###################################################

this is generated by: splinfo \\servername ( splinfo from the ressource kit for win2k3 )

Are they any Powershell Commands which can pass me the same informations like splinfo, which i can run on daily basis ?
0
Comment
Question by:MaurizioSchmidt
  • 3
4 Comments
 
LVL 70

Expert Comment

by:Chris Dent
ID: 24353041

You can get it from WMI, it's not a single command. Do you want to use that?

Chris
0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 24353886
This will return the same information using WMI (because I was curious how much was available).

I made the processor and OS information optional with the -SystemInformation parameter because it increases the processing time rather a lot. It runs against the local computer by default.

You could call it like these:

Get-SpoolerInformation -v
Get-SpoolerInformation -c "SomeComputer"
Get-SpoolerInformation -verbose -s
Get-SpoolerInformation "SomeComputer" -sys -v

In short, it's flexible about the parameter names, as long as they can be matched to the "Param" list it's fine.

Chris
Function Get-SpoolerInformation {
  Param(
    $ComputerName = ".",
    [Switch]$SystemInformation,
    [Switch]$Verbose)
 
  # Quite a few queries, can make everything a bit slow so is optional
  If ($SystemInformation) {
    $OS = Get-WMIObject Win32_OperatingSystem -ComputerName $ComputerName
    $OSName = "$($OS.Caption) ($($OS.CSDVersion))"
    $Processors = Get-WMIObject Win32_Processor `
      -ComputerName $ComputerName | Select-Object Name, Manufacturer, Caption
 
    $SystemInformation = "" | Select-Object `
      @{n='LocalPrinterCount';e={ `
        (Get-WMIObject Win32_Printer -ComputerName $ComputerName -Filter "Local=$True").Count }}, `
      @{n='OperatingSystem';e={ $OSName }}, `
      @{n='Processors';e={ $Processors }}, `
      @{n='ProcessorCount';e={ $Processors.Count }}, `
      @{n='SpoolerStartTime';e={ `
        [System.Management.ManagementDateTimeConverter]::ToDateTime((Get-WMIObject Win32_Process `
        -ComputerName $ComputerName -Filter "Name='spoolsv.exe'").CreationDate) }}, `
      @{n='ServerBootTime';e={ `
        [System.Management.ManagementDateTimeConverter]::ToDateTime($OS.LastBootUpTime) }}
 
    Write-Output $SystemInformation
  }
 
  # Write per-printer statistics instead of all totals
  If ($Verbose) {
    $PrinterStats = Get-WMIObject Win32_PerfRawData_Spooler_PrintQueue `
      -ComputerName $ComputerName -Filter "Name<>'_Total'"
  } Else {
    $PrinterStats = Get-WMIObject Win32_PerfRawData_Spooler_PrintQueue `
      -ComputerName $ComputerName -Filter "Name='_Total'"
  }
 
  # Set error control to carry-on-regardless. Ignoring divide by 0 errors.
  $ErrorActionPreference = "SilentlyContinue"
 
  $PrinterStats = $PrinterStats | Select-Object `
    Name, `
    TotalJobsPrinted, TotalPagesPrinted, `
    @{n='TotalBytesPrinted';e={ $_.BytesPrintedPerSec }}, `
    @{n='TotalJobsSpooling';e={ $_.JobsSpooling }}, `
    @{n='TotalJobErrors';e={ $_.JobErrors }}, `
    NotReadyErrors, OutofPaperErrors, `
    @{n='AverageBytesPerJob';e={ [Math]::Round(([Int64]$_.BytesPrintedPerSec / [Int64]$_.TotalJobsPrinted), 2) }}, `
    @{n='AveragePagesPerJob';e={ [Math]::Round(([Int64]$_.TotalPagesPrinted / [Int64]$_.TotalJobsPrinted), 2) }}, `
    @{n='AverageBytesPerPage';e={ [Math]::Round(([Int64]$_.BytesPrintedPerSec / [Int64]$_.TotalPagesPrinted), 2) }}, `
    @{n='BrowseListRequested';e={ $_.EnumerateNetworkPrinterCalls }}
 
  Return $PrinterStats
}

Open in new window

0
 
LVL 4

Author Comment

by:MaurizioSchmidt
ID: 24356475
Thx very much Chris.

how exactly can i run this command on daily basis ? create some file called printerstats.vbs or ?!
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 24357590

It could be saved off as a .ps1 file, however you would still need to tell PowerShell that it's allowed to run it, and then call it with PowerShell.

On the system running the script you need to execute this command once:

Set-ExecutionPolicy Unrestricted

Ideally run that with the user which will be executing the script... just to make sure. Otherwise you can't run unsigned scripts (this is the telling PowerShell it's allowed bit). If that isn't set it needs a digital signature (code-signing certificate), not something I have.

Then you could run the script file like this:

PowerShell.exe script.ps1

Don't forget that you have to call the function within the script, just the function on its own won't do much :) Also bear in mind that the code does not have to run on the Printer Server itself, it will run remotely like splinfo.

What do you want to happen with the output? It returns an object from the function, same kind of thing as you'd get if you were to run "Get-Host". Presumably you want to send that somewhere? It'll easily export to a CSV file.

Alternatively, if you're building a bigger report we can combine the output from several different printer servers into a single object (and then into CSV file / email).

Chris
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Set OWA language and time zone in Exchange for individuals, all users or per database.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question