Tuesday, March 10, 2009

VBScript to Map Drives Based on Group Membership

Often times, drives will need to mapped for users in a specific group. The script below uses a case statement to check for group membership and maps the drive accordingly. To modify the script simply change the group name in the quotes and put the correct drive letter and UNC path for the drive mapping.

'Group Drive Mappings Script

Const HKEY_CURRENT_USER = &H80000001

Dim WshNetwork, WshShell
Dim oDrives, fUser, fDomain, oGroupDict
Dim ScriptLogName

ScriptLogName = "_LoginScript.log"

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set oDrives = WshNetwork.EnumNetworkDrives
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set inputFile = fso.OpenTextFile(fTMP & "\" & ScriptLogName, 2, True)

fTMP = WshShell.ExpandEnvironmentStrings("%TEMP%")

WriteLog(Now() & ": ****** Start Login Script ******")

fUser = WshNetwork.UserName
fDomain = WshNetwork.UserDomain

'****** Group Drives ******

WriteLog("Starting Group Script Area")

if isMember("Accounting") then
MapDrive "S:", "\\server\shared\"
end if

if isMember("Human Resources") then
MapDrive "S:", "\\server\hr"
end if

'***** use elseif for mapping multiple groups to the same share*******
if isMember("IT Infrastructure") then
MapDrive "S:", "\\server\it"
elseif isMember("Helpdesk Staff") then
MapDrive "S:", "\\server\it"
elseif isMember("Data Services") then
MapDrive "S:", "\\server\it"
end if

WriteLog("Ending Group Script Area")

WriteLog(Now() & ": ****** Login Script Complete ******")

Function IsMember(sGroup)
Dim sAdsPath, oUser, oGroup

If IsEmpty(oGroupDict) Then
Set oGroupDict = CreateObject("Scripting.Dictionary")
oGroupDict.CompareMode = vbTextCompare

sAdsPath = WshNetwork.UserDomain & "/" & WshNetwork.UserName
Set oUser = GetObject("WinNT://" & sAdsPath & ",user")

For Each oGroup In oUser.Groups
oGroupDict.Add oGroup.Name, "-"
Set oUser = Nothing
End If
IsMember = CBool(oGroupDict.Exists(sGroup))
if (IsMember) then
WriteLog(Now() & " User is member of " & sGroup)
end if

End Function

Sub MapDrive(sLetter,sUNC)
Dim oNetwork
Set oNetwork = WScript.CreateObject("WScript.Network")
For i = 0 to oDrives.Count -1 Step 2
if LCase(sLetter) = LCase(oDrives.Item(i)) then
oNetwork.RemoveNetworkDrive sLetter, true, true
WriteLog(Now() & ": Removed drive """ & sLetter & """ from share """ & sUNC & " (already connected) " & "")
end if
oNetwork.MapNetworkDrive sLetter,sUNC
If Err.Number = 0 Then
WriteLog(Now() & ": Connect """ & sLetter & """ to share """ & sUNC & """")
WriteLog(Now() & ": Failed to Connect """ & sLetter & """ to share """ & sUNC & """")
end if
on error goto 0
End Sub

sub WriteLog(strEntry)
End Sub


Switch said...

This script is great when double-clicking on the local computer to run it, but when applied in Group Policy, it does not work

Switch said...

Did not work in Group Policy user login script

Binh said...

Thanks for this. I used something similar but ported the VB codes to Powershell instead. I've written about it here: http://thisishelpful.com/powershell-login-script-map-network-drives-printers-applicaton-settings.html.

Hope this helps too.