Solved

VB Script to get user group information

Posted on 2006-11-07
14
1,428 Views
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

ON ERROR RESUME NEXT

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
wscript.quit
0
Comment
Question by:ItsChad
  • 9
  • 5
14 Comments
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 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.

HTH

Chris
0
 

Author Comment

by:ItsChad
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.
0
 
LVL 70

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?

Chris
0
 

Author Comment

by:ItsChad
ID: 17890376
Chris,

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.
0
 
LVL 70

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 192.168.1.1

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

Chris
0
 

Author Comment

by:ItsChad
ID: 17891535
Well lets give it a try!

Thanks,
Chad
0
 

Author Comment

by:ItsChad
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!
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 70

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.

Chris
0
 

Author Comment

by:ItsChad
ID: 17892605
Chris,

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.


Chad
0
 

Author Comment

by:ItsChad
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.

Thanks,
Chad
0
 

Author Comment

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

strGroup = LCase(Join(currentUser.MemberOf))
0
 

Author Comment

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

Author Comment

by:ItsChad
ID: 17893292
Ok solved that problem myself. Found some info here that helped me:
http://www.experts-exchange.com/Operating_Systems/WinXP/Q_21569341.html

Thanks for all the help today A+ worthy.
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 17893918

Glad I could help out a little :)

Chris
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

747 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

13 Experts available now in Live!

Get 1:1 Help Now