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.
'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
'Uses the acquired OU name to determin which printer should be allocated, then allocates the printer
Select Case ComputerOU
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