+ Post New Thread
Results 1 to 11 of 11
Scripts Thread, VB script to delete printers on logoff in Coding and Web Development; Hi Does anyone have a VB Script to delete network printers at logoff without having to name each printer? I've ...
  1. #1
    jgcracknell's Avatar
    Join Date
    Mar 2009
    Location
    Somewhere
    Posts
    93
    Thank Post
    5
    Thanked 1 Time in 1 Post
    Rep Power
    20

    VB script to delete printers on logoff

    Hi

    Does anyone have a VB Script to delete network printers at logoff without having to name each printer? I've got some staff profiles with some dud printers dating back a while.

    thanks

    J.

  2. #2

    ZeroHour's Avatar
    Join Date
    Dec 2005
    Location
    Edinburgh, Scotland
    Posts
    5,641
    Thank Post
    895
    Thanked 1,314 Times in 798 Posts
    Blog Entries
    1
    Rep Power
    444
    Yeh I do, I have a script as well that can be enabled to reset local machine drivers for network printers too if you like.
    I will post them tomorrow when I am in work.

  3. #3
    jgcracknell's Avatar
    Join Date
    Mar 2009
    Location
    Somewhere
    Posts
    93
    Thank Post
    5
    Thanked 1 Time in 1 Post
    Rep Power
    20
    Hi

    They both sound excellent. I am not a VB programmer myself and compared with how messy bat files are for just adding printers it really tidies up the whole process of printer delivery.

    Looking forward to trying them out.

    Thanks.

    J.

  4. #4

    ZeroHour's Avatar
    Join Date
    Dec 2005
    Location
    Edinburgh, Scotland
    Posts
    5,641
    Thank Post
    895
    Thanked 1,314 Times in 798 Posts
    Blog Entries
    1
    Rep Power
    444
    I will just give you complete templates of logon/logoff and reset drivers scripts then?
    The logon script adds and sets defaults based on the pc name.

  5. #5

    Ric_'s Avatar
    Join Date
    Jun 2005
    Location
    London
    Posts
    7,590
    Thank Post
    109
    Thanked 762 Times in 593 Posts
    Rep Power
    180

  6. #6
    neon's Avatar
    Join Date
    Apr 2009
    Location
    Market Harborough
    Posts
    222
    Thank Post
    7
    Thanked 22 Times in 20 Posts
    Rep Power
    14
    Ive got this if its any use to you mate.

    Option Explicit

    Dim objNetwork
    Dim objPrinters, ix
    Set objNetwork = CreateObject("WScript.Network")
    Set objPrinters = objNetwork.EnumPrinterConnections

    ' ### Delete all currently installed network printers
    For ix= 0 to objPrinters.Count - 1 Step 2
    Dim sPrinter, sUNCPath
    sPrinter = objPrinters.Item(ix)
    sUNCPath = objPrinters.Item(ix+1)

    If InStr(1, sPrinter, "\\SERVER", 1) > 0 Then
    objNetwork.RemovePrinterConnection sPrinter, True, True
    ElseIf InStr(1, sUNCPath, "\\SERVER", 1) > 0 Then
    objNetwork.RemovePrinterConnection sUNCPath, True, True
    End If

    If Err <> 0 Then
    Wscript.Echo "Error deleting printer: " & Err.Description
    Err.Clear
    End If
    Next

  7. #7
    ricki's Avatar
    Join Date
    Jul 2005
    Location
    uk
    Posts
    1,475
    Thank Post
    20
    Thanked 164 Times in 157 Posts
    Rep Power
    52
    Hi

    I dont use vb script but I do use

    \\server\NETLOGON\CON2PRT /F

    dont forget to put the con2prt in the netlogon lols.

    I put it in my printer script at logon and not in the logoff script

    Richard

  8. #8

    mattx's Avatar
    Join Date
    Jan 2007
    Posts
    9,240
    Thank Post
    1,058
    Thanked 1,068 Times in 625 Posts
    Rep Power
    740
    Code:
    rem ** Do Not Delete Next Line ** 
    Set WshNetwork = CreateObject("WScript.Network")
    
    rem ** This will delete existing printers **
    On Error Resume Next
    Set oPrinters = WshNetwork.EnumPrinterConnections
            For i = 1 to oPrinters.Count - 1 Step 2
                  WshNetwork.RemovePrinterConnection oPrinters.Item(i)
            Next

  9. #9

    ZeroHour's Avatar
    Join Date
    Dec 2005
    Location
    Edinburgh, Scotland
    Posts
    5,641
    Thank Post
    895
    Thanked 1,314 Times in 798 Posts
    Blog Entries
    1
    Rep Power
    444
    Here is the logon script we use for printers. When naming machines we use the form:
    {3 digits}-{3 digits}-{asset tag}
    i.e. LAN-R42-7065 (languages room 42 pc 7065)
    This allows us to allocate default printers to rooms.
    Variables to change:
    deparment (is currently pulls the first 3 characters of pc name for this)
    p_server (is your printer server)
    department_location (is currently pulling the 2nd batch of 3 characters in the middle)
    {PRINT SERVER} which should only be in one place as PRINTSVR (no \'s etc)

    The main function is mapprinter which maps the printers. My script also automatically creates event log entries as it runs so if a person has 3/5 printers you can check the event log to see what the error was potentially.
    Also when its done adding printers it will show a popup telling the users as well

    I am working on the other scripts for you too.

    Code:
    On Error Resume Next
    Dim objShell
    Dim WshNetwork
    Dim department,department_location,p_server
    Set objShell = wscript.createObject("wscript.shell")
    Set WshNetwork = WScript.CreateObject("WScript.Network")
    Set oPrinters = WshNetwork.EnumPrinterConnections
    
    Const EVENT_SUCCESS = 0
    Const EVENT_FAILURE = 1
    Const EVENT_WARNING = 2
    Const EVENT_INFORMATION = 4
    
    pcname= UCase(WshNetwork.ComputerName)
    department = UCase(Left(pcname, 3))
    p_server = "\\rgc-print-01\"
    
    department_location = UCase(Mid(pcname, 5,3))
    if len(pcname)=12 then
    	department_machine = UCase(Mid(pcname, 9,4))
    else
    	department_machine = UCase(Mid(pcname, 9,3))
    end if
    
    '  ####################################### REMEMBER TO INCREMENT BELOW!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    version = "1.00"
    
    function LogtoEvent (MSG,success)
    	select case success
    		case 1	objShell.LogEvent EVENT_SUCCESS, MSG ' 1 is a success
    		case 0	objShell.LogEvent EVENT_FAILURE, MSG ' 0 is a error
    		case 2	objShell.LogEvent EVENT_WARNING, MSG ' 2 is a warning
    		case 3	objShell.LogEvent EVENT_INFORMATION, MSG ' do not use
    	end select
    end function
    
    LogtoEvent	"PRINTERS: Starting Printer Script Version: " & version,3
    
    unction speaktome(MSG, beforeaction, loopwith, extradelay)
    	LogtoEvent	"PRINTERS: Starting speaktome",3
    	objCharacter.Stop
    	objCharacter.Show
    	'LogtoEvent	"PRINTERS: Starting showing trev",3
    	if beforeaction > "" then
    		objCharacter.Play beforeaction
    	end if
    	
    	Set objRequest = objCharacter.Think (MSG)
    
    	Do While objRequest.Status > 0
    		if objCharacter.Visible = False Then
    			if extradelay = 1 then
    				objCharacter.Show
    				objCharacter.Play beforeaction
    				objCharacter.Think (MSG)
    				Wscript.Sleep 3000
    				exit Do
    			else
    				objRequest.Status = 0
    			end if
    		end if
        		Wscript.Sleep 100
    	Loop
    
    	'LogtoEvent	"PRINTERS: Done my talking",3
    
    	if extradelay = 1 then
    		Wscript.Sleep 2400
    	else
    		wscript.sleep 1000
    	end if
    
    	objCharacter.Stop
    	if loopwith > "" then
    		objCharacter.Play loopwith
    	end if
    	LogtoEvent	"PRINTERS: Ending speaktome",3
    end function
    
    function mapprinter(pname)
    	if not pname = "" then
    		LogtoEvent	"PRINTERS: Starting to add printer " & pname,1
    		set check = WSHNetwork.AddWindowsPrinterConnection(pname)
    		If Not check then
          			LogtoEvent	"PRINTERS: Failed adding Printer: '" & pname & "' Error Message is: '" & Err.Description & "'",0
    		End If
    		'LogtoEvent	"done",1
    		If Err.Number <> 0 Then
    			LogtoEvent	"PRINTERS: Failed adding Printer: '" & pname & "' Error Message is: '" & Err.Description & "'",0
    			Err.Clear
    		Else
    			LogtoEvent	"PRINTERS: Added Printer: '" & pname & "'",1
    		End If
    		'wscript.sleep(2000)
    		LogtoEvent	"PRINTERS: Finished adding printer " & pname,1
    		'LogtoEvent	"fffff",1
    '		speaktome "Added printer: " & Replace(pname,p_server & ""," "),"Suggest","Searching"
    '		WScript.Sleep(8000)
    	end if
    end function
    
    function setdefprinter(defprinter)
    	if defprinter > "" then
    		'WScript.Sleep(9000)
    		set check = WSHNetwork.SetDefaultPrinter(defprinter)
    		If Not check then
          			LogtoEvent	"PRINTERS: Failed adding Printer: '" & pname & "' Error Message is: '" & Err.Description & "'",0
    		End If
    		If Err.Number <> 0 Then
    			LogtoEvent	"PRINTERS: Failed setting default Printer: '" & defprinter & "' Error Message is: '" & Err.Description & "'",0
    			Err.Clear
    		Else
    			LogtoEvent	"PRINTERS: Default Printer: '" & defprinter & "'",1
    		End If
    		speaktome "PRINTERS: Your default printer is:" & replace(defprinter,p_server & ""," "),"Wave","Suggest",1
    	end if
    end function
    
    LogtoEvent	"PRINTERS: Added functions",1
    
    strAgentName = "Merlin"
    strAgentPath = "C:\Windows\Msagent\Chars\" & strAgentName & ".acs"
    Set objAgent = CreateObject("Agent.Control.2")
    
    objAgent.Connected = TRUE
    objAgent.Characters.Load strAgentName, strAgentPath
    Set objCharacter = objAgent.Characters.Character(strAgentName)
    LogtoEvent	"PRINTERS: Loaded Agent Trevor",1
    
    objCharacter.MoveTo 800, 600
    
    LogtoEvent	"PRINTERS: Starting removal of previous printers",1
    For i = 0 to oPrinters.Count - 1 Step 2
    	On Error Resume Next
    		Err.Clear
    		'oPrinters.Item(i) = LCase(oPrinters.Item(i))
    		if oPrinters.Item(i+1) <> "Adobe PDF" and Left(oPrinters.Item(i), 3) <> "lpt" And Left(oPrinters.Item(i), 3) <> "usb" or UCase((Left(oPrinters.Item(i+1), 8))) = p_server & "" or UCase(Left(oPrinters.Item(i+1), 8)) = "\\{PRINT SERVER}." then
    				LogtoEvent	"PRINTERS: Removing printer: " & oPrinters.Item(i+1),1
    				WshNetwork.RemovePrinterConnection oPrinters.Item(i+1), true, true
    				LogtoEvent	"PRINTERS: Removed printer: " & oPrinters.Item(i+1),1
    			end if
    		else
    			LogtoEvent	"PRINTERS: Ignored the remove of printer '" & oPrinters.Item(i+1) & "' as it was not on the print server or No PREVIOUS Printers found to remove",1
    		end if
    		If Err.Number <> 0 Then
    			LogtoEvent	"PRINTERS: Failed Removing Printer: '" & oPrinters.Item(i+1) & "' Error Message is: '" & Err.Description & "'",0
    		End If
    Next
    LogtoEvent	"PRINTERS: Finished removing previous printers",1
    
    ' This actually begins adding printers
    
    if department = "COM" then		
    		printer1 = p_server & "com-t2"
    		printer2 = p_server & "com-t3"
    
    
    		Select Case department_location
    			case "T02"	defprinter=printer1
    			case "T03"	defprinter=printer2
    		End Select
    
    		mapprinter printer1
    		mapprinter printer2
    		
    		setdefprinter defprinter
    END IF
    mapprinter p_server & "A PRINTER EVERONE GETS"
    
    'LogtoEvent	"66",1
    objCharacter.Show
    'Wscript.sleep(2000) THIS WAS 
    'LogtoEvent	"6666",1
    
    speaktome "Your printers are now ready.","","",1
    LogtoEvent	"PRINTERS: Script Finished",3
    Last edited by ZeroHour; 8th December 2009 at 04:46 PM.

  10. #10

    ZeroHour's Avatar
    Join Date
    Dec 2005
    Location
    Edinburgh, Scotland
    Posts
    5,641
    Thank Post
    895
    Thanked 1,314 Times in 798 Posts
    Blog Entries
    1
    Rep Power
    444
    Here is my log off script. Please edit where it says {PRINT SERVER}

    Code:
    On Error Resume Next
    Dim objShell
    Dim WshNetwork
    Dim department,department_location
    Set objShell = wscript.createObject("wscript.shell")
    Set WshNetwork = WScript.CreateObject("WScript.Network")
    Set oPrinters = WshNetwork.EnumPrinterConnections
    Const EVENT_SUCCESS = 0
    Const EVENT_FAILURE = 1
    Const EVENT_WARNING = 2
    Const EVENT_INFORMATION = 4
    pcname= WshNetwork.ComputerName
    
    version = "1.00"
    
    function LogtoEvent (MSG,success)
    	select case success
    		case 1	objShell.LogEvent EVENT_SUCCESS, MSG ' 1 is a success
    		case 0	objShell.LogEvent EVENT_FAILURE, MSG ' 0 is a error
    		case 2	objShell.LogEvent EVENT_WARNING, MSG ' 2 is a warning
    		case 3	objShell.LogEvent EVENT_INFORMATION, MSG ' do not use
    	end select
    end function
    
    LogtoEvent	"R_PRINTERS: Starting Printer Removal Script Version: " & version,3
    
    LogtoEvent	"R_PRINTERS: Starting removal of previous printers",1
    For i = 0 to oPrinters.Count - 1 Step 2
    	On Error Resume Next
    		Err.Clear
    		'oPrinters.Item(i) = LCase(oPrinters.Item(i))
    		if oPrinters.Item(i+1) <> "Adobe PDF" and Left(oPrinters.Item(i), 3) <> "lpt" And Left(oPrinters.Item(i), 3) <> "usb" or UCase((Left(oPrinters.Item(i+1), 8))) = "\\{PRINT SERVER}\" or UCase(Left(oPrinters.Item(i+1), 8)) = "\\{PRINT SERVER}." then
    				LogtoEvent	"R_PRINTERS: Removing printer: " & oPrinters.Item(i+1),1
    				WshNetwork.RemovePrinterConnection oPrinters.Item(i+1), true, true
    				LogtoEvent	"R_PRINTERS: Removed printer: " & oPrinters.Item(i+1),1
    		else
    			LogtoEvent	"R_PRINTERS: Ignored the remove of printer '" & oPrinters.Item(i+1) & "' as it was not on the print server or No PREVIOUS Printers found to remove",1
    		end if
    		If Err.Number <> 0 Then
    			LogtoEvent	"R_PRINTERS: Failed Removing Printer: '" & oPrinters.Item(i+1) & "' Error Message is: '" & Err.Description & "'",0
    		End If
    Next
    LogtoEvent	"R_PRINTERS: Finished removing previous printers",1

  11. #11

    Join Date
    Dec 2009
    Posts
    603
    Thank Post
    8
    Thanked 36 Times in 34 Posts
    Rep Power
    17
    I actually Remove my printers on Logon , I find it a lot better and saves usign another script . Basically it removes all printers from all rooms .. And Using loopback Policy just reapplys the printers it needs Per OU.

    So basically i have 4 rooms , so i put this in 4 OU's where my PC's are kept . And all i change is the adding new printers part

    ON ERROR RESUME NEXT

    Set objPrinters = WScript.CreateObject("WScript.Network")
    Set objShell = WScript.CreateObject("WScript.Shell")

    'Remove old printers
    objPrinters.RemovePrinterConnection "\\SERVERNAME\PRINTERNAME", True, True
    objPrinters.RemovePrinterConnection "\\SERVERNAME\PRINTERNAME", True, True
    objPrinters.RemovePrinterConnection "\\SERVERNAME\PRINTERNAME", True, True
    objPrinters.RemovePrinterConnection "\\SERVERNAME\PRINTERNAME", True, True
    objPrinters.RemovePrinterConnection "\\SERVERNAME\PRINTERNAME", True, True
    objPrinters.RemovePrinterConnection "\\SERVERNAME\PRINTERNAME", True, True

    'Install new Printers
    objPrinters.AddWindowsPrinterConnection "\\SERVERNAME\PRINTERNAME"
    objPrinters.AddWindowsPrinterConnection "\\SERVERNAME\PRINTERNAME"

    'set default Printer
    objPrinters.SetDefaultPrinter "\\SERVERNAME\PRINTERNAME"


    'Synchronizes the time with Server
    objShell.Run "cmd /c NET TIME \\SERVERNAME /set /y", 0, True

    'Clean Up Memory Used
    set objShell = Nothing
    Set objPrinters = Nothing

    wscript.quit
    Last edited by 2097; 14th December 2009 at 10:45 AM.

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 0
    Last Post: 7th September 2009, 01:27 PM
  2. logoff script to delete registry key
    By ICMC in forum Scripts
    Replies: 3
    Last Post: 26th January 2009, 10:37 PM
  3. Logoff Script problems and GPO
    By placky in forum Windows
    Replies: 3
    Last Post: 27th March 2008, 06:03 PM
  4. Delete Profile on LogOff
    By roland in forum How do you do....it?
    Replies: 11
    Last Post: 20th September 2007, 09:43 PM
  5. Replies: 7
    Last Post: 27th November 2005, 10:41 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •