Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell Script to AutoMail Printer Server Statistics

Posted on 2009-05-11
4
Medium Priority
?
1,054 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 71

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 71

Accepted Solution

by:
Chris Dent earned 2000 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 71

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

782 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