Solved

Powershell Script to AutoMail Printer Server Statistics

Posted on 2009-05-11
4
1,024 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

930 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now