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
Solved

VBS login sample

Posted on 2007-11-29
15
1,142 Views
Last Modified: 2010-04-21
I was recently given the login script sample below for a previous question and it works great.  I am now looking to add to the login script and was looking for help on the syntax.  I'm looking to map a drive based on a user being in a specific group in AD.  I'm sure it's possible.  Something like if you're part of the general ledger group then map a drive to the general ledger share.  Any help would be greatly appreciated.

Thanks...

With a VBS file, you can use the WSH Network object to return the username, so you can use the same approach as a Batch file's %username% value.

Dim wshNetwork, objFSO
Set wshNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.DriveExists("h:") = True Then wshNetwork.RemoveNetworkDrive "h:"
If objFSO.DriveExists("m:") = True Then wshNetwork.RemoveNetworkDrive "m:"
If objFSO.DriveExists("o:") = True Then wshNetwork.RemoveNetworkDrive "o:"
If objFSO.DriveExists("s:") = True Then wshNetwork.RemoveNetworkDrive "s:"
wshNetwork.MapNetworkDrive "h:", "\\filesrv\users\" & wshNetwork.UserName
wshNetwork.MapNetworkDrive "m:", "\\filesrv\data"
wshNetwork.MapNetworkDrive "o:", "\\filesrv\masters"
wshNetwork.MapNetworkDrive "s:", "\\filesrv\shared"
WSCript.Quit
0
Comment
Question by:nyceuser
  • 9
  • 6
15 Comments
 
LVL 5

Expert Comment

by:usarian
ID: 20375533




Const PRODUCTION     	= "cn=plant floor users"
Const ADMINISTRATORS	= "cn=domain admins"
Const MANAGERS 		= "cn=production managers"
 
Set wshNetwork = CreateObject("WScript.Network")
 
Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)
 
strGroups = LCase(Join(CurrentUser.MemberOf,""))
 
wshNetwork.RemoveNetworkDrive "F:", True, True
 
IF InStr(strGroups, PRODUCTION) Then wshNetwork.MapNetworkDrive "F:", "\\{server}\{folder}"

Open in new window

0
 
LVL 5

Expert Comment

by:usarian
ID: 20375559
I found that I had to remove the network drive before adding it or I would get an error, and I also had to put strGroup =  "cn=default" in case the join returned no results, but that's all just error catching stuff.

Usarian
0
 

Author Comment

by:nyceuser
ID: 20375982
Would you be able to explain what each line does in the script you provided (since I'm not a VB guy)?  Also, does it matter where in the script I put these additional lines?

Thanks...
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:usarian
ID: 20376703
Try using this for your logon script:
Dim wshNetwork, objFSO
Set wshNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName) 
If objFSO.DriveExists("h:") = True Then wshNetwork.RemoveNetworkDrive "h:"
If objFSO.DriveExists("m:") = True Then wshNetwork.RemoveNetworkDrive "m:"
If objFSO.DriveExists("o:") = True Then wshNetwork.RemoveNetworkDrive "o:"
If objFSO.DriveExists("s:") = True Then wshNetwork.RemoveNetworkDrive "s:"
If objFSO.DriveExists("f:") = True Then wshNetwork.RemoveNetworkDrive "f:"
wshNetwork.MapNetworkDrive "h:", "\\filesrv\users\" & wshNetwork.UserName
wshNetwork.MapNetworkDrive "m:", "\\filesrv\data"
wshNetwork.MapNetworkDrive "o:", "\\filesrv\masters"
wshNetwork.MapNetworkDrive "s:", "\\filesrv\shared"
strGroups = LCase(Join(CurrentUser.MemberOf,""))
IF InStr(strGroups, "cn=general ledger") Then wshNetwork.MapNetworkDrive "f:", "\\filesrv\general ledger"
WSCript.Quit

Open in new window

0
 

Author Comment

by:nyceuser
ID: 20377201
Ok... I made the appropriate changes and I think I'm almost there.  I'm getting the following error from line 15:

Line: 15
Char: 1
Error: Type mismatch : 'Join'
Code: 800A000D
Source: Microsoft VBScript runtime error

Any ideas?

Thanks for you help.
0
 

Author Comment

by:nyceuser
ID: 20377352
I figured out that if the user is part of more than one group the script processes but doesn't map the drive relating to the group membership.  It maps all other drives.  I'm sure I have something mixed up here.

