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("ADSystemInf o")
Set currentUser =GetObject("LDAP://"&objUs er.UserNam e)
strGroup = LCase(Join(currentUser.Mem berOf))
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.NetW ork")
Set netPrinter = CreateObject("WScript.Netw ork")
Set WSHShell = CreateObject("WScript.Shel l")
'------------------------- --------- 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.AddWindowsPrin terConnect ion ZoneA
'netPrinter.SetDefaultPrin ter ZoneA
case "b"
MsgBox "Printer B Zone"
'netPrinter.AddWindowsPrin terConnect ion ZoneB
'netPrinter.SetDefaultPrin ter ZoneB
case "c"
MsgBox "Printer C Zone"
'netPrinter.AddWindowsPrin terConnect ion ZoneC
'netPrinter.SetDefaultPrin ter ZoneC
case "d"
MsgBox "Printer D Zone"
'netPrinter.AddWindowsPrin terConnect ion ZoneD
'netPrinter.SetDefaultPrin ter ZoneD
case "e"
MsgBox "Printer E Zone"
'netPrinter.AddWindowsPrin terConnect ion ZoneE
'netPrinter.SetDefaultPrin ter ZoneE
case "f"
MsgBox "Printer F Zone"
'netPrinter.AddWindowsPrin terConnect ion ZoneF
'netPrinter.SetDefaultPrin ter ZoneF
End Select
'--------------------- Drive Maps -------------------------- -
'
ShareH = "\\raebackroom\f"
ShareI = "\\servername\sharefolder"
ShareJ = "\\servername\sharefolder"
ShareK = "\\Servername\sharefolder"
'--------------------- Apply Drive Mappings -------------------------- -
'
Set objUser =CreateObject("ADSystemInf o")
Set currentUser =GetObject("LDAP://"&objUs er.UserNam e)
strGroup = LCase(Join(currentUser.Mem berOf))
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
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("ADSystemInf
Set currentUser =GetObject("LDAP://"&objUs
strGroup = LCase(Join(currentUser.Mem
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.NetW
Set netPrinter = CreateObject("WScript.Netw
Set WSHShell = CreateObject("WScript.Shel
'-------------------------
'
'***** 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
'Check Printer and Connect
Select Case LCase(strZone)
case "a"
MsgBox "Printer A Zone"
'netPrinter.AddWindowsPrin
'netPrinter.SetDefaultPrin
case "b"
MsgBox "Printer B Zone"
'netPrinter.AddWindowsPrin
'netPrinter.SetDefaultPrin
case "c"
MsgBox "Printer C Zone"
'netPrinter.AddWindowsPrin
'netPrinter.SetDefaultPrin
case "d"
MsgBox "Printer D Zone"
'netPrinter.AddWindowsPrin
'netPrinter.SetDefaultPrin
case "e"
MsgBox "Printer E Zone"
'netPrinter.AddWindowsPrin
'netPrinter.SetDefaultPrin
case "f"
MsgBox "Printer F Zone"
'netPrinter.AddWindowsPrin
'netPrinter.SetDefaultPrin
End Select
'--------------------- Drive Maps --------------------------
'
ShareH = "\\raebackroom\f"
ShareI = "\\servername\sharefolder"
ShareJ = "\\servername\sharefolder"
ShareK = "\\Servername\sharefolder"
'--------------------- Apply Drive Mappings --------------------------
'
Set objUser =CreateObject("ADSystemInf
Set currentUser =GetObject("LDAP://"&objUs
strGroup = LCase(Join(currentUser.Mem
MsgBox strGroup
If InStr(strGroup, Administrators) Then
MsgBox "Admin User"
objNetwork.MapNetworkDrive
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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.
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)
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
ASKER
Well lets give it a try!
Thanks,
Chad
Thanks,
Chad
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!
If InStr(strGroup, Users) Then
MsgBox "User"
objNetwork.MapNetworkDrive
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("primaryG
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("sAMAccoun
Then you can do:
strGroups = LCase(Join(objUser.MemberO
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
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
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
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
Thanks,
Chad
ASKER
Also this is where I get the error when I try to run it as a user:
strGroup = LCase(Join(currentUser.Mem berOf))
strGroup = LCase(Join(currentUser.Mem
ASKER
Sorry last update:
No error message when running as Admin.
No error message when running as Admin.
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.
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
ASKER
"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.