Complete Printer Management
This script, which has been extensively tested on Windows XP, cuts down on the effort required to manage network printers. It works with group policy, and will automatically install (or delete) printers depending upon it's set-up. It's particularly useful when a site upgrade of printers has been completed, as old printers can be deleted and new ones added without visiting the workstations or needing the user to do anything (especially combined with a set default printer script).
The script is presumed to be pushed out by group policy and run as a startup script. It will only work with certified printer drivers (so if when manually installing a driver you get a security prompt, that printer cannot be installed using this script). Also, the printers are assumed to be network printers and printed directly to directly (so IP address is needed). I prefer this method to sharing printers on a server.
' Script written by David Barrett
' Copyright 2010
' http://www.cedit.biz/
' This script is licensed under the Creative Commons
' Attribution 2.5 Licence
' http://creativecommons.org/licenses/by/2.5/
'
' You are free to use it for both personal and
' commercial purposes, so long as full attribution
' is given to the author (David Barrett).
'
' This notice must not be removed
'
'
rem Printer installation/tidy up script
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters
rem Delete old printers/old printer names
if objPrinter.Name="Photocopier" then
DeletePrinter(objPrinter.Name)
elseif objPrinter.Name="SPC410dn (ICT Suite)" then
DeletePrinter(objPrinter.Name)
end if
Next
' Install any printers (if a printer already exists of this name, it is ignored)
' Syntax:
' Install "Printer Name", "Driver name (from .inf)", "Driver files location", "inf file", "IP address"
Install "Photocopier (Reception)", _
"NRG MP C3500 PCL 5c","\\server\Software Install\Photocopier\MPC3500", _
"\\server\Software Install\Photocopier\MPC3500\oemsetup.inf","10.209.56.124"
sub DeletePrinter(strPrinterName)
' Delete the specified printer
Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer where DeviceID='" & _
strPrinterName & "'")
for each objPrinter in colInstalledPrinters
objPrinter.CancelAllJobs()
objPrinter.Delete_
next
End sub
sub Install(strPrinter, strDriverName, strDriverSource, strDriverInf, strIP)
' Install the specified printer, including driver and port
InstallPrinterDriver strDriverName, strDriverSource, strDriverInf
InstallPrinterPort strIP
InstallPrinter strPrinter, strDriverName, "IP_" & strIP
end sub
sub InstallPrinter(strName, strDriverName, strPortName)
' Install printer as detailed
' First check whether the printer is installed already
dim colInstalledPrinters, objPrinter
Set colInstalledPrinters = objWMIService.ExecQuery("Select Name from Win32_Printer")
for each objPrinter in colInstalledPrinters
if objPrinter.Name=strName then exit sub ' We have a match, so no need to install
next
Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_
objPrinter.DriverName = strDriverName
objPrinter.PortName = strPortName '"IP_169.254.110.160"
objPrinter.DeviceID = strName
objPrinter.Location = ""
objPrinter.Network = True
objPrinter.Shared = false
objPrinter.Put_
end sub
sub InstallPrinterDriver(strDriverName, strDriverSource, strDriverInf)
' Installs the printer driver
dim colInstalledDrivers, objDriver, strInstalledDriverName
' First check whether the driver is installed already
Set colInstalledDrivers = objWMIService.ExecQuery("Select Name from Win32_PrinterDriver")
for each objDriver in colInstalledDrivers
strInstalledDriverName=Left(objDriver.Name, Instr(1,objDriver.Name,",")-1)
if strInstalledDriverName=strDriverName then exit sub ' We have a match, so no need to install driver
next
' Driver not present, so install it
Set objDriver = objWMIService.Get("Win32_PrinterDriver")
objDriver.Name = strDriverName
objDriver.SupportedPlatform = "Windows NT x86"
objDriver.Version = "3"
objDriver.DriverPath = strDriverSource
objDriver.Infname = strDriverInf
objDriver.AddPrinterDriver(objDriver)
end sub
sub InstallPrinterPort(strIP)
' First check whether the port exists already
Set colInstalledPorts = objWMIService.ExecQuery _
("Select Name from Win32_TCPIPPrinterPort")
for each objPort in colInstalledPorts
if objPort.Name="IP_" & strIP then exit sub ' We have a result, so no need to add port
next
' Add new printer port
Set objNewPort = objWMIService.Get _
("Win32_TCPIPPrinterPort").SpawnInstance_
objNewPort.Name = "IP_" & strIP
objNewPort.Protocol = 1
objNewPort.HostAddress = strIP
objNewPort.PortNumber = "9100"
objNewPort.SNMPEnabled = False
objNewPort.Put_
end sub
