Solved

Adding network printers

Posted on 2011-09-21
5
280 Views
Last Modified: 2012-05-12
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?
0
Comment
Question by:WNottsC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 36577977
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

0
 

Author Comment

by:WNottsC
ID: 36578656
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.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36578662
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.
0
 

Author Comment

by:WNottsC
ID: 36578737
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

0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 36580045
OK, looking at your code, I've done things a little more streamlined, which will hopefully work out for you.

Incidently though, I don't actually see any reason for your code not to have looped through all the groups, except for a case where your computer was only a member of one other group, other than the primary group of Domain Computers. If that *is* the case, then you were missing the logic condition at which point MemberOf only returns a string, and not an array.

Regards,

Rob.
Set objNetwork = CreateObject("WScript.Network")
Set objADSysInfo = CreateObject("ADSystemInfo")
sADsPath = objADSysInfo.UserName
sADsPath = objADSysInfo.ComputerName

strGroupStart = "Printer Group "

'Bind user or computer AD object.
Set objUserOrComputer = GetObject("LDAP://" & sADsPath)

If TypeName(objUserOrComputer.MemberOf) <> "Empty" Then
	If TypeName(objUserOrComputer.MemberOf) = "String" Then
		If LCase(Left(objUserOrComputer.MemberOf, 14)) = LCase(strGroupStart) Then AddPrinter objUserOrComputer.MemberOf
	Else
		'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 objUserOrComputer.MemberOf
			If LCase(Left(sGroup, 14)) = LCase("Printer Group ") Then AddPrinter sGroup
		Next
	End If
End If

Sub AddPrinter(sGroupDN)
	Set objGroup = GetObject("LDAP://" & sGroupDN)
	strPrinterPath = "\\Print-server\" & Replace(objGroup.CN, strGroupStart, "")
	On Error Resume Next
	objNetwork.AddWindowsPrinterConnection strPrinterPath
	objNetwork.SetDefaultPrinter strPrinterPath
	If Err.Number <> 0 Then
		WScript.Echo "Error adding printer " & strPrinterPath & vbCrLf & "Error " & Err.Number & ": " & Err.Description
		Err.Clear
	End If
	On Error Goto 0
End Sub

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question