Sponsored Links

Login Form






Lost Password?

Syndicate

Home arrow Knowledgebase arrow Windows arrow Map/UnMap Network Drive Based on Group Membership
Map/UnMap Network Drive Based on Group Membership PDF Print E-mail
User Rating: / 0
PoorBest 
Written by Cheyenne Harden   
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")


' 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.

Comments
Add NewSearch
Anthony Vignola - Can you use %UserName% with th   | 63.138.196.xxx | 29-01-2007 11:22:08
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?

AVignola@ccmd.org
LazyNetworkAdmin - Nothing wrong   | 67.109.213.xxx | 29-01-2007 12:09:55
You are not doing anything wrong. That is one of the faults of this script. Using %username% will not work with this script as is it.
LazyScriptCopier   | 24.214.202.xxx | 08-02-2008 16:24:25
Hello

I am having trouble getting any hidden shares to map "\\abc\abc$".

Is this is a limitation of the script?
Viper_iii - IT Dunce   | 74.87.0.xxx | 25-02-2008 18:10:49
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?
ceharden - Issues   | Publisher | 29-02-2008 13:51:17
Guys,

I don't believe it should be a problem. In my CSV file I have Human Resources,R:,\\Server01\Acctng$ and it works great.

I have had some mysterious issues also with it not running once in awhile. To fix this I edit a GPO that run the script synchronously.

Thanks,
Chey
Write comment
Name:
Email:
 
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
 
Security Image
Please input the anti-spam code that you can read in the image.

Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved.

 
Tag it:
Delicious
Furl it!
Spurl
digg
YahooMyWeb
< Prev   Next >
 

Google Search

Google

Get Firefox