Jump to content

Recommended Posts

Posted

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.

Posted

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.

Posted

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.

Posted

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

Posted

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

Posted
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

Posted (edited)

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.

 

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

Edited by ZeroHour
Posted

Here is my log off script. Please edit where it says {PRINT SERVER}

 

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

Posted (edited)

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

Edited by 2097

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...