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?
tenoverAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
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-
0
Joseph DalyCommented:
Depending on wether your clients have the Group policy preference client side extensions installed you could probably do this using GPPs.

I have used group policy preferences in the past to map shared printers for my users. A good how to can be found here .
http://www.grouppolicy.biz/2011/01/faq-2-how-do-you-map-printer-using-group-policy-preferences/

In the group policy prefences for printers there is also the ability to delete existing printers off of peoples machines.

I would say that with a combination GPP you could select your 5 printers for a delete operation and then add then using the FAQ above

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2003

From novice to tech pro — start learning today.