Solved

VBS login sample

Posted on 2007-11-29
15
1,139 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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