powershell and captured printers

Peter wanted an  inventory of printers attached to a print server.  Sure, i said, and started to look for WMI objects of interest.

Turned out to be ridiculously easy.  From two posts Chris Towles (provided by James Douglas) and MichaelStevelmans, i got code that just worked.  The problem was solved in about 5 minutes with an inventory of 80 printers.  The code from Michael Stevelmans is shorter, and does write to an excel spreadsheet.

Default output looked like this:

NameAvgPagesPerMinute : 0
Caption : ALHS Printer #2
Comment : HP LJ P3005dn
Default : False
DriverName : HP LaserJet P3005 PCL 6
JobCountSinceLastReset : 0
Local : True
Location : Alumni House - 1st Floor
Name : ALHS Printer #2
Network : False
PortName : 10.103.18.102:lp
PrinterStatus : 3
PrintJobDataType : RAW
PrintProcessor : hpcPP5r1
Shared : True
ShareName : ALHS-NP2
Status : Unknown
StatusInfo : 
SystemName : XXXXXXXX
WorkOffline : False

from code that looked like this:

$ComputerName = "xxx1.xxx.edu"
Write-Host "Connecting to $ComputerName by WMI to gather printer information."
 Write-Host "Warning this may take a few minutes depending how many printers there are." -ForegroundColor Red

 $colItems = get-wmiobject -class "Win32_Printer" -namespace "root\CIMV2" -computername $ComputerName
 $count = $colItems.Count
 $pos = 0
 Write-Host "Found $count printers. Getting details on each now."
 $Printers = @()

 foreach ($objItem in $colItems) 
 {
 write-progress -activity "Getting Information on each printer." -status "% Complete" -percentcomplete (($pos++/$count)*100);

 #$list = "" | select "AvgPagesPerMinute", "Caption", "Comment", "Default", "DriverName" ,"InstallDate", "JobCountSinceLastReset", "Local", "Location" ,"Name", `
 #"Network", "PortName", "PrinterStatus", "PrintJobDataType", "PrintProcessor", "Shared", "ShareName", "Status", "StatusInfo", "SystemName" , "WorkOffline"

 #creating a new object called $PrinterInfo 
 $PrinterInfo = New-Object psobject 

 #write-host "Attributes: " $objItem.Attributes
 $PrinterInfo | Add-Member NoteProperty NameAvgPagesPerMinute $objItem.AveragePagesPerMinute 
 $PrinterInfo | Add-Member NoteProperty Caption $objItem.Caption
 #write-host "Character Sets Supported: " $objItem.CharSetsSupported
 $PrinterInfo | Add-Member NoteProperty Comment $objItem.Comment$objItem.ConfigManagerUserConfig
 $PrinterInfo | Add-Member NoteProperty "Default" $objItem.Default
 $PrinterInfo | Add-Member NoteProperty "DriverName" $objItem.DriverName
 $PrinterInfo | Add-Member NoteProperty "JobCountSinceLastReset" $objItem.JobCountSinceLastReset
 $PrinterInfo | Add-Member NoteProperty "Local" $objItem.Local
 $PrinterInfo | Add-Member NoteProperty "Location" $objItem.Location
 $PrinterInfo | Add-Member NoteProperty "Name" $objItem.Name
 $PrinterInfo | Add-Member NoteProperty "Network" $objItem.Network
 $PrinterInfo | Add-Member NoteProperty "PortName" $objItem.PortName
 $PrinterInfo | Add-Member NoteProperty "PrinterStatus" $objItem.PrinterStatus
 $PrinterInfo | Add-Member NoteProperty "PrintJobDataType" $objItem.PrintJobDataType
 $PrinterInfo | Add-Member NoteProperty "PrintProcessor" $objItem.PrintProcessor
 $PrinterInfo | Add-Member NoteProperty "Shared" $objItem.Shared
 $PrinterInfo | Add-Member NoteProperty "ShareName" $objItem.ShareName
 $PrinterInfo | Add-Member NoteProperty "Status" $objItem.Status
 $PrinterInfo | Add-Member NoteProperty "StatusInfo" $objItem.StatusInfo
 $PrinterInfo | Add-Member NoteProperty "SystemName" $objItem.SystemName
 $PrinterInfo | Add-Member NoteProperty "WorkOffline" $objItem.WorkOffline

 $Printers += $PrinterInfo
 }
 write-progress -activity "Getting Information on each Port." -status "% Complete:" -percentcomplete 100 -Completed;
 # return 
 $Printers| Export-Csv c:\users\rbeck\desktop\list.csv -notype

I actually remove a whole bunch of commented out line, and some of the output seemed not to be helpful anyway, but it got the job done.  I added the export-csv after the first run, and Peter seemed pleased.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.