ICC Home  /  Members  /  Meetings  /  Peer Support  /  Documentation  /  Projects


The    
"printers.vbs" script


Return to Print Services Documentation

Introduction

The script below includes code for deleting existing printers.
That feature has been added temporarily in order to clean up
printers that were inadvertently installed due to improper
permissioning. That portion is disabled to avoid long logon times.

printers.vbs

'***IFAS Printer Mapping Script
'***Last Modified: July 27, 2012
'***By: Santos Soler (ssoler@ufl.edu)
'***Normal Syntax: cscript \\ad.ufl.edu\netlogon\ifas\printers.vbs Print_Server_Name
'***Example: cscript \\ad.ufl.edu\netlogon\ifas\printers.vbs if-srvv-print
'***Debug Mode Syntax: cscript printers.vbs Print_Server_Name /debug
'***Example: cscript \\ad.ufl.edu\netlogon\ifas\printers.vbs if-srvv-print /debug
'***Debug mode will run the script, output status messages and install the printers        

On Error Resume Next
'initialize DebugMode to 0 (off)
intDebugMode = 0

Dim objWSHShell
Dim strLogonServer

Set objWSHShell = Wscript.CreateObject("Wscript.Shell")

strLogonServer = objWSHShell.ExpandEnvironmentStrings("%LOGONSERVER%")
StrLogonServer = Right(strLogonServer,Len(strLogonServer)-2)

'Perform an action depending on the number of arguments entered on the command line
Select Case Wscript.Arguments.Count
	'With zero arguments, the print server name has not been specified so the script will display an error message and exit
	Case 0
		Wscript.echo "Incorrect Syntax: Print server name is required."
		Wscript.echo ""
		Wscript.echo "Example: printers.vbs if-srvv-print"
		Wscript.echo "or"
		Wscript.echo "Example: printers.vbs if-srvv-print /debug"
		Wscript.echo "To run the script with debugging information output to the screen"
		Wscript.quit
	'If one argument is given, set it as the print server name
	Case 1
		strPrintServer = Wscript.Arguments(0)
	'With two arguments, set the print server name to the first argument, and check the second argument
	'To determine if the user wants to run the script in debug mode
	Case 2
		strPrintServer = Wscript.Arguments(0)
		If strcomp(lcase(Wscript.Arguments(1)), "/debug") = 0 Then
			intDebugMode = 1
		End If
	'if there are more than two arguments, assume the user has made an error, display an error message and exit.	
	Case Else
		Wscript.echo "Incorrect Syntax: Too many arguments"
		Wscript.echo ""
		Wscript.echo "This script onlt accepts two arguments - the print server name and the /debug flag"
		Wscript.echo "Example: printers.vbs if-srvv-print"
		Wscript.echo "or"
		Wscript.echo "Example: printers.vbs if-srvv-print /debug"
		Wscript.echo "To run the script with debugging information output to the screen"
		Wscript.quit
	End Select

	
'***********Start DebugMode Code*****************
if intDebugMode = 1 then
	wscript.echo (now) & " - Starting printer script"
	wscript.echo "Waiting 10 seconds, this is normal"
	wscript.echo ""
end if
'***********End DebugMode Code*******************

'sleep for 10 seconds
wscript.sleep 10000

Const ADS_SCOPE_SUBTREE = 2
Set objNetwork = CreateObject("WScript.Network")
Set objConnection = CreateObject("ADODB.Connection")
Set objDNCommand =   CreateObject("ADODB.Command")
Set objPrintQueueCommand =   CreateObject("ADODB.Command")
objConnection.Open "Provider=ADsDSOObject;"

'Printers which will not be deleted from computers (mostly used in labs)
PrintersExemptFromDeletion = "EYN-hp4300pcl6, EYN-hp4300ps, agr-hpbi1200"

'Get the user's username and computername
strUsername =  objNetwork.UserName 
strComputername = objNetwork.ComputerName

'***********Start DebugMode Code*****************
if intDebugMode = 1 then
	Wscript.echo "Username: " & strUsername
	Wscript.echo "Computer name: " & strComputername
	Wscript.echo "Print Server: " & strPrintServer
	Wscript.echo "Printers Exempt From Deletion: " & PrintersExemptFromDeletion
	wscript.echo ""
end if
'***********End DebugMode Code*******************

'Make sure the script is not being run on the print server
if strcomp(lcase(strComputername), lcase(strPrintServer)) = 0 then 
	wscript.echo "This script should not be run on the print server.  Please run this script from client computers only"
	wscript.quit
