john Posted August 4, 2012 Posted August 4, 2012 Hi all I am in need of a bit of scripting to help solve an annoying problem.... A member of staff has a local printer attached to there PC, and like all other workstations has access to the central print room colour unit and some others, including a local PDF printer (for use with non-MSFT apps) and the usual MS Image writer etc... Now this user has issues at times with not having the local printer that is attached to the PC they are sat at as default and it choosing a network printer or another local one such as the PDF creator as the new default. Now most people do File > Print and check the name of the printer and manage quite well, however this user fails to do that and just does File > Print > Print before even reading what the printer is, sometimes jobs end up at the colour copier, sometimes as a PDF or sometimes to the local one and this person does handle sensitive and confidential data. Now you may think stupid user and I'd agree as setting default and reading from the list when you have ~4 to choose from isn't hard and the rest of my staff that have identical setup's have no issues at all doing so, but this user is very instant and says they cannot believe they are the only person in the world that "cannot" print to there own printer (aka not manage to choose it from a list). So I need a solution to solve this, my first idea was remove all other printer options and deny them access to network ones thus they could only use the one on the PC. This was thrown back instantly as not acceptable so is some scripting guru able to knock me up a simple script that I could put as a logon script to this users account perhaps that simple allows me to forcibly set the default printer to a named one that I choose? The printer attached is a simple HP LaserJet 1010 usb printer so nothing networkable or fancy I'm afraid and if the user logs onto a pc that isn't there own it just goes meh cannot see that and doesn't set anything? Any help would be appreciated, if it makes a difference the user in question uses a Windows 7 X64 PC. Thanks
ihaveaproblem Posted August 4, 2012 Posted August 4, 2012 You should be able to achieve all this through Group Policy Preferences.
john Posted August 5, 2012 Author Posted August 5, 2012 You should be able to achieve all this through Group Policy Preferences. GPP doesn't work reliably enough as we have tried that and it wasn't 100% reliable. The user in question needs something idiot proof and guaranteed to work.
Steve21 Posted August 5, 2012 Posted August 5, 2012 Don't have printer on, but works between PDF/XML writers etc strPrinter = "CutePDF Writer" Set WshNetwork = WScript.CreateObject("WScript.Network") Set oPrinters = WshNetwork.EnumPrinterConnections MapPrinter = 1 For i = 0 to oPrinters.Count - 1 Step 2 If oPrinters.Item(i+1) = strPrinter Then MapPrinter = 0 Exit For End If Next If MapPrinter = 0 Then WshNetwork.SetDefaultPrinter strPrinter End If (Obviously change name to whatever other is showing as) Steve 1
john Posted August 5, 2012 Author Posted August 5, 2012 Cheers Steve will give it a test on Monday with our test account on the users PC. Assume I can just do it as the logon script in AD for that user and then they might stop complaining
Steve21 Posted August 5, 2012 Posted August 5, 2012 Aye should work fine, just you'll need to check when it's firing off, vs your other printer settings. If your GPP is done after the script, it might reset defaults etc Steve 1
mac_shinobi Posted August 5, 2012 Posted August 5, 2012 Don't have printer on, but works between PDF/XML writers etc strPrinter = "CutePDF Writer" Set WshNetwork = WScript.CreateObject("WScript.Network") Set oPrinters = WshNetwork.EnumPrinterConnections MapPrinter = 1 For i = 0 to oPrinters.Count - 1 Step 2 If oPrinters.Item(i+1) = strPrinter Then MapPrinter = 0 Exit For End If Next If MapPrinter = 0 Then WshNetwork.SetDefaultPrinter strPrinter End If (Obviously change name to whatever other is showing as) Steve Not that it makes much difference aside from the amount of code but just curious why are you enumerating the printers etc, couldn't you just do something like strPrinter = "CutePDF Writer" Set WshNetwork = WScript.CreateObject("WScript.Network") WshNetwork.SetDefaultPrinter strPrinter The printer name needs to be exactly the same as the actual printer name when assigned to strPrinter so if it was a HP Color Laser Jet 4550c Then strPrinter would be strPrinter = "HP Color Laser Jet 4550c"
Steve21 Posted August 5, 2012 Posted August 5, 2012 Not that it makes much difference aside from the amount of code but just curious why are you enumerating the printers etc, couldn't you just do something like strPrinter = "CutePDF Writer" Set WshNetwork = WScript.CreateObject("WScript.Network") WshNetwork.SetDefaultPrinter strPrinter The printer name needs to be exactly the same as the actual printer name when assigned to strPrinter so if it was a HP Color Laser Jet 4550c Then strPrinter would be strPrinter = "HP Color Laser Jet 4550c" If it doesn't exist it'll still set it as default then bomb out everytime. As it'll run on computers without printer too Steve
mac_shinobi Posted August 5, 2012 Posted August 5, 2012 strPrinter = "CutePDF Writer" Set WshNetwork = WScript.CreateObject("WScript.Network") Set oPrinters = WshNetwork.EnumPrinterConnections MapPrinter = 1 For i = 0 to oPrinters.Count - 1 Step 2 If oPrinters.Item(i+1) = strPrinter Then WshNetwork.SetDefaultPrinter strPrinter Exit For End If Next fair enough, one last question, why use the MapPrinter as a flag when you can just set the printer as the default if it finds said printer ?
Steve21 Posted August 6, 2012 Posted August 6, 2012 strPrinter = "CutePDF Writer" Set WshNetwork = WScript.CreateObject("WScript.Network") Set oPrinters = WshNetwork.EnumPrinterConnections MapPrinter = 1 For i = 0 to oPrinters.Count - 1 Step 2 If oPrinters.Item(i+1) = strPrinter Then WshNetwork.SetDefaultPrinter strPrinter Exit For End If Next fair enough, one last question, why use the MapPrinter as a flag when you can just set the printer as the default if it finds said printer ? No particular reason, but just remember doing it your way doesn't give option for an alternative default printer/action if not found. But depends if you need to do anything else, or it's just if found do this, else nothing. @john Gimme a shout if it doesn't work etc. Steve 1
mac_shinobi Posted August 6, 2012 Posted August 6, 2012 Maybe a Select Case would be neater ?? strPrinter="Printer Name" Select Case oPrinters.Item(i+1) Case strPrinter: WshNetwork.SetDefaultPrinter strPrinter Case Else: End Select
Steve21 Posted August 6, 2012 Posted August 6, 2012 Maybe a Select Case would be neater ?? strPrinter="Printer Name" Select Case oPrinters.Item(i+1) Case strPrinter: WshNetwork.SetDefaultPrinter strPrinter Case Else: End Select But that would fire off every time you loop, thus setting every printer to default no matter what it is Unless you're doing it after loop, which is no different to setting flags then. Steve 1
mac_shinobi Posted August 6, 2012 Posted August 6, 2012 (edited) Had to delete the Exit For so it will go through looping until it has finished and just tested it, so have put a msgbox "Execute" to see how many times it executes and it only does it the once Set WshNetwork = WScript.CreateObject("WScript.Network") Set oPrinters = WshNetwork.EnumPrinterConnections For i = 0 to oPrinters.Count - 1 Step 2 msgbox oPrinters.Item(i+1) Select Case oPrinters.Item(i+1) Case "MP C3500 PCL5c": WshNetwork.SetDefaultPrinter "MP C3500 PCL5c" msgbox "Executed" Case Else: End Select Next Select Case would not work with variables ( at least when I have been trying it ) but I have most likely been doing it wrong. As @Steve21 mentioned - just stick with his code as will work better, I am just trying to see how the Select Case would work and also with variables as just doing it out of curiosity, that is all Edited August 6, 2012 by mac_shinobi
Steve21 Posted August 6, 2012 Posted August 6, 2012 Had to delete the Exit For so it will go through looping until it has finished and just tested it, so have put a msgbox "Execute" to see how many times it executes and it only does it the once What I meant is if you put something in your "case else" aka if printer doesn't exist, set this as default, It'll fire off every time the other isn't found, "on every loop" e.g. Printer1 isn't it, it'll fire off, Printer2 is it it'll set default, Printer3 isn't it it'll reset. If that made sense? Steve 1
mac_shinobi Posted August 6, 2012 Posted August 6, 2012 (edited) What I meant is if you put something in your "case else" aka if printer doesn't exist, set this as default, It'll fire off every time the other isn't found, "on every loop" e.g. Printer1 isn't it, it'll fire off, Printer2 is it it'll set default, Printer3 isn't it it'll reset. If that made sense? Steve That's correct - which is a shame, doh I take it there is no way to do error checking using the Case Else so that it doesnt repeatdly fire off the msgbox / wscript.echo commands Edited August 6, 2012 by mac_shinobi
Steve21 Posted August 6, 2012 Posted August 6, 2012 That's correct - which is a shame, doh I take it there is no way to do error checking using the Case Else so that it doesnt repeatdly fire off the msgbox / wscript.echo commands Like what? On Error GoTo AMGFIXERROR DOLOOPSTUFF... AMGFIXERROR: wscript.echo "AMG YOU BROKE IT" [code] That kind of thing? So it only fires off on an error message? Bit confused Steve
mac_shinobi Posted August 6, 2012 Posted August 6, 2012 Like what? On Error GoTo AMGFIXERROR DOLOOPSTUFF... AMGFIXERROR: wscript.echo "AMG YOU BROKE IT" [code] That kind of thing? So it only fires off on an error message? Bit confused Steve Select Case variable Case A: code here Case B: Code here Case Else: On Error GoTo errHandler End Select errHandler: msgbox err.description & err.number etc etc unless you can do a case on error ??
penfold Posted August 6, 2012 Posted August 6, 2012 If it's a local printer, could you not just put a script on their computer in their startup folder which sets their local printer as default? That way the network printers should get installed and then it should overwrite the default printer with the local script.
Steve21 Posted August 6, 2012 Posted August 6, 2012 Select Case variable Case A: code here Case B: Code here Case Else: On Error GoTo errHandler End Select errHandler: msgbox err.description & err.number etc etc unless you can do a case on error ?? Shouldn't need to, putting the "on error" will follow the code below it, so doing it before the case statement will still fire it off, or should do Steve
mac_shinobi Posted August 6, 2012 Posted August 6, 2012 Shouldn't need to, putting the "on error" will follow the code below it, so doing it before the case statement will still fire it off, or should do Steve I couldn't get the on error thing to work, tried On Error GoTo errHandler loop code and any other code errHandler: msgbox "An error occured with description of " & err.description & " Error Number : " & err.Number I tried wscript.echo instead of msgbox, it showed me the error number but err.description was null / blank Tried the code you posted above with the same Case Else : On Error GoTo errHandler or whatever the tag was for handling errors and didn't work. I tried using On Error Resume Next and also did an err.Clear or err.Raise etc An example dummy script would be good if you get a few mins just so I can see the error handling actually working.
john Posted August 7, 2012 Author Posted August 7, 2012 Wow so much discussion over a simple script!! I haven't had 10 minutes to remotely logon to the users PC to check but on my test system here it works great but will drop you a note Steve if it has any moments Cheers all for the suggestions and advice and pointers
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now