Add a Network Printer Logon Script

Many people have been asking about adding network printers using a logon script so here goes. If you want to add network printers to groups of computers here is some script to help you out. What the script does is acquire the location of the computer within in the active directory. It then finds the last Organisational Unit the computer is in and an assigns a printer dependant on that organisational unit. For instance you have 3 offices each one with a different network printer. You organise the computers into seperate groups for each office in the Active directory and then assign a printer dependant on which "office" they are in. The code has comments to help you understand the what each line does.

The Code

'Define Objects to pull information from
Set objNetwork = CreateObject("WScript.Network")
Set objSysInfo = CreateObject("ADSystemInfo")
Set objShell = WScript.CreateObject("WScript.Shell")

' Reset network printers in roaming user profile
objShell.Run("%comspec% /c %LOGONSERVER%\netlogon\Con2Prt /F"), 0, True

'Gets Computers FULL Name from AD in form CN=#####, OU=#####, OU=######, DC=####### DC=#######
strComputer = objSysInfo.ComputerName
Set objComputer = GetObject("LDAP://" & strComputer)

'Splits the full string into an array. Each new item in array is created where seperated by a comma
arrOUs = Split(objComputer.Parent, ",")

'Removes all the headings for the OU i.e removes CN= , OU =, DC =
arrMainOU = Split(arrOUs(0), "=")

'Assigns ComputerOU the name of the computers organisational unit
ComputerOU = arrMainOU(1)

'Removes any existing networked printers to provide
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

'Uses the acquired OU name to determin which printer should be allocated, then allocates the printer
Select Case ComputerOU

Case "Office1"
objNetwork.AddWindowsPrinterConnection "\\servername\printername1"
objNetwork.SetDefaultPrinter("\\servername\printername1")

Case "Office2"
objNetwork.AddWindowsPrinterConnection "\\servername\printername2"
objNetwork.SetDefaultPrinter("\\servername\printername2")

Case "Office3"
objNetwork.AddWindowsPrinterConnection "\\servername\printername3"
objNetwork.SetDefaultPrinter("\\servername\printername3")

End Select

Additional Code

if you wish to add some printers to a specific computer we can use a case statement using the ItemName rather than the ComputerOU and add it to the code above as you still need the object definations set in previous code.

'Assigns itemname the name of the object identifyer (unique name) on the network.
ItemName = objNetwork.ComputerName

Select Case ItemName

'Use the computer name (the one in the Active directory) to determine laptops below are in textiles and add relevant printer

Case "Comp-01"
objNetwork.AddWindowsPrinterConnection "\\servername\printername1"
objNetwork.SetDefaultPrinter("\\servername\printername1")

End Select




Disclaimer: By using any of the software mentioned in this document you are adhering to their terms and conditions of use. I do not accept any responsibility for any loss, damage or disruption to your data or computer system that may occur while using the software mentioned in this document.
Site Map | Privacy Policy | Contact Us
| Best viewed in 1024 * 768 or above | ©2008 www.MyPcHealth.co.uk