Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


VB Script to get user group information

Posted on 2006-11-07
Medium Priority
Last Modified: 2012-05-05
I have the following code from a script I am writing to map printer by zones as well as to get rid of multiple batch logon scripts. The script works perfectly on the server, however when it is on a workstation it is real slow to run and I do not get the user group. I put in a check to see if the strGroup was getting set properly. On the server it gives me all the "cn=" information as I expected. On the workstation it takes about 3 minutes to complete but the message box that comes up is blank which is telling me for some reason the strGroup isnt getting set.

Here is the code I have to get user group information, I am pretty sure that this is where the problems are:
Set objUser =CreateObject("ADSystemInfo")
Set currentUser =GetObject("LDAP://"&objUser.UserName)
strGroup = LCase(Join(currentUser.MemberOf))

MsgBox strGroup

Here is the complete code:
'Login Script


Dim WSHShell, WSHNetwork, objDomain, DomainString,UserString, UserObj, Path, objNetwork, objNetwork1

'Declare Printer Variables
Dim objNet
Dim strZone
Dim netPrinter

'Declare Printer Zones
Dim ZoneA, ZoneB, ZoneC, ZoneD, ZoneE, ZoneF, ZoneG, ZoneH

'Declare User Groups as const so they can not be changed
Const Administrators ="cn=administrators"
Const Users = "cn=users"
'Const Aries = "cn=aries"

Dim strGroup, objUser, CurrentUser

'Declare Share Folders
Dim ShareH, ShareI, ShareJ, ShareK, ShareL, ShareM, ShareN, ShareO, ShareP, ShareQ, ShareR, ShareS, ShareT, ShareU,

ShareV, ShareW, ShareX, ShareY, ShareZ

'Set Variables
Set objNet = CreateObject("WScript.NetWork")
Set netPrinter = CreateObject("WScript.Network")
Set WSHShell = CreateObject("WScript.Shell")

'---------------------------------- Printer Maps ----------------------------------------

'***** ZoneA Printers *****
ZoneA = "\\ServerName\Printer"

'***** ZoneB Printers *****
ZoneB = "\\ServerName\Printer"

'***** ZoneC Printers *****
ZoneC = "\\ServerName\Printer"

'***** ZoneD Printers *****
ZoneD = "\\ServerName\Printer"

'***** ZoneE Printers *****
ZoneE = "\\ServerName\Printer"

'***** ZoneF Printers *****
ZoneF = "\\ServerName\Printer"

'------------------------ Install Printer by Zone ------------------------------------

'Get First Letter of Computer name
strZone = (Mid(objNet.ComputerName,1,1))

'Check Printer and Connect

Select Case LCase(strZone)
      case "a"            
            MsgBox "Printer A Zone"
            'netPrinter.AddWindowsPrinterConnection ZoneA
            'netPrinter.SetDefaultPrinter ZoneA
       case "b"            
            MsgBox "Printer B Zone"
            'netPrinter.AddWindowsPrinterConnection ZoneB
            'netPrinter.SetDefaultPrinter ZoneB
      case "c"            
            MsgBox "Printer C Zone"
            'netPrinter.AddWindowsPrinterConnection ZoneC
            'netPrinter.SetDefaultPrinter ZoneC
      case "d"            
            MsgBox "Printer D Zone"
            'netPrinter.AddWindowsPrinterConnection ZoneD
            'netPrinter.SetDefaultPrinter ZoneD
        case "e"            
            MsgBox "Printer E Zone"
            'netPrinter.AddWindowsPrinterConnection ZoneE
            'netPrinter.SetDefaultPrinter ZoneE
       case "f"            
            MsgBox "Printer F Zone"
            'netPrinter.AddWindowsPrinterConnection ZoneF
            'netPrinter.SetDefaultPrinter ZoneF
 End Select

'--------------------- Drive Maps ---------------------------

ShareH = "\\raebackroom\f"
ShareI = "\\servername\sharefolder"
ShareJ = "\\servername\sharefolder"
ShareK = "\\Servername\sharefolder"

'--------------------- Apply Drive Mappings ---------------------------
Set objUser =CreateObject("ADSystemInfo")
Set currentUser =GetObject("LDAP://"&objUser.UserName)
strGroup = LCase(Join(currentUser.MemberOf))

MsgBox strGroup
If InStr(strGroup, Administrators) Then
            MsgBox "Admin User"      
            objNetwork.MapNetworkDrive "h:", ShareH
End If

'--------------------- Clean Up ---------------------------
'Clean Up Memory We Used
Set UserObj = Nothing
Set GroupObj = Nothing
Set WSHNetwork = Nothing
Set DomainString = Nothing
Set WSHSHell = Nothing
Set WSHPrinters = Nothing
Set objNetwork = Nothing
Set objNetwork1 = Nothing
Set objNet = Nothing                    

'Quit the Script
Question by:ItsChad
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
  • 9
  • 5
LVL 71

Accepted Solution

Chris Dent earned 2000 total points
ID: 17889918

It may fail if the user is only a member of a single group, .MemberOf doesn't always return an array (it's a string when the user is only a member of a single group) and in that case "Join" fails and would normally produce a terminal error.

So, two things to do.

First, when trying to troubleshoot get rid of lines that suppress error messages, so either comment out or remove "ON ERROR RESUME NEXT".

Secondly, we can deal with the problem of retrieving that information as follows:

Set objUser = CreateObject("ADSystemInfo")
Set currentUser = GetObject("LDAP://" & objUser.UserName)
strGroup = LCase(Join(currentUser.GetEx("memberOf")))

In the lines above we've swapped from currentUser.MemberOf to currentUser.GetEx("memberOf") as using GetEx will mean we always return an Array (even if it's only an array of one element).

It's possible that the group method isn't the problem at all, this is why "On Error Resume Next" should be removed.



Author Comment

ID: 17890042
I am getting the following error:

"The specified domain either does not exist or could not be contacted"

It is on:
Set currentUser = GetObject("LDAP://" & objUser.UserName)

hmm, I am confused now. Hah, if you could help me I would really appreciate it. It may have something to do with my network speed today possibly.
LVL 71

Expert Comment

by:Chris Dent
ID: 17890123

Okay, lets have a look at what's held in objUser.UserName by adding:

WScript.Echo objUser.UserName

Above the "Set currentUser =" line.

I take it the machine you're running this on is a domain member? Could you let me know how you've configured the DNS Servers in TCP/IP configuration?

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Comment

ID: 17890376

I have this setup on a test room that I just configured and um the DNS server on the workstation is set to automatic.

The server was left blank so I believe it is using the static ip address as the primary dns.

Be honest this isnt my strong point, networking that is. If you see a problem please let me know.
LVL 71

Expert Comment

by:Chris Dent
ID: 17890499

Well the most likely cause of cannot find domain is that the DNS Server information is set incorrectly.

The AD System Information command you're running returns the ADSPath of the current user (so CN=User Name,OU=Some OU,DC=YourDomain,DC=local) then tries to bind to that with the "Set currentUser = ..." command.

If ADSPath is returning correctly but it's failing on the Bind to the account, that would imply there's a problem with the network configuration as it will need to be able to find a Domain Controller for that operation. For Active Directory Domains the DNS Server should be the Domain Controller (or a Domain Controller) as DNS is very important to Active Directory.

If we take an example:

Test Domain Server - IP Address

Then any workstation expected to work on that domain would need to have listed as the Preferred DNS (with Alternate left blank for such a simple configuration).


Author Comment

ID: 17891535
Well lets give it a try!


Author Comment

ID: 17891727
Thanks! That worked for my administrator account, if I logon as an administrator everything works properly. However if I logon using as a regular user that is only a member who is just a member of the domain/users the MsgBox strGroup will pop up blank and it will not enter the following check:

If InStr(strGroup, Users) Then
          MsgBox "User"    
          objNetwork.MapNetworkDrive "h:", ShareH
End If

Thanks for the help!
LVL 71

Expert Comment

by:Chris Dent
ID: 17892042

That one has a bit of an odd explaination.

The memberOf attribute lists all groups the user is a member of with the exception of the Primary Group. This defaults to Domain Users when you create a new account and is rarely changed, unfortunately that makes it pretty tricky (or at least a bit of extra messing around) to see if the user is a member of that particular group (making it slightly less than useful for scripting like this).

To pick that one up using LDAP you would have to read the PrimaryGroupToken attribute from the user account (currentUser.Get("primaryGroupToken")) then compare that with the same attribute on each group within AD. There are a few common ones so comparison is not always necessary, Domain Users is always 513 if I remember correctly.

Of course there are other ways to enumerate membership of that group, for instance, we can use the WinNT interface instead of LDAP:

Set objUser = GetObject("WinNT://" & objNetwork.UserDomain & "/" & currentUser.Get("sAMAccountName") & ", user")

Then you can do:

strGroups = LCase(Join(objUser.MemberOf))

As the WinNT interface presents the information to us slightly differently it will not have the problem LDAP has with the Join command.

In the end though I would stay away from using that group in scripting, it just makes life difficult, it would be better to create a brand new group for your domain users.


Author Comment

ID: 17892605

Ok I made a group named Teacher, and made my test user a member of this group. So now he is a user of "users" and of "teacher". Yet it still is failing to work. If I log in as an admin it works. If I login as the test user it doesnt.

I appreciate all your help with this by the way.


Author Comment

ID: 17892628
By not workign I mean two things: One it is not setting the proper shares, and it is also not displaying anything on the msgbox I have that pops up showing the contents of strGroup. Is there any other thing that would prevent it from doing this for a normal user. LIke I say admin it works great.


Author Comment

ID: 17892694
Also this is where I get the error when I try to run it as a user:

strGroup = LCase(Join(currentUser.MemberOf))

Author Comment

ID: 17892729
Sorry last update:
No error message when running as Admin.

Author Comment

ID: 17893292
Ok solved that problem myself. Found some info here that helped me:

Thanks for all the help today A+ worthy.
LVL 71

Expert Comment

by:Chris Dent
ID: 17893918

Glad I could help out a little :)


Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

719 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