Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

Recommended Posts

Posted

Used Maniac's and (at least one others) scripts to make my own.

 

Removes all network printers (leaves local ones) and uses Maniacs script to add printers according to the container OU of the computer.

 

I've got it working as a logon script for users, anyone know if it's possible to use it as a startup script for computer accounts? I can't seem to get it to run that way! Also if anyone is bored feel free to tidy it up - I'm no scripting genius like many on here :D

 

One question though, would it be possible to make it completely skip the whole thing (deletion of printers and adding new ones) if the computer isn't in one of the UOs listed?

 

=================

on error resume next

'Set variables.

 

Set shell= WScript.CreateObject("WScript.Shell")

Set WshNetwork = WScript.CreateObject("WScript.Network")

dim Counter, PrinterPath

 

'Remove All Network Printers.

 

Set oPrinters = WshNetwork.EnumPrinterConnections

FOR Counter = 0 to oPrinters.Count - 1

IF mid(oPrinters.Item(Counter), 1, 2) = "\\" THEN

PrinterPath = oPrinters.Item(Counter)

WshNetwork.RemovePrinterConnection PrinterPath, True, True

End If

Next

'Set even more variables.

Set objWshNetwork = CreateObject("WScript.Network")

Set objAdsSystemInfo = CreateObject("adsysteminfo")

Set objComputerName = GetObject("LDAP://" & objAdsSystemInfo.ComputerName)

Set objOU = GetObject(objComputerName.Parent)

strOU = replace(objOU.Name,"OU=","")

 

Dim computerName

computerName = LCase(WshNetwork.ComputerName)

 

'Delete existing connections to network printers

 

For i = 0 to oPrinters.Count - 1 Step 2

On Error Resume Next

if Left(oPrinters.Item(i), 3) <> "lpt" And Left(oPrinters.Item(i), 3) <> "usb" then

WshNetwork.RemovePrinterConnection oPrinters.Item(i+1), true, true

else WScript.Echo "No network printers found"

end if

'Add printers depending on AD OU containing computer.

Next

 

Select Case strOU

Case "IT1"

objWshNetwork.AddWindowsPrinterConnection "\\print\Dell1700 IT1"

objWshNetwork.SetDefaultPrinter "\\PRINT\Dell1700 IT1"

objWshNetwork.AddWindowsPrinterConnection "\\print\HP2500C IT1"

 

Case "IT2"

objWshNetwork.AddWindowsPrinterConnection "\\print\Dell 1700n IT2"

objWshNetwork.SetDefaultPrinter "\\PRINT\Dell 1700n IT2"

 

 

End Select

=================

 

I trawled through a lot of the scripts on this forum to put this together so none of it is my own work!

 

The reasons I wanted it to delete the printers before creating new ones is it'll remove any problems of moving computers around between containers etc.

Posted

Also worth noting that the current script will only work if you have a single OU layer.

 

ie: ou=foo, cn=foos, dc=my, dc=local

 

... this little function (thanks AJbritton) helps in other instances.

' Check if user is member of an Organisational Unit
Function UserInOU(sOU)
' Returns true if the user is a member of the specified OU (or descendant)
Dim oAdSys, sUserDN
Set oAdSys = CreateObject("ADSystemInfo")
sUserDN = UCase(oAdSys.UserName)
UserInOU = (InStr(1, sUserDN, UCase(sOU)) > 0)
End function

Posted

Hi all,

 

I've changed the script quite a bit this morning as it wasn't quite doing what I wanted it to. No doubt this can be done in a neater way as I'm very much a VB script newbie!

 

Essentially the script checked the UO the workstation is in, if it matches one of the list then it deletes all network printers and adds in specified printers and makes one default.

 

I wanted it to delete all printers as I have a few laptops that are on loan between departments and I can simply drag them into the different UO as the departments use them, the staff get very annoyed if their print outs appear in the other departments printers so I simply stop this happening by removing the bloomin printers!

 

It works for me so far, still have to test it a little with students more restrictive accounts though.

 

 

==================

on error resume next

'Set variables.

 

Set shell= WScript.CreateObject("WScript.Shell")