Any ideas?
0
 

Author Comment

by:nyceuser
ID: 20377468
Ok... I got it working so it maps the drive for the membership but it only works if the user is part of more than 1 group.  I'm sure there's different syntax that I can use that won't matter how many groups the user is part of.
0
 
LVL 5

Expert Comment

by:usarian
ID: 20377556
Quick FYI, the "cn=blablabla" has to be lowercase.

If you haven't figure out the 1 group thing yet, post your code and I will take a look.
0
 

Author Comment

by:nyceuser
ID: 20377671
I haven't figured out the 1 group thing yet.  Below is the current script.

Dim wshNetwork, objFSO
Set wshNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)
If objFSO.DriveExists("f:") = True Then wshNetwork.RemoveNetworkDrive "f:"
If objFSO.DriveExists("h:") = True Then wshNetwork.RemoveNetworkDrive "h:"
If objFSO.DriveExists("i:") = True Then wshNetwork.RemoveNetworkDrive "i:"
If objFSO.DriveExists("s:") = True Then wshNetwork.RemoveNetworkDrive "s:"
wshNetwork.MapNetworkDrive "f:", "\\filesrv\data"
wshNetwork.MapNetworkDrive "h:", "\\filesrv\users\" & wshNetwork.UserName
wshNetwork.MapNetworkDrive "i:", "\\filesrv\groupshares\installs"
wshNetwork.MapNetworkDrive "s:", "\\filesrv\groupshares"
strGroups = LCase(Join(CurrentUser.MemberOf,""))
IF InStr(strGroups, "cn=business systems2") Then wshNetwork.MapNetworkDrive "g:", "\\filesrv\data\groupshares\genledger"
end if
WSCript.Quit
0
 
LVL 5

Expert Comment

by:usarian
ID: 20378788
Well, what I would do atthis point is check the contents of the string variable.
To do that, put msgbox(strGroups) between the "strGroups =" line and the "IF InStr(strGroups"  line.
Make sure the text "cn=business systems2" shows up and is identical (spaces and all).

Also, get rid of the END IF right above WScript.Quit
0
 

Author Comment

by:nyceuser
ID: 20382864
Ok...  added the additional line in and everything looks OK.  Got rid of the END IF.  Still same result.  As soon as I take the user out of one of the groups so he only belongs to one group the script fails with the type mismatch: 'Join' error.
0
 
LVL 5

Accepted Solution

by:
usarian earned 250 total points
ID: 20383289
Try replacing:
strGroups = LCase(Join(CurrentUser.MemberOf))
    with
For each oGroup in CurrentUser.Groups
  strGroups = strGroups & Lcase(oGroup.name)
next


0
 

Author Comment

by:nyceuser
ID: 20383474
This is the script now.  Not sure if I have it right.  It processes through without any errors but does not map the g: drive based on group membership

Dim wshNetwork, objFSO
Set wshNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set Currentuser = GetObject("LDAP://" & ADSysInfo.UserName)
If objFSO.DriveExists("f:") = True Then wshNetwork.RemoveNetworkDrive "f:"
If objFSO.DriveExists("h:") = True Then wshNetwork.RemoveNetworkDrive "h:"
If objFSO.DriveExists("i:") = True Then wshNetwork.RemoveNetworkDrive "i:"
If objFSO.DriveExists("s:") = True Then wshNetwork.RemoveNetworkDrive "s:"
wshNetwork.MapNetworkDrive "f:", "\\filesvr\data"
wshNetwork.MapNetworkDrive "h:", "\\filesvr\users\" & wshNetwork.UserName
wshNetwork.MapNetworkDrive "i:", "\\filesvr\groupshares\installs"
wshNetwork.MapNetworkDrive "s:", "\\filesvr\groupshares"
For each oGroup in CurrentUser.Groups
      strGroups = strGroups & Lcase(oGroup.name)
next
IF InStr(strGroups, "cn=bsg") Then wshNetwork.MapNetworkDrive "g:", "\\filesvr\data\groupshares\genledger"
WSCript.Quit
0
 

Author Comment

by:nyceuser
ID: 20383576
Looks like it's working now.  I think I had to wait until the changes took affect.  Thanks for your help... It now seems to work great.

Thanks again.  You're the best!
0
 

Author Closing Comment

by:nyceuser
ID: 31411723
Great job... saved me a lot of work and research.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

839 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