Skip to content

Instantly share code, notes, and snippets.

@DexterPOSH
Created September 30, 2014 15:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DexterPOSH/3481845c7d1fc76690e4 to your computer and use it in GitHub Desktop.
Save DexterPOSH/3481845c7d1fc76690e4 to your computer and use it in GitHub Desktop.
Set-StrictMode -Version latest
function Get-PrinterInfo
{
[CmdletBinding()]
[OutputType([int])]
Param
(
# Specify the ComputerName
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[string[]]$ComputerName
)
Begin
{
Write-Verbose -Message "[BEGIN] Starting with the Get-PrinterInfo function"
$PrinterStatusarray= "Other","Unknown","Idle","Printing","WarmingUp","StoppedPrinting","Offline"
}
Process
{
foreach ($computer in $ComputerName)
{
#check if the machine is online
If (Test-Connection -ComputerName $computer -Count 2 -Quiet)
{
TRY
{
$Printers = Get-WmiObject -Class Win32_Printer -ComputerName $computer -ErrorAction Stop | Select-Object -Property Name,Caption,SystemName,Drivername,PrinterStatus
$PrinterPerf = Get-WmiObject -Class Win32_PerfFormattedData_Spooler_PrintQueue -ComputerName $Computer -ErrorAction Stop
foreach ($Printer in $Printers )
{
$PerfData = $PrinterPerf | Where-Object -FilterScript {$_.Name -eq $Printer.Name } #Hoping that all printer names are unique ;)
$Printer.PrinterStatus = $PrinterStatusarray[$($printer.PrinterStatus - 1)]
$Printer | Add-Member -MemberType NoteProperty -Name Jobs -Value $PerfData.Jobs -PassThru |
Add-Member -MemberType NoteProperty -Name CurrentJobsSpooling -Value $PerfData.JobsSpooling -PassThru |
Add-Member -MemberType NoteProperty -Name MaximumJobsSpooling -Value $PerfData.MaxJobsSpooling -PassThru |
Add-Member -MemberType NoteProperty -Name TotalJobsPrinted -Value $PerfData.TotalJobsPrinted -PassThru |
Add-Member -MemberType NoteProperty -Name JobErrors -Value $PerfData.JobErrors -PassThru |
Add-Member -MemberType NoteProperty -Name NotReadyErrors -Value $PerfData.NotReadyErrors -PassThru |
Add-Member -MemberType NoteProperty -Name OutOfPaperErrors -Value $PerfData.OutOfPaperErrors -PassThru
}
}
CATCH
{
#handle Error here
Write-Warning -Message "[PROCESS] Connecting to $computer over WMI threw $($_.exception)"
}
}
else
{
Write-Warning -Message "[PROCESS] $computer is offline"
}
}
}
End
{
Write-Verbose -Message "[BEGIN] Starting with the Get-PrinterInfo function"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment