Solved

VBS login sample

Posted on 2007-11-29
15
1,131 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

19 Experts available now in Live!

Get 1:1 Help Now