We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


VB Script to get user group information

ItsChad asked
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
Watch Question

PowerShell Developer
Top Expert 2010

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.



Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


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.
Chris DentPowerShell Developer
Top Expert 2010


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?




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.
Chris DentPowerShell Developer
Top Expert 2010


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).



Well lets give it a try!



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!
Chris DentPowerShell Developer
Top Expert 2010


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.




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.



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.



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

strGroup = LCase(Join(currentUser.MemberOf))


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


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

Thanks for all the help today A+ worthy.
Chris DentPowerShell Developer
Top Expert 2010


Glad I could help out a little :)

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.