again some old code I have written that need airing.
Have you ever wanted to have users automatically connect to the printer nearest the workstation they are logging into. We here is how it is done.
- Create a printer queue(daqueue) on printserver (pserver)
- Create a global group prt_daqueue_pserver
- Add the workstation accounts which are closest to the printer (daqueue).
- Now use the following sub in a users start-up script and hey presto the printer is mapped.
'*******************************************************************************************
'*
'*
'* 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
Create a printer queue(daqueue) on printserver (pserver)
Create a global group prt_daqueue_pserver
Add the workstation accounts which are closest to the printer (daqueue).
Now use the following sub in a users start-up script and hey presto the printer is mapped.
Advertisement