Link to home
Start Free TrialLog in
Avatar of WNottsC
WNottsCFlag for Afghanistan

asked on

Adding network printers

We have a fair few printers in our organisation and at the moment they are not published in AD and our director does not want the user to have to find a printer to add to their machine but would like the to be automatticaly added.

We have achieved this through a vbscript and an AD group the same name as the printer share that containes the computers the printer shoud be added to.  The script will search the group membership of the computers untill it finds one that begins with "Printer Group", at which point it will use the rest of the name of the group which should match a printer share to add the printer.  The issue I have not been able to sort out yet is when there are more than one network printer in the room.  I need to add a loop to the vbscript to loop through all group membership and run add the printer each time a group starting "Printer Group" is found.

As well as the vbscript issue above surely there is a better way to automatically add the printers to the machines without a group for each printer or to add a group policy for each group if they are published in AD?
Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, there are other ways if you want a script.  Here's one that maps the specified printers depending on what you put in the array.  You specify a group name and the printer to pair to it.

Regards,

Rob.
' THIS LINE MUST BE PLACED IN THE MAIN CODE TO ALLOW THE DICTIONARY
' OBJECT TO REMAIN PERSISTENT FOR THE ISMEMBEROFGROUP FUNCTION
Dim objMemberships

Set objNetwork = CreateObject("WScript.Network")
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
arrPrinters = Array( _
	"Instructors~\\server\Printer1", _
	"Office Users~\\server\Printer2", _
	"Office Users~\\server\Printer3", _
	"Office Users~\\server\Printer4", _
	"Super Administrators~\\server\Printer1", _
	"Super Administrators~\\server\Printer2", _
	"Super Administrators~\\server\Printer6", _
	"Super Administrators~\\server\Printer7", _
	"Super Administrators~\\server\Printer8" _
	)

For Each strPrinterList In arrPrinters
	strGroup = Split(strDriveList, "~")(0)
	strPrinterPath = Split(strDriveList, "~")(1)
	If IsMemberOfGroup(objUser, strGroup) = True Then
		On Error Resume Next
		objNetwork.AddWindowsPrinterConnection strPrinterPath
        If Err <> 0 then 
			WScript.Echo "Unable to add printer " & strPrinterPath & VbCrLf & _
				"Please contact your network administrator." 
			Err.Clear
        End If 
	End If
Next

Function IsMemberOfGroup(objADUser, strGroupCN)
	If IsEmpty(objMemberships) = True Then
		Set objMemberships = CreateObject("Scripting.Dictionary")
		objMemberships.Add LCase("ALL"), 0
		If IsNull(objADUser.MemberOf) = False Then
			If TypeName(objADUser.MemberOf) = "String" Then
				objMemberships.Add LCase(Mid(Split(objADUser.MemberOf, ",")(0), 4)), 0
			Else
				For Each strGroupName In objADUser.MemberOf
					objMemberships.Add LCase(Mid(Split(strGroupName, ",")(0), 4)), 0
				Next
			End If
		End If
	End If
	If objMemberships.Exists(LCase(strGroupCN)) = True Then
		IsMemberOfGroup = True
	Else
		IsMemberOfGroup = False
	End If
End Function

Open in new window

Avatar of WNottsC

ASKER

did I mention I have 300 plus printers, his would be a large array to write.  I just thought microsoft would have developed  an easier way but I surpose that its what published printers in AD is for.
Eeek!  OK, so that's out of the question!

Well, in your current script, does it query the user's group, but just stop after one?  Could you show that group enumeration?  It should be easy enough to be able to add a user to multiple groups and add the printer from each one.

Rob.
Avatar of WNottsC

ASKER

I have added the code I use for adding a printer.  I think what I need is for the script to include a loop so it goes through every group member forthe computer and then if this starts with "Printer Group " it should then run the sub.

 
Dim sDNSDomain, oTrans, sNetBiosDomain, sAdsPath, sComputer, oUser
Dim colGroups, strGroup, v
Dim WshNetwork, PrinterShare

Set WshNetwork = WScript.CreateObject("WScript.Network")

' Determine DNS domain name from RootDSE object.
Dim oRoot : Set oRoot = GetObject("LDAP://RootDSE")
sDNSDomain = oRoot.Get("DefaultNamingContext")


' Use the NameTranslate object to find the NetBIOS domain name from the DNS domain name.
Set oTrans = CreateObject("NameTranslate")
oTrans.Init 3, sDNSDomain
oTrans.Set 1, sDNSDomain
sNetBIOSDomain = oTrans.Get(3)


' Use NameTranslate to convert the NT user name to the DistinguishedName required for the LDAP provider.
oTrans.Init 1, Left(sNetBIOSDomain, Len(sNetBIOSDomain) - 1)
oTrans.Set 3, sNetBIOSDomain & sNTName
sAdsPath = oTrans.Get(1)

' Bind to the user object in AD with the LDAP provider.
Set oUser = GetObject("LDAP://" & sAdsPath)

' Use NameTranslate to convert the NT name of the computer to the DistinguishedName required by LDAP.
' Computer names must end with "$".
sComputer = WshNetwork.ComputerName

oTrans.Set 3, sNetBIOSDomain & sComputer & "$"
sAdsPath = oTrans.Get(1)

' Bind to the computer object in AD with the LDAP provider.
Set oComputer = GetObject("LDAP://" & sAdsPath) 



AddPrinter sAdsPath, "Printer Group " 'Modified by Wayne Hale



Sub AddPrinter(ByVal UserOrComputerDN, ByVal GroupName) 
	On Error Resume Next
	Dim objUserOrComputer
	Dim objGroup
	Dim sGroup
	Dim colGroups
 	
	'Bind user or computer AD object.
	Set objUserOrComputer = GetObject("LDAP://" & UserOrComputerDN)

	If ((Err.Number = 0) And (IsObject(objUserOrComputer))) Then
		'Get the collection of groups the object is a member of.
		colGroups = objUserOrComputer.MemberOf
		If (Not IsEmpty(colGroups)) Then
				'Object is a member of multiple groups.
				'Iterate through the groups the user is a member of and see
				'if any of them match the specified group.
				
				'Loop through all groups
				For Each sGroup In colGroups
					Set objGroup = GetObject("LDAP://" & sGroup)
					
					If ((Err.Number = 0) And (IsObject(objGroup))) Then
						'Check if group matches search criteria
						If InStr(LCase(objGroup.CN), LCase(GroupName)) Then 'Modified by Wayne Hale
							'Add group to list of matching groups
							'MsgBox "Printer Group Found"
							WshNetwork.AddWindowsPrinterConnection "\\PRINT-server\" & LTrim(Replace(objGroup.CN, GroupName, "")) 'Modified by Wayne Hale
							WshNetwork.SetDefaultPrinter "\\PRINT-server\" & LTrim(Replace(objGroup.CN, GroupName, ""))
						End If
					End If
				Next
		End If
	End If
 
	'Dispose objects
	Set objGroup = Nothing
	Set objUserOrComputer = Nothing
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial