Solved

Powershell Script to AutoMail Printer Server Statistics

Posted on 2009-05-11
4
1,036 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…

710 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