talksr Posted July 30, 2013 Posted July 30, 2013 Hi there, I am currently experiencing some problems at a school I have been supporting. The school currently have 4 networked printers which are shared out to all users via the Active Directory Logon script. The script looks like this: @Echo Off Net Use YXC \\servername\netlogon\con2prt.exe /c \\servername\Printer1 \\servername\netlogon\con2prt.exe /c \\servername\Printer2 \\servername\netlogon\con2prt.exe /c \\servername\Printer3 \\servername\netlogon\con2prt.exe /c \\servername\Printer4 The script appears to have worked well, however, I am finding that when I log on to a random system as any user, there are about 10 printers. They seem to be old networked printers which no longer exist. The fact these are showing on normal user network accounts is strange. I can't see anything in the group policy. THis really needs tidying up as people keep sending jobs to the non existant printers. Can anyone suggest ways I can solve this mess?
EPCHS Posted July 30, 2013 Posted July 30, 2013 We use a vbs to map them, and before mapping them we put: Set WSHPrinters = WSHNetwork.EnumPrinterConnections For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2 'To remove only networked printers use this If Statement If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True End If This clears off the existing network printers before connecting the new ones. 1
talksr Posted July 30, 2013 Author Posted July 30, 2013 Hi there, thanks for your post. I have never mapped them via VBS before. Would you just point the script path on Active Directory to the vbs file in the NetLogon Folder? I don't suppose you could show me an example of your script with a network printer included and I will give it a go here? Thanks for the removal part of the vbs script, very useful.
AJWhite1970 Posted July 30, 2013 Posted July 30, 2013 On Error resume Next set objNetwork = WScript.CreateObject("WScript.Network") set objShell = WScript.CreateObject("WScript.Shell") 'Add Network printers objNetwork.AddWindowsPrinterConnection "\\quartz\HP Colour LaserJet CP2025n ART01" 'Set Default Printer objNetwork.SetDefaultPrinter "\\quartz\HP Colour LaserJet CP2025n ART01" 1
talksr Posted July 30, 2013 Author Posted July 30, 2013 Hi there, here is what I have knocked up based on your helpful posts: Set WSHPrinters = WSHNetwork.EnumPrinterConnections For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2 'To remove only networked printers use this If Statement If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True End If On Error resume Next set objNetwork = WScript.CreateObject("WScript.Network") set objShell = WScript.CreateObject("WScript.Shell") 'Add Network printers objNetwork.AddWindowsPrinterConnection "\\smcessvr\ICT-SUITE" 'Set Default Printer objNetwork.SetDefaultPrinter "\\smcessvr\ICT-SUITE" WScript.quit It is telling me the following when it runs: Expected 'Next' on Line 17, char 1. Line 17 would be the blank line after WScript.quit. What am I missing here?
ChrisH Posted July 30, 2013 Posted July 30, 2013 You have started a for loop For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2 But you haven't defined an end to it. Try this Set WSHPrinters = WSHNetwork.EnumPrinterConnections For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2 'To remove only networked printers use this If Statement If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True End If next On Error resume Next set objNetwork = WScript.CreateObject("WScript.Network") set objShell = WScript.CreateObject("WScript.Shell") 'Add Network printers objNetwork.AddWindowsPrinterConnection "\\smcessvr\ICT-SUITE" 'Set Default Printer objNetwork.SetDefaultPrinter "\\smcessvr\ICT-SUITE" WScript.quit 1
talksr Posted July 30, 2013 Author Posted July 30, 2013 You have started a for loop For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2 But you haven't defined an end to it. Try this Set WSHPrinters = WSHNetwork.EnumPrinterConnections For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2 'To remove only networked printers use this If Statement If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True End If next On Error resume Next set objNetwork = WScript.CreateObject("WScript.Network") set objShell = WScript.CreateObject("WScript.Shell") 'Add Network printers objNetwork.AddWindowsPrinterConnection "\\smcessvr\ICT-SUITE" 'Set Default Printer objNetwork.SetDefaultPrinter "\\smcessvr\ICT-SUITE" WScript.quit Hi there. Thanks for your help. I have not had to look at Visual Basic for quite some time, and even when I have, it has just been tweaking stuff others have already done. Maybe I should get on a course. I have tried your updated version, it now says that on line one, Object required: 'WSHNetwork'. I can see it uses WSHNetwork, so im assuming I need to declare it somewhere?
AJWhite1970 Posted July 30, 2013 Posted July 30, 2013 Alternate printer delete script:- strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colInstalledPrinters = objWMIService.ExecQuery _ ("Select * From Win32_Printer Where Network = True") For Each objPrinter in colInstalledPrinters objPrinter.Delete_ Next
talksr Posted July 30, 2013 Author Posted July 30, 2013 Alternate printer delete script:- strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colInstalledPrinters = objWMIService.ExecQuery _ ("Select * From Win32_Printer Where Network = True") For Each objPrinter in colInstalledPrinters objPrinter.Delete_ Next Hi there, that script worked very well and without any errors. I am thinking of adding that VBS to the existing netlogon bat file. At the moment, when I reference the VBS script, the computer is trying to look in C:\ of the local machine rather than netlogon. How can I overcome this? The cmd error shown is: Input Error: Can not find script file "C:\Windows\remprinters.vbs". Press any key to continue . . . The batch script is as follows: @ECHO OFF cscript.exe remprinters.vbs pause 10 NET USE I: \\smcessvr\APPLIC /PERSISTENT:NO NET USE O: \\smcessvr\SHARED /PERSISTENT:NO NET USE T: \\smcessvr\STAFFSHARED /PERSISTENT:NO NET use u: \\smcessvr\ClassHome /persistent:no \\smcessvr\netlogon\con2prt.exe /c \\smcessvr\Staffworkroom \\smcessvr\netlogon\con2prt.exe /c \\smcessvr\ICT-Suite \\smcessvr\netlogon\con2prt.exe /c \\smcessvr\StaffRMPrinter \\smcessvr\netlogon\con2prt.exe /c \\smcessvr\Reception-Building \\smcessvr\netlogon\con2prt.exe /c \\smcessvr\StaffRMCopier
ChrisH Posted July 30, 2013 Posted July 30, 2013 Hi there. Thanks for your help. I have not had to look at Visual Basic for quite some time, and even when I have, it has just been tweaking stuff others have already done. Maybe I should get on a course. I have tried your updated version, it now says that on line one, Object required: 'WSHNetwork'. I can see it uses WSHNetwork, so im assuming I need to declare it somewhere? You need to move your declarations to the top set objNetwork = WScript.CreateObject("WScript.Network") set objShell = WScript.CreateObject("WScript.Shell")
talksr Posted July 31, 2013 Author Posted July 31, 2013 Hi there. Thanks for all of your help. This is what I have so far. It is working really well on Windows XP and 7 systems. But I am getting a Generic failure on line 9 of the vbs script when ran on a Windows 8 system. Here is my batch file: @ECHO OFF REM The following lines run my VBS script to remove all printers on computer. H: is mapped to run the script from NetLogon NET USE H: \\smcessvr\NETLOGON wscript "H:\remprinters.vbs" pause 5 REM End of Printer Removal Script NETUSE H: is removed on last line below: NET USE I: \\smcessvr\APPLIC /PERSISTENT:NO NET USE O: \\smcessvr\SHARED /PERSISTENT:NO NET USE T: \\smcessvr\STAFFSHARED /PERSISTENT:NO NET use U: \\smcessvr\ClassHome /PERSISTENT:NO net use H: /delete /Y REM Printers are added below: \\smcessvr\netlogon\con2prt.exe /c \\smcessvr\Staffworkroom \\smcessvr\netlogon\con2prt.exe /c \\smcessvr\ICT-Suite \\smcessvr\netlogon\con2prt.exe /c \\smcessvr\StaffRMPrinter \\smcessvr\netlogon\con2prt.exe /c \\smcessvr\Reception-Building \\smcessvr\netlogon\con2prt.exe /c \\smcessvr\StaffRMCopier And this is my VBS file with help from you guys: strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colInstalledPrinters = objWMIService.ExecQuery _ ("Select * From Win32_Printer Where Network = True") For Each objPrinter in colInstalledPrinters objPrinter.Delete_ Next
talksr Posted October 10, 2013 Author Posted October 10, 2013 (edited) Hi, I am currently running the following vbs script on Logon, but I am getting an error: strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colInstalledPrinters = objWMIService.ExecQuery _ ("Select * From Win32_Printer Where Network = True") For Each objPrinter in colInstalledPrinters objPrinter.Delete_ Next The error is as follows: Permission denied 'GetObject' Code: 800A0046 The line in question is: Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") I am logging in as a pupil. Could it be that they don't have the right access levels to run this vbs script? Any help would be great! Edited October 10, 2013 by talksr
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