Add / remove printers via VBS script & Group Policy

Ok, so this issue is *well* documented all over this forum.  I borrowed some code from another posting.  I'm trying to augment the script to apply to another case - where a new user logs in and has *no* network printers associated with his/her account on a particular workstation.

I added an Else If section at the end and am looping over what I think is the correct array.

I am no VBS expert, in fact this is the first time I even tried to mess with it.

When the Else If section is invoked, the printers are added BUT the script is not exiting cleanly.  I keep seeing this error:

WScript.exe - Application Error
     The instruction at "0x7c910f29" referenced memory at "0x00000000".  The memory could not be "read".
     Click on OK to terminate the program

Any thoughts on what I'm doing wrong or why the script won't exit cleanly?


'====================
strComputer = "."
 
' FORMAT OF EACH LINE IS:
' \\oldserver\oldprintername +":"+ \\newserver\newprintername
' eg, \\oldserver\hplj8150:\\newserver\hplj8150
arrPrinterShares = Array( _
			"\\VRHOMESERVER\5th Floor:\\VRBDC01\5th-Floor", _
			"\\VRHOMESERVER\Clntsrvc_color:\\VRBDC01\Clntsrvc-color", _
			"\\VRHOMESERVER\Creative hp4250n:\\VRBDC01\Creative-hp4250n", _
			"\\VRHOMESERVER\Mailroom_color:\\VRBDC01\Mailroom-color", _
			"\\VRHOMESERVER\Media:\\VRBDC01\Media-hp1300n", _
			"\\VRHOMESERVER\Phaser 6120:\\VRBDC01\Phaser6120", _
			"\\VRHOMESERVER\Phaser 8500N:\\VRBDC01\Phaser8500", _
			"\\VRHOMESERVER\Production-hp5000:\\VRBDC01\Production-hp5000", _
			"\\VRHOMESERVER\Doc240:\\VRBDC01\Doc240" _
			)
 
 
Set objNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery _
	("Select * From Win32_Printer Where Local = False")
 
If colPrinters.Count <> 0 Then
	For Each objPrinter In colPrinters
		For intPrinter = LBound(arrPrinterShares) To UBound(arrPrinterShares)
			strOldPrinter = Split(arrPrinterShares(intPrinter), ":")(0)
			strNewPrinter = Split(arrPrinterShares(intPrinter), ":")(1)
			If UCase(objPrinter.DeviceID) = UCase(strOldPrinter) Then
				If objPrinter.Default = True Then
					objNetwork.RemovePrinterConnection objPrinter.DeviceID, True, True
					objNetwork.AddWindowsPrinterConnection strNewPrinter
					objNetwork.SetDefaultPrinter strNewPrinter
				Else
					objNetwork.RemovePrinterConnection objPrinter.DeviceID, True, True
					objNetwork.AddWindowsPrinterConnection strNewPrinter
				End If
			Else
				objNetwork.AddWindowsPrinterConnection strNewPrinter
			End If
		Next
	Next
' 
' ADDED TO AUGMENT BORROWED CODE
' 
ElseIf colPrinters.Count = 0 Then
	For intPrinterAgain = LBound(arrPrinterShares) To UBound(arrPrinterShares)
		strNewPrinterAgain = Split(arrPrinterShares(intPrinterAgain), ":")(1)
		objNetwork.AddWindowsPrinterConnection strNewPrinterAgain
	Next
End If
 
'===============

Open in new window

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

RobSampsonCommented:
Hi,

That doesn't seem to be an issue with the script at all, it's a Windows memory error.......

Your extra code is correct in that it will add *all* of the new printers defined.  Because of this, however, it may be having to install quite a few drivers, and may be causing Windows a bit of a hissy fit.

Try just adding a sleep to let it catch up a bit.

Under this:
objNetwork.AddWindowsPrinterConnection strNewPrinterAgain

add this:
WScript.Sleep 5000 ' wait for 5 seconds

and see how it goes.  This of course means it will take 9 by 5 seconds to execute, so the script won't finish for at least 45 seconds, but it may behave better.....

Regards,

Rob.
0
pteeterAuthor Commented:
I'll try it Rob.

Thanks for sanity checking my 'code'.

3 seconds still results in an error.

5 seconds also results in an error, albeit a different one.

     Microsoft (r) Windows Based Script Host has encountered a problem and needs to close.  We are sorry for the inconvenience.

The stack trace is a bit verbose.

What do you think?

0
RobSampsonCommented:
Hmmm, maybe try reducing your printer array just to test....perhaps it's just the installation of one printer that's causing the problem.....

An annoying trouble-shooting step, I know, but because it's a Windows error, it doesn't give us much more information....

Regards,

Rob.
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
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

pteeterAuthor Commented:
You're right.

Boo.

0
RobSampsonCommented:
LOL! Yeah, sorry to be the bearer of bad news, but hopefully we can track it down!

Regards,

Rob.
0
pteeterAuthor Commented:
I tried each printer individually - none caused the script to mess the bed.

It seems that running them all in sequence causes the issues.

I'm removing each of the Fiery devices we print to...the drivers for these are largest and therefore most time consuming to install/copy.

This version finishes cleanly, though the sleep time is up to 10 seconds.  And now, with the sleep removed it also exits cleanly.

Darn those Fiery RIPs!

More digging to do.

 
0
RobSampsonCommented:
Bugger!  If you add those in one by one, can you find if it's a specific one of those?

Regards,

Rob.
0
pteeterAuthor Commented:
So now, after messing with the order of how the printers are added/parsed in the array, I see *NO* errors even without a SLEEP built-in.

Two things to wonder about...

1. Might the order matter?
2. I was running the script locally to speed up testing, might running the script via Group Policy logon cause some other kind of Windows weirdness we're not expecting?

I'll test the Group Policy enforcement shortly.


0
pteeterAuthor Commented:
And now via GPO...it's working just fine.

What?!?!


0
RobSampsonCommented:
Hmmmm, very very strange!  I wouldn't expect behaviour via GPO to be any different from running it locally (unless it's Startup Script, instead of Logon Script).

Perhaps the order is all that's needed, and while one is installing, the other interferes, etc.....who knows!

Is your problem solved then?

Regards,

Rob.
0
pteeterAuthor Commented:
Well, we'll see how it continues to work.

Script is addressing two issues for me.

1. Moving all current user/machine logons to new print server
2. Ensuring that when new user logs into bound machine his/her printers get setup automatically

Thanks for the help, you definitely win!

0
pteeterAuthor Commented:
Solved for now...we'll see how it goes.
0
RobSampsonCommented:
Thanks.  Your point 1 is exactly why I created that script in my environment.....we retired an old print server and need to map users to the same printers, but on a new server....worked for us!

Regards,

Rob.
0
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
VB Script

From novice to tech pro — start learning today.