Solved

Adding network printers

Posted on 2011-09-21
5
239 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
  • 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now