Sponsored Links

Login Form






Lost Password?

Syndicate

Home
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
handcrafted jewellry - wholesale jewelry     | 222.131.25.xxx | 20-11-2008 05:37:57
wholesale jewelry
handmade jewelry
handcrafted jewelry
jewelry wholesale
wholesale handmade jewelry
pearl jewelry
wholesale pearl jewelry
wholesale turquoise jewelry
wholesale coral jewelry
wholesale shell jewelry
wholesale gemstone jewelry
wholesale crystal jewelry
fashion jewelry
wholesale jewellry
[url=h...
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.
fashion jewelry - fashion jewelry     | 222.131.25.xxx | 20-11-2008 05:38:30
wholesale jewelry
jewelry store
fashion jewelry
crystal jewelry
jewelry wholesale
pearl jewelry
wholesale crystal
wholesale pearl
wholesale turquoise
wholesale semi-precious jewelry
wholesale coral
wholesale shell
wholesale swarovski crystal
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
Buba - Mapped drives   | 200.193.81.xxx | 01-12-2008 06:31:15
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?
Buba - Mapped drives 2   | 200.193.81.xxx | 02-12-2008 05:14:39
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!

Const HKEY_CURRENT_USER = &H80000001

strComputer = "."

Set oReg=GetObject(& #34;winmgmts:{impers
onationLevel=imperso
nate}!\\" & _
strComputer & "\root& #92;default:StdRegPr
ov"


oReg.DeleteKey HKEY_CURRENT_USER, "Network\A"
oReg.DeleteKey HKEY_CURRENT_USER, "Network\B"
oReg.DeleteKey HKEY_CURRENT_USER, "Network\C"
oReg.DeleteKey HKEY_CURRENT_USER, "Network\D"
oReg.DeleteKey HKEY_CURRENT_USER, "Network\E"
oReg.DeleteKey HKEY_CURRENT_USER, "Network\F"
oReg.DeleteKey HKEY_CURRENT_USER, "Network\G"
oReg.DeleteKey HKEY_CURRENT_USER, "Network\H"
oReg.DeleteKey ...
Buba - Mapped drives   | 200.193.81.xxx | 02-12-2008 05:16:33
Sorry, the script was line broke
url for microsoft site:

http://www.microsoft.com/technet/scriptcenter/scripts/os/registry/default.mspx?mfr=true
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