end if

'make sure that the print server is available (responding to pings)
'this ensures that mapped printers are not deleted due to network or server problems
set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
      ("select * from Win32_PingStatus where address = '" & strPrintServer & "'")

 for each objRetStatus in objPing
    if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 then

	'***********Start DebugMode Code*****************
		if intDebugMode = 1 then
			WScript.Echo strPrintServer &" is not available"
		end if
	'***********End DebugMode Code*******************	
	 
	wscript.quit

	Else
	
	'***********Start DebugMode Code*****************
		if intDebugMode = 1 then
			wscript.echo strPrintServer & " is available"
		end if
	'***********End DebugMode Code*******************	

	end if
next

'Search for the Distinguished Name of the print server 
Set objDNCommand.ActiveConnection = objConnection
objDNCommand.Properties("Page Size") = 1000
objDNCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 

objDNCommand.CommandText = _
    "SELECT distinguishedName FROM 'LDAP://" & strLogonServer & ".ad.ufl.edu' WHERE objectCategory='computer' " & _
        "AND CN='" & strPrintServer & "'"
Set objDNRecordSet = objDNCommand.Execute
objDNRecordSet.MoveFirst
strPrintServerDn = objDNRecordSet.Fields("distinguishedName").Value

'***********Start DebugMode Code*****************
if intDebugMode = 1 then
	wscript.echo strPrintServer & " Distinguished Name: " & strPrintServerDN
	wscript.echo ""
	wscript.echo (now) & " - Enumerating Printers on " & strPrintServer
end if
'***********End DebugMode Code*******************

'Enumerates printers on print server
Set objPrintQueueCommand.ActiveConnection = objConnection
objPrintQueueCommand.CommandText = "Select printShareName from 'LDAP://" & strPrintServerDN & "' where objectClass='printQueue'" 
objPrintQueueCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objPrintQueueCommand.Properties("Cache Results") = False 
Set objPrintQueueRecordSet = objPrintQueueCommand.Execute

'***********Start DebugMode Code*****************
if intDebugMode = 1 then
	wscript.echo (now) & " - Done Enumerating Printers on "& strPrintServer
	Wscript.echo (now) & " - Enumerating Printers on local machine"
end if
'***********End DebugMode Code*****************

'Enumerates printers on local machine and removes erroneous printers
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colPrinters = objWMIService.ExecQuery ("Select ShareName, ServerName, Name, Attributes from Win32_Printer")

'***********Start DebugMode Code*****************
if intDebugMode = 1 then
	Wscript.echo (now) & " - Done Enumerating Printers on local machine"
end if
'***********End DebugMode Code*****************

