Map/UnMap Network Drive Based on Group Membership

By | January 17, 2007

We are migrating a file server over the weekend and need to remove all mapped drives from users computers.
Then we need to map the correct drives. You're thinking that this isn't a problem, just change the login script.
Wrong! What if some of the drives had been mapped manually?!?
So, I created this script to removed all mapped drives if they were not in a grouplist.csv file.
This script will retreive group membership from Active Directory and map/remove your drives and printers accordingly based on the information in the grouplist.csv file.
Note: The files should be located in the same folder. (e.g., \\fileserver\netlogon)
Also, This script incorperates the "LazyNetworkAdmin's" Map Network Drive and Printers Based on Group Membership script!

 

Note: The base of this script was created by James Currie. This was unbeknownst to me until today.

———-COPY EVERYTHING BELOW THIS LINE for the Script———-
'This script removes mapped drives that are not in the grouplist.csv
'Created By Chey Harden 1.17.07

On Error Resume Next
Const ForReading = 1

Dim objFSO, objDrive, objTextFile, intDrive, intNetLetter, strSearch
Dim objFile, strContents, WSHNetwork, strDrive, objNetwork

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")
Set objDrive = objNetwork.EnumNetworkDrives
Set objTextFile = objFSO.OpenTextFile("\\fileserver\netlogon\grouplist.csv", ForReading)
Set WSHNetwork = WScript.CreateObject("WScript.Network")

' Enumerated Mapped Drives
For intDrive = 0 to objDrive.Count -1 Step 2
intNetLetter = IntNetLetter +1
'WScript.Echo  objDrive.Item(intDrive) & "," & objDrive.Item(intDrive +1)
strSearch = objDrive.Item(intDrive) & "," & objDrive.Item(intDrive +1)
'Wscript.Echo strSearch

 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("\\fileserver\netlogon\grouplist.csv", ForReading)
strContents = objFile.ReadAll
objFile.Close

If InStr(strContents, strSearch) Then
 'Wscript.Echo "Found"
 
 Else
    'Wscript.Echo strSearch
 strDrive = Left(strSearch,2)
 'Wscript.Echo strDrive
 strDrive = "" & strDrive & ""
 'Wscript.Echo strDrive
 WSHNetwork.RemoveNetworkDrive strDrive
End If

'************************
Next

'The next part of the script maps the correct drives 

'On Error Resume Next

Dim GroupList
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")

GetGroupInfo()

LogonPath = fso.GetParentFolderName(WScript.ScriptFullName)
'**************************************Group Mappings Based on Grouplist.csv*********************************
If fso.FileExists(logonpath&"\Grouplist.csv") Then
   Set grplist = Fso.OpenTextFile(logonpath&"\Grouplist.csv")
   ' Make File into an Array
   aGroup = Split(grplist.Readall,vbcrlf)
   For I = 0 to UBound(GroupList) ' Check Every Group Membership the user is in (populated into Grouplist)
      grpname = Grouplist(i)
      For x = 0 to UBound(aGroup) ' Read the entire CSV to make sure all drives are mapped for each Group
         mapline = agroup(x)
         If InStr(LCase(mapline),LCase(grpname)) Then ' If you're in the group
            mapline = Mid(mapline,InStr(mapline,",")+1) ' Remove the GroupName from the line
            Drive = Left(mapline,InStr(mapline,",")-1) ' Extract Drive Letter
            Path = Mid(mapline,InStr(mapline,",")+1) ' Extract the path

            If (fso.DriveExists(drive) <> True) and (Drive<>"!!") Then ' If The Drive is not already mapped
               WshNetwork.MapNetworkDrive drive,path ' Map The Drive
               wscript.sleep 1000
            End If

        If Drive = "!!" then
               WSHNetwork.AddWindowsPrinterConnection Path
               wscript.sleep 1000
            end if

         End If
      Next
   Next
End If

Sub GetGroupInfo
Set UserObj = GetObject("WinNT://" & wshNetwork.UserDomain & "/" & WshNetwork.UserName)
Set Groups = UserObj.groups

For Each Group In Groups
GroupCount = GroupCount + 1
Next

ReDim GroupList(GroupCount -1)
i = 0
For Each Group In Groups
GroupList(i) = Group.Name
i = i + 1
Next
End Sub
'msgbox "The Logon script has been run"

 

———-COPY EVERYTHING ABOVE THIS LINE for the Script———-

 

———-COPY EVERYTHING BELOW THIS LINE FOR THE GROUPLIST.CSV FILE———-
Clock,T:,\\TimeClock\Time$
HR,G:,\\FS1\Hr
Users,I:,\\FS2\Files
Main-HR-Printer1,!!,\\FS1\Main-HR-Printer1

———-COPY EVERYTHING ABOVE THIS LINE FOR THE GROUPLIST.CSV FILE———-

PLEASE MAKE SURE NO WORD WRAPPING IS HAPPENING IN YOUR SCRIPT!!!

To make this script work you will need the items below!

1. Run this script as a logon script.
2. Make sure both files are located in the same folder.
3. Make sure users have the ability to remove mapped drives.
4. The GROUPLIST.CSV is just an example! See instructions below!

Grouplist.csv
In the first item "Clock,T:,\\TimeClock\Time$"
1. Clock is the name of an AD Group.
2. T: is the Drive Letter.
3. \\TimeClock\Time$ is the path for the mapped drive.
Note: You need commas between each of these items.

This information is provided "AS IS" with no warranties expressed or implied.

Leave a Reply

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