Set WshNetwork = WScript.CreateObject("WScript.Network")

dim Counter, PrinterPath

Set objWshNetwork = CreateObject("WScript.Network")

Set objAdsSystemInfo = CreateObject("adsysteminfo")

Set objComputerName = GetObject("LDAP://" & objAdsSystemInfo.ComputerName)

Set objOU = GetObject(objComputerName.Parent)

strOU = replace(objOU.Name,"OU=","")

 

Dim computerName

computerName = LCase(WshNetwork.ComputerName)

 

'Add printers depending on AD OU containing computer.

 

if strOU="IT1" then

Set oPrinters = WshNetwork.EnumPrinterConnections

FOR Counter = 0 to oPrinters.Count - 1

IF mid(oPrinters.Item(Counter), 1, 2) = "\\" THEN

PrinterPath = oPrinters.Item(Counter)

WshNetwork.RemovePrinterConnection PrinterPath, True, True

End If

objWshNetwork.AddWindowsPrinterConnection "\\print\Dell1700 IT1"

objWshNetwork.SetDefaultPrinter "\\PRINT\Dell1700 IT1"

objWshNetwork.AddWindowsPrinterConnection "\\print\HP2500C IT1"

Next

elseif strOU="IT2" then

Set oPrinters = WshNetwork.EnumPrinterConnections

FOR Counter = 0 to oPrinters.Count - 1

IF mid(oPrinters.Item(Counter), 1, 2) = "\\" THEN

PrinterPath = oPrinters.Item(Counter)

WshNetwork.RemovePrinterConnection PrinterPath, True, True

End If

objWshNetwork.AddWindowsPrinterConnection "\\print\Dell 1700n IT2"

objWshNetwork.SetDefaultPrinter "\\PRINT\Dell 1700n IT2"

Next

elseif strOU="Mathematics" then

Set oPrinters = WshNetwork.EnumPrinterConnections

FOR Counter = 0 to oPrinters.Count - 1

IF mid(oPrinters.Item(Counter), 1, 2) = "\\" THEN

PrinterPath = oPrinters.Item(Counter)

WshNetwork.RemovePrinterConnection PrinterPath, True, True

End If

objWshNetwork.AddWindowsPrinterConnection "\\print\HP LJ2100 Maths"

objWshNetwork.SetDefaultPrinter "\\PRINT\HP LJ2100 Maths"

Next

else

'No printers added or deleted

End if

==============

Posted

I would probably clean it up abit:

 

on error resume next
'Set variables.

Set shell= WScript.CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
dim Counter, PrinterPath
Set objWshNetwork = CreateObject("WScript.Network")
Set objAdsSystemInfo = CreateObject("adsysteminfo")
Set objComputerName = GetObject("LDAP://" & objAdsSystemInfo.ComputerName)
Set objOU = GetObject(objComputerName.Parent)
strOU = replace(objOU.Name,"OU=","")
Set oPrinters = WshNetwork.EnumPrinterConnections

Dim computerName
computerName = LCase(WshNetwork.ComputerName)

'Add printers depending on AD OU containing computer.

Select Case strOU
Case "IT1"
       	removePrinters
	addPrinter "\\print\Dell1700 IT1",True
	addPrinter "\\print\HP2500C IT1",False
Case "IT2"
	removePrinters
	addPrinter "\\print\Dell 1700n IT2",True
Case "Mathematics"
	removePrinters
	addPrinter "\\print\HP LJ2100 Maths",True

end select

function removePrinters()

FOR Counter = 0 to oPrinters.Count - 1
	IF mid(oPrinters.Item(Counter), 1, 2) = "\\" THEN
		PrinterPath = oPrinters.Item(Counter)
		WshNetwork.RemovePrinterConnection PrinterPath, True, True
	end if
next

end function

function addPrinter(strConnectString,isDefault)

	if strConnectString <> "" then
			strResult = WshNetwork.AddWindowsPrinterConnection(strConnectString)
		if isDefault then
			WshNetwork.SetDefaultPrinter strConnectString
		end if
	end if
end function

 

somehting like this should work.. i think

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...