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!
----------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")
'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.
I am using this script to map network drives, when i want to map the drive to the users name with "%UsersName%" it does not map the drive.
Am i doing anything wrong?
Using this script is great... minor problem is that its flaky on some of my users... I have had to map the script directly and have them click it in order to get the drives mapped... I'm not sure why this is... any ideas?
I'm having problems with old mapped drives(maped on command: "net use u: \\182.174.192.182", the script can't remove then, just can remove the drives mapped by script. Somebody have some tip?
I found the solution, when add map drives by "net use" command or in explorer->mapped drives. This procedure adds a key in registry HKEY_CURRENT_USER\NETWORK\X where x is the drive letter, then i add in this script code(i foun it in microsoft script center) to delete this keys!