Halfmad Posted June 22, 2007 Posted June 22, 2007 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 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.
Halfmad Posted June 22, 2007 Author Posted June 22, 2007 Ah that'd explain why I couldn't get it working at machine startup
contink Posted June 22, 2007 Posted June 22, 2007 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
Halfmad Posted June 22, 2007 Author Posted June 22, 2007 we only have a single layer at present but yeah that's pretty useful Contink
Halfmad Posted June 25, 2007 Author Posted June 25, 2007 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 ==============
apeo Posted June 25, 2007 Posted June 25, 2007 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
Halfmad Posted June 25, 2007 Author Posted June 25, 2007 Cheers Apeo, looks much more tidy. I've edited it and added in all of our printers. works like a dream :prayer: Apeo
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