Solved

VBS login sample

Posted on 2007-11-29
15
1,136 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 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

23 Experts available now in Live!

Get 1:1 Help Now