Vbscript - adding printers to logon script

We have an SBS 2003 server and I am running logon scripts which map drives, add information into the description field in AD and add printers.  The mapped drive and the printers are based on a group.

For some strange reason I cannot get the script to recognize the matching group.

I know the syntax is correct because one of the groups works -- in fact, if I add all the users to the "sii" group and use it to map the all the printers, all printers get installed with no issue.  Very strange.  I'm thinking that there is a problem with the group names.

I'm not much of a vbsscript guy.  I just copied and tried to get it to work.

Does the group have to be all caps, or all small letters or any specific syntax?  I've tried it every way I can think of.

Thanks for the help!
logonscript.vbs
svillardiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Spike99On-Site IT TechnicianCommented:
I'm not a VBS scripting guru by any stretch, but this answer uses the same method I used before to map printers by group names:
http://www.experts-exchange.com/Software/Server_Software/Active_Directory/Q_23418775.html

Just like the accepted answer for that EE question, we had a "case" line at the top of each printer section that named the group & then one line for each printer we wanted to add. Then, we added a line to make one of those the default.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobSampsonCommented:
Hi, try changing your InStr calls to this format:
If InStr(1, strGroups, "mpc5503", vbTextCompare) > 0 Then

so that they are not case sensitive.  Note the > 0 is important, because InStr returns the character position of the string to search for, so a return code of zero means it doesn't exist.

You can also use
MsgBox strGroups

before those If statement to manually see whether it should match or not.

Rob.
it_saigeDeveloperCommented:
Based upon your example here, may I suggest an alternative:
Dim objGroupList ' Group List Object
Dim objUser ' User Object
Dim objSysInfo ' System Information Object
Dim strUserDN ' User String

Set objSysInfo = CreateObject("ADSystemInfo")
strUserDN = objSysInfo.userName

Set objUser = GetObject("LDAP://" & strUserDN)

If IsMember(objUser, "mpc5503") Then
	WSHNetwork.AddWindowsPrinterConnection "\\server.domain.local\ricoh-mpc5503"
End If

If IsMember(objUser, "mp4054-1") Then
	WSHNetwork.AddWindowsPrinterConnection "\\server.domain.local\ricoh-mp4054-1"
End If

If IsMember(objUser, "mpc4054-2") Then
	WSHNetwork.AddWindowsPrinterConnection "\\server.domain.local\ricoh-MP4054-2"
End If

If IsMember(objUser, "sii-lp-1030") Then
	WSHNetwork.AddWindowsPrinterConnection "\\server.domain.local\sii-lp-1030"
End If

Set objGroupList = Nothing
Set objUser = Nothing
Set objSysInfo = Nothing
Set strUserDN = Nothing

Function IsMember(objADObject, strGroup)
	' Function to test for group membership.
	' objGroupList is a dictionary object with global scope.

	' Set IsMember to False
	IsMember = False

	' Declare variables
	Dim Group

	' If objGroupList is Empty
	If (IsEmpty(objGroupList) = True) Then
		' Create a dictionary object and set it to objGroupList
		Set objGroupList = CreateObject("Scripting.Dictionary")
	End If

	' If objGroupList does not have an object called SAMAccountName\
	If (objGroupList.Exists(objADObject.sAMAccountName & "\") = False) Then
		' Call LoadGroups function with objADObject as both variables
		Call LoadGroups(objADObject, objADObject)

		' Add SAMAccountName\ to the objGroupList dictionary
		objGroupList.Add objADObject.sAMAccountName & "\", True
	End If

	' Set IsMember to True or False depending upon if the intended group
	' exists in the dictionary
	IsMember = objGroupList.Exists(objADObject.sAMAccountName & "\" & strGroup) OrElse HasGroup(strGroup)
End Function

Function HasGroup(strGroup)
	Dim result = False
	For Each item in objGroupList
		If InStr(1, item, strGroup, vbTextCompare) > 0 Then
			result = True
			Exit For
		End If
	Next
	HasGroup = result
End Function

Sub LoadGroups(objPriObject, objADSubObject)
	' Recursive subroutine to populate dictionary object objGroupList.
	Dim colstrGroups, objGroup, j
	objGroupList.CompareMode = vbTextCompare
	colstrGroups = objADSubObject.memberOf

	If (IsEmpty(colstrGroups) = True) Then
		Exit Sub
	End If

	If (TypeName(colstrGroups) = "String") Then
		Set objGroup = GetObject("LDAP://" & colstrGroups)
		If Not objGroupList.Exists(objPriObject.sAMAccountName & "\" & objGroup.sAMAccountName) Then
			objGroupList.Add objPriObject.sAMAccountName & "\" & objGroup.sAMAccountName, True
			Call LoadGroups(objPriObject, objGroup)
		End If

		Set objGroup = Nothing
		Exit Sub
	End If

	For j = 0 To UBound(colstrGroups)
		Set objGroup = GetObject("LDAP://" & colstrGroups(j))
		If Not objGroupList.Exists(objPriObject.sAMAccountName & "\" & objGroup.sAMAccountName) Then
			objGroupList.Add objPriObject.sAMAccountName & "\" & objGroup.sAMAccountName, True
			Call LoadGroups(objPriObject, objGroup)
		End If
	Next

	Set objGroup = Nothing
End Sub

Open in new window

This not only provides a case insensitive comparison, but also enumerates the groups, taking care to enumerate sub groups as well.

-saige-
svillardiAuthor Commented:
I will need to get back to this.
Spike99On-Site IT TechnicianCommented:
The script posted in Oct 2012 by Vortex is nearly identical to the ones we used to use in my old job:
http://www.edugeek.net/forums/coding/122544-vbs-printer-script-checking-pc-group-membership.html

So I modified his example by adding the groups & printers from your original script along with a default printer for each group.

This script is a little more straightforward & easier to edit going forward: just add a new "case" section  for each group along with any printers that group will need.

'Printer vbs for logon script

'determines the user who just logged on
Set objSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("Wscript.Network")
'As soon as we tack on LDAP:// and construct an ADsPath we then bind to the user account in
'Active Directory and report back the groups the user belongs to; this can be done simply 
'by enumerating the values in the MemberOf attribute.


strUserPath = "LDAP://" & objSysInfo.UserName
Set objUser = GetObject(strUserPath)


For Each objGroup in objUser.Groups
  strGroupName = objGroup.CN

'Mapping of printers by group and set default printer for that group
  Select Case strGroupName
 
    Case "mpc5503"
	objNetwork.AddWindowsPrinterConnection "\\server.domain.local\ricoh-mpc5503"
        objNetwork.SetDefaultPrinter  "\\server.domain.local\ricoh-mpc5503"

'Map Ricoh Printers
     Case "mp4054-1"
	objNetwork.AddWindowsPrinterConnection "\\server.domain.local\ricoh-mp4054-1"
        objNetwork.SetDefaultPrinter "\\server.domain.local\ricoh-mp4054-1"
     
     Case "mp4054-2"
	objNetwork.AddWindowsPrinterConnection "\\server.domain.local\ricoh-MP4054-2"
        objNetwork.SetDefaultPrinter "\\server.domain.local\ricoh-MP4054-2"

'Map SII Plotter Printer
      Case "sii-lp-1030"
	objNetwork.AddWindowsPrinterConnection "\\server.domain.local\sii-lp-1030"
        objNetwork.SetDefaultPrinter "\\server.domain.local\ricoh-MP4054-2"

'==========Add addtional case sections for each group above================
    
  End Select
next

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.