Link to home
Start Free TrialLog in
Avatar of tenover
tenoverFlag for United States of America

asked on

Remove certain printers, add replacements at login

I have a print server that hosts 20 network printers.  5 of these are being replaced tomorrow. They are being added to a new print server as well.  Is there an easy way to run a login script for everyone that removes ONLY these 5 old printers if they are mapped from the old server, and then add the 5 new printers that are hosted on the new server?
Avatar of it_saige
it_saige
Flag of United States of America image

You could use something like this:
'Option Explicit ' Force Explicit Declarations

	'Declare constants
	const kErrorSuccess = 0
	const kNameSpace = "root\cimv2"

	' Declare variables
	' Object variables
	Dim objWMISvc ' WMI Service Object

	' Miscellaneous variables
	Dim varIdx ' Index Counting
	Dim strComputer, colItems, Printers
	Dim oPrinter, oService, iResult, strPrinterName
	Dim bDelete, bConnection, strTemp, remPrn
	Dim strPrinter ' Printer string
	Dim strUser ' User string
	Dim strPassword ' Password string

	' Array of old printers to remove
	oPrn(0) = "\\server1\oldPrinter1"
	oPrn(1) = "\\server1\oldPrinter2"
	oPrn(2) = "\\server1\oldPrinter3"
	oPrn(3) = "\\server1\oldPrinter4"
	oPrn(4) = "\\server1\oldPrinter5"


	' Array of new printers to add

	nPrn(0) = "\\server2\newPrinter1,TRUE"
	nPrn(1) = "\\server2\newPrinter2,FALSE"
	nPrn(2) = "\\server2\newPrinter3,FALSE"
	nPrn(3) = "\\server2\newPrinter4,FALSE"
	nPrn(4) = "\\server2\newPrinter5,FALSE"

	strComputer ="."

	' Call WMIConn function
	If WMIConn(strComputer, kNameSpace, strUser, strPassword, oService) Then

		' Set Printers to an instance of the oServer based on the printer
		set Printers = oService.InstancesOf("Win32_Printer")

		' Set the WMI Service object to get objects from the local computer
		Set objWMISvc = GetObject("winmgmts:\\" & strComputer & "\" & kNameSpace)

		' Set the Collected Items to be installed printers
		Set colItems = objWMISvc.ExecQuery ("SELECT * FROM Win32_Printer WHERE Local = False")

		' For loop to work with enumerated printer connections
		For Each oPrinter In Printers
			' Remove network printer connections
			oPrinter.Delete_
		' Next iteration in for-loop
		Next

		' Beggining of printer connections

		' Username driven printer connections
		For varIdx = LBound(nPrn) To UBound(nPrn)

			optionPrn = Split(nPrn(varIdx),",")

			If Not ConnPrn(optionPrn(0), optionPrn(1)) Then
				msg = msg & "Printer: " & optionPrn(0) & " - Not connected" & vbcrlf
			Else
				msg = msg & "Printer: " & optionPrn(0) & " - Connected" & vbcrlf
			End If

		' Next iteration of Printer Connections Loop
		Next

		' End of printer connections

		Wscript.echo msg
		' End of script processing

Function WMIConn(strComputer, strNameSpace, strUser, strPassword, oService)
	On Error Resume Next
	Dim oLocator
	Dim bResult
	oService = null
	bResult = FALSE
	Set oLocator = CreateObject("WbemScripting.SWbemLocator")
	If Err = kErrorSuccess Then
		Set oService = oLocator.ConnectServer(strComputer, strNameSpace, strUser, strPassword)
		If Err = kErrorSuccess then
			bResult = true
			oService.Security_.impersonationlevel = 3
			'
			' Required to perform administrative tasks on the spooler service
			'
			oService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege"
			Err.Clear
		End if
	End if
	WMIConn = bResult
End Function

Function ConnPrn(strPrnUNC, strDefPrn)
	' Function to connect to a network printer share.
	' If the printer specified is already connected, the function
	' attempts to remove the network connection.
	' objFSO is the File System Object, with global scope.
	' objNetwork is the Network object, with global scope.
	' objShell is the Shell Object, with global scope.
	' Returns True if printer connected, False otherwise.

	Dim objWMISvc ' WMI Service Object
	Dim objPrn ' Printer Object
	Dim intIdx ' Index counter
	Dim colItems ' Collected Items
	Dim strComputer ' Local computer variable
	Dim strDefaultState ' Default share state
	Dim strCmd ' Default command string
	Dim cmdRetVal ' Return Value of the command

	strComputer ="."

	'On Error Resume Next

	' Set the WMI Service object to get objects from the local computer
	Set objWMISvc = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

	' Set the Collected Items to be installed printers
	Set colItems = objWMISvc.ExecQuery ("SELECT * FROM Win32_Printer")


	If Prns.Count = 0 Then
		strCmd = "rundll32 printui.dll,PrintUIEntry /in /q /n " & strPrnUNC 
		'Wscript.echo "Prns.Count - Executing command: " & strCmd
		cmdRetVal = objShell.Run(strCmd, 1, TRUE)
		'Wscript.echo "Prns.Count - Return value is: " & cmdRetVal
	Else
		For Each objPrn In colItems
			'Wscript.Echo objPrn.DeviceID
			If UCase(objPrn.DeviceID) = UCase(strPrnUNC) Then
				' Add new printer. Need the ,1,true to wait for shell to complete before continue
				strCmd = "rundll32 printui.dll,PrintUIEntry /dn /q /n " & strPrnUNC 
				'Wscript.echo "objPrn - Executing command: " & strCmd
				cmdRetVal = objShell.Run(strCmd, 1, TRUE)
				'Wscript.echo "objPrn - Return value is: " & cmdRetVal
				'Wscript.Echo "objPrn - First error Number is: " & Err.Number			
			End If
		Next

		Set objPrn = Nothing
	End If

	strCmd = "rundll32 printui.dll,PrintUIEntry /in /q /n " & strPrnUNC 
	'Wscript.echo "Out of loop - Executing command: " & strCmd
	cmdRetVal = objShell.Run(strCmd, 1, TRUE)
	'Wscript.echo "Out of loop - Return value is: " & cmdRetVal
	'Wscript.Echo "Out of loop - Second error Number is: " & Err.Number

	'Wscript.echo "This is a default printer?" & strDefPrn		
	If strDefPrn = "TRUE" Then
		strCmd = "rundll32 printui.dll,PrintUIEntry /y /n " & strPrnUNC 
		'Wscript.echo "strDefPrn - Executing command: " & strCmd
		cmdRetVal = objShell.Run(strCmd, 1, TRUE)
		'Wscript.echo "strDefPrn - Return value is: " & cmdRetVal
	End If
	If cmdRetVal = 0 Then
		ConnPrn = True
	Else
		Err.Clear
		ConnPrn = False
	End If
	'On Error GoTo 0
End Function

Open in new window

HTH,

-saige-
ASKER CERTIFIED SOLUTION
Avatar of Joseph Daly
Joseph Daly
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial