Link to home
Start Free TrialLog in
Avatar of ItsChad
ItsChad

asked on

VB Script to get user group information

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
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ItsChad
ItsChad

ASKER

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.

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
Avatar of ItsChad

ASKER

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.

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
Avatar of ItsChad

ASKER

Well lets give it a try!

Thanks,
Chad
Avatar of ItsChad

ASKER

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!

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
Avatar of ItsChad

ASKER

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
Avatar of ItsChad

ASKER

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
Avatar of ItsChad

ASKER

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

strGroup = LCase(Join(currentUser.MemberOf))
Avatar of ItsChad

ASKER

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

ASKER

Ok solved that problem myself. Found some info here that helped me:
https://www.experts-exchange.com/questions/21569341/Logon-Script-Error-Type-mismatch-Join.html

Thanks for all the help today A+ worthy.

Glad I could help out a little :)

Chris