Powershell Script to AutoMail Printer Server Statistics

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 ?
LVL 4
MaurizioSchmidtAsked:
Who is Participating?
 
Chris DentConnect With a Mentor PowerShell DeveloperCommented:
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
 
Chris DentPowerShell DeveloperCommented:

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

Chris
0
 
MaurizioSchmidtAuthor Commented:
Thx very much Chris.

how exactly can i run this command on daily basis ? create some file called printerstats.vbs or ?!
0
 
Chris DentPowerShell DeveloperCommented:

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
All Courses

From novice to tech pro — start learning today.