'******************************************************************************************* '* '* '* Purpose: Connect user to a networked printer depending on the machines Global Group membership '* '* if the machine is a member of a group beginning with prt the subroutine '* extract the servername and printername from the group name and attempt to '* connect to the printer. '* '* Therefore to add a printer to a machine, simply add the machine to the printers group '* If the user set a default printer, this will remain the default. '* this has not been tested on roaming profiles/ '* group format = prt_printername_servername '* NB: the printer and server name cannot include _ '* '******************************************************************************************* 'on error resume Next 'Ignore any error and continue ( when debugging comment this line out) '* Declare variables Dim objWSHNetwork 'Network object for connecting to printers Dim objComputer 'local computer object which has group membership Dim objMember 'group membership Dim strComputerName 'The local computername Dim strObjPath 'The object path of the computer in the directory Dim strPrinter 'Printer name Dim strPrintServer 'Printserver name Dim strConnectString 'Connection String Dim strEventLogText 'Eventlog text Set objSysInfo = CreateObject("ADSystemInfo") Set objWSHShell = WScript.CreateObject("WScript.Shell") strComputerName = objSysInfo.ComputerName 'Get the DN of this computer strObjPath = "LDAP://" & strComputerName 'Get the Object Path of this computer in the directory Set objComputer = GetObject(strObjPath) 'Setup the computer object from the directory Set objWSHNetwork = CreateObject("WScript.Network") 'Setup the network object ready to add printers For Each objMember In objComputer.Groups 'Enumerate all the group this computer is a member of if left(lcase(objMember.Name),6)="cn=prt" Then ' test group to see if it is a printer setting strPrinter="" 'The next three lines extract the printername and printservername from the global group name strPrinter=right(objMember.Name,len(objMember.Name)-7) strPrinter=left(strPrinter,InStr(1,strPrinter,"_")-1) strPrintServer=right(objMember.Name,(len(objMember.Name)-len(strPrinter))-8) strConnectString="\\"& strPrintServer & "\" & strPrinter 'build the connection string objWSHNetwork.AddWindowsPrinterConnection strConnectString 'connect the network printer strEventLogText="ConnectNetworkPrinter: Connecting " & strConnectString 'Create eventlog text objWSHShell.LogEvent 4, strEventLogText 'log and event showing which printer has been added End If Next Set objComputer = Nothing 'deallocate the computer object Set objWSHNetwork = Nothing 'deallocate the network object