Print Friendly

In order to provide my users with drive letters, and manage this process using Active Directory group membership, I decided to design a number of scripts and batch files to do this. Select the code below and save this to a file for example:    map-drives.vbs

‘ Nick Furnell June 2011
‘ Extracts from  http://cwashington.netreach.net/depo/default.asp?topic=repository&ScriptType=vbscript

‘Also http://www.microsoft.com/technet/scriptcenter/guide/sas_ads_xoth.mspx?mfr=true
Dim Adinfo, userOU, GroupName, answer
set adinfo = CreateObject(“ADSystemInfo”)
‘Finds the user Distinguished Name
usernameDN = adinfo.UserName
On Error Resume Next
Const E_ADS_PROPERTY_NOT_FOUND  = &h8000500D
Set objUser = GetObject(“LDAP://”&usernameDN)
intPrimaryGroupID = objUser.Get(“primaryGroupID”)
areMemberOf = objUser.GetEx(“memberOf”)
If Err.Number = E_ADS_PROPERTY_NOT_FOUND Then
‘ No MemberOf  attribute set
Else
For Each Group in  areMemberOf
GroupName =  FindGroup(Group)
‘  msgbox(“Full group:” & Group & ” / Groupname:”  & GroupName)
Compare  Groupname
Next
End If


********************************************************************************

‘ Put drive mappings for ALL users regardless of group membership on next line

‘ MapDrive “U:”, “\\YOURDOMAIN.local\Update”

‘ ********************************************************************************
‘msgbox(“DONE!”)

wscript.quit
‘————————————————————————————–

Sub Compare (TestGroup)

TestGroup = UCASE(TestGroup)
If TestGroup = UCASE(“AD-Group1”)             then MapDrive  “F:”, “\\SERVERNAME-D1\Share1
If TestGroup = UCASE(“AD-Group2”)             then MapDrive  “F:”, “\\SERVERNAME-D1\Share2
If TestGroup = UCASE(“AD-Group3”)             then MapDrive  “F:”, “\\SERVERNAME-D1\Share3
If TestGroup = UCASE(“AD-Group4”)             then MapDrive  “F:”, “\\SERVERNAME-D1\Share4
End Sub


——————————————————————————–

‘Checking users Group name and setting destination variable  accordingly

Function FindGroup(strDN)
FindGroup = Mid(strDN, InStr(strDN, “=”) + 1)
FindGroup = Left(FindGroup, InStr(FindGroup, “=”)  -4)
End Function

‘ ——————————————————————————–

sub CheckGroup (GroupName, username, Drive, unc)
‘the line below was here for testing purposes and debugging
‘ MsgBox(groupname & ” – ” & username)
‘ replace //YOURDOMAIN/ with your domain name.
Set AdsGroup =  GetObject(“WinNT://YOURDOMAIN/” & GroupName)
Set  ADSGROUPMEMBERSHIP = AdsGroup.Members()
for each adsUser in  adsgroupmembership
If  UCase(adsUser.name) = username Then MapDrive Drive, UNC
next

End Sub

 

‘ ——————————————————————————–
Sub mapdrive (driveletter, UNCString)
‘the line below was here for testing purposes and debugging
‘ MsgBox(“Mapping Drive!”)
Set WSHNetwork = WScript.CreateObject(“WScript.Network”)
On Error Resume Next
WSHNetwork.MapNetworkDrive driveletter, Uncstring
End sub

 

2 thoughts on “VBS Logon Script to Map Drive Letters to UNC Paths

  1. naturally like your web site but you need to take a look at the spelling on several of your posts. Several of them are rife with spelling issues and I to find it very troublesome to tell the reality however I¡¦ll definitely come again again.

Leave a Reply

Your email address will not be published. Required fields are marked *

WordPress spam blocked by CleanTalk.