'For each local Printer 
For each objPrinter in colPrinters
	If objPrinter.ShareName <> "" then
		'Check if the local printer points to an old server and delete
		If lcase(objPrinter.ServerName) = "\\if-srv-print" or lcase(objPrinter.ServerName) = "\\nt-file" or lcase(objPrinter.ServerName) = "\\if-wec-srv" or lcase(objPrinter.ServerName) = "\\if-srv-milton" or lcase(objPrinter.ServerName) = "\\if-srv-polk" or lcase(objPrinter.ServerName) = "\\if-srv-dixie" or lcase(objPrinter.ServerName) = "\\if-srv-jay" or lcase(objPrinter.ServerName) = "\\if-srv-aquatic" or lcase(objPrinter.ServerName) = "\\if-srv-putnam" or lcase(objPrinter.ServerName) = "\\if-srv-escambia" or lcase(objPrinter.ServerName) = "\\if-srv-ruskin" or lcase(objPrinter.ServerName) = "\\if-srv-manatee" or lcase(objPrinter.ServerName) = "\\if-srv-lake" or lcase(objPrinter.ServerName) = "\\if-srv-bradford" or lcase(objPrinter.ServerName) = "\\if-srv-jackson" or lcase(objPrinter.ServerName) = "\\if-srv-jeffersn"  or lcase(objPrinter.ServerName) = "\\if-srv-wakulla" or lcase(objPrinter.ServerName) = "\\if-srv-suwannee" or lcase(objPrinter.ServerName) = "\\if-srv-brooks" or lcase(objPrinter.ServerName) = "\\if-srv-sumter" or lcase(objPrinter.ServerName) = "\\if-srv-hardee" or lcase(objPrinter.ServerName) = "\\if-srv-ona" then
		objPrinter.Delete_
		end if
		'Check that the local printer is on the print server
		if lcase(objPrinter.ServerName) = "\\"& strPrintServer then
			curLocalPrinter = objprinter.servername & "\" & objprinter.sharename

		'***********Start DebugMode Code*****************
			if intDebugMode = 1 then
				Wscript.echo ""
				wscript.echo "Local Printer: " &curLocalPrinter
				wscript.echo "Printer is on " & strprintserver		
			end if
		'***********End DebugMode Code*******************
		
			'reset valid printer to 0 
			'all printers start out as erroneous and need to be checked before they are cnsidered valid
			ValidPrinter = 0
			
			'check if the printer is in the exemption list (exempt from deletion)
			'and set the printer as valid if it is on the exemption list
			if InStr(lcase(PrintersExemptFromDeletion), lcase(objprinter.sharename)) > 0 then
				ValidPrinter = 1
				
			'***********Start DebugMode Code*****************
				if intDebugMode = 1 then
					wscript.echo "Printer is in the exemptionlist"
				end if
			'***********End DebugMode Code*******************
										
			elseIf objPrintQueueRecordSet.EOF = False then
				objPrintQueueRecordSet.MoveFirst
				
				'Loop through each remote printer and compare it against the current local printer to determine if the local printer is valid
				'The loop will exit when the local printer is found to be valid or it has been checked against all remote printers and found to be invalid
				Do Until objPrintQueueRecordSet.EOF OR ValidPrinter = 1
					printShareName = objPrintQueueRecordSet.Fields("printShareName").Value
					curRemotePrinter= "\\" & strPrintServer & "\" & printShareName(0)
				
				'***********Start DebugMode Code*****************
					if intDebugMode = 1 then
						wscript.echo "Remote Printer: " & curRemotePrinter
					end if
				'***********End DebugMode Code*******************	
					
					'compare the current local printer against the current remote printer
					PrinterComp =(strcomp(lcase(curLocalPrinter), lcase(CurRemotePrinter)))
					
					'if the current local printer matches the current remote printer, then the local printer is not erroneous (is valid).
					If (PrinterComp = 0) Then	
						ValidPrinter = 1
					
					'***********Start DebugMode Code*****************
						if intDebugMode = 1 then
							wscript.echo "** Printer match, user has permission **"
						end if
					'***********End DebugMode Code*******************
					
					else 
					'move to the next remote printer in the list
					objPrintQueueRecordSet.MoveNext
					end if
		
				Loop
			End If
			
			'if the local printer does not have a match in the remote printer list
			If ValidPrinter = 0 Then
				'The user no longer has permission to the remote printer, so delete it fom the local computer
				objprinter.delete_
			end if
			'move to the first remote printer in preparation for checking the next local printer	
			objPrintQueueRecordSet.MoveFirst 
			
		'If the printer is not on the print server, ignore it	
		else 
		'***********Start DebugMode Code*****************		
			if intDebugMode = 1 then
				wscript.echo ""
				wscript.echo "Local Printer: " & objprinter.servername & "\" & objprinter.sharename
				wscript.echo "Printer is not on "& strPrintServer
			end if
		'***********End DebugMode Code*******************
		end if
	End If
'move to the next local printer	
Next

'***********Start DebugMode Code*****************
if intDebugMode = 1 then
	wscript.echo ""
	wscript.echo (now) & " - Install printers"
end if
'***********End DebugMode Code*******************

'Installs printers not already installed
objPrintQueueRecordSet.MoveFirst
If objPrintQueueRecordSet.EOF = FALSE then
	objPrintQueueRecordSet.MoveFirst
	Do Until objPrintQueueRecordSet.EOF
		printShareName = objPrintQueueRecordSet.Fields("printShareName").Value

	'***********Start DebugMode Code*****************
		if intDebugMode = 1 then
			wscript.echo lcase("\\"& strPrintServer & "\" & printShareName(0))
		end if
	'***********End DebugMode Code*******************
	
		objNetwork.AddWindowsPrinterConnection "\\" & strPrintServer & "\" & printShareName(0)
		objPrintQueueRecordSet.MoveNext
	Loop
End If

'***********Start DebugMode Code*****************
if intDebugMode = 1 then
	wscript.echo ""
	wscript.echo (now) & " - Printer script completed"
end if
'***********End DebugMode Code*******************

Implementation

This script is called from the main co-managed "LoginScript.vbs"
script and runs for all non-"IF-ADM" accounts.


last edited 10 October 2012 by Santos Soler