Map Network Drive and Printers based on Group Membership
Written by LazyNetworkAdmin
One thing Microsoft leaves out of its Group Policies is the ability to configure mapped network drives and network printers. I guess Microsoft assumes that we all know how to write scripts to do these things. Here is a script that will retreive group membership from Active Directory and map your drives and printers accordingly based on the information in the csv file.
In order for this to work, you will need Windows 2000 Professional or greater and must be attached to a Windows Domain.
Copy and paste the code below into notepad and save it as Logon.vbs
----------COPY EVERYTHING BELOW THIS LINE----------
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,true ' 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
----------COPY EVERYTHING ABOVE THIS LINE----------
Then make yourself a GroupList.csv file and follow this format:
----------CSV FILE CONTENTS----------
Group Name,x:,\\Servername\share_name
Group Name,!!,\\Print_Server\Printer
----------CSV FILE CONTENTS----------
You can add as many lines as necessary to get this done but it is important that there are no spaces above the top line and no spaced below the bottom line, the script will error out.
Once you have your files created, move them to the logon servers(GC) netlogon share and make sure the grouplist.csv has read rights to everyone and the logon.vbs has execute rights to everyone.
Under your Group Policy editor, create a group policy for the logon script and edit the configuration. Navigate to User Configuration-> Windows Settings-> Scripts -> Logon
Add the script from the netlogon folder.
This script was found a while ago and the original author is unknown.
EDIT: Modified on 4/17/06, added "On Error Resume Next" on line 1 to stop error message for end user in the event of an error.
CREDIT: Chey Harden
EDIT: Modified on 6/12/2007, added "true" to the command that maps the network drive to make the connection persistent.
Credit: LazyNetworkAdmin
This information is provided "AS IS" with no warranties expressed or implied.
Very useful article, thanks.
I've used something like that before I started using Desktop Authority. Now I'm free from script writing and many pc management things have become much easier.
Great recommendation to all the lazy admins!
how can i add renaming of mapped drives(from 'share$ on server1' to 'MyFolder') functionality to the above script? Individual scripts are available to do this nothing a success
I like that , but when I test it, is not work with me , I am using server 2003 with xp client ...I changed "WinNT://" to "windows://" in Sub GetGroupInfo... but still not work.....and I didnot understand what do you mean by group name to be filled in GroupList.csv ? I tried group policy name, OU name and user group name but also still not function , please advice me and thanks
I need to obtain the same result however the server will be a stand alone terminal server. Group membership will be defined locally and not through a domain. Can you please post a script for this? i have no idea about scripting...
Sorry guys, that code working very well but the problem in the csv file ,it gives me "The network path was not found" when I put "\\servername\sharedfolder" or even"\\servername.domainname.com\
sharedfolder" ,even I can access both in run command !!!! please help me in this
I run the script and it works perfectly except for the separation of groups.
We have 3 groups, Accounting (with X:=Server01& #92;AccountingShare,
Operations (with Y:=Server01& #92;OperationsShare)
, and Executive (with Z:=Server01\ExecutiveShare). TESTUSER is a member of only Accounting, but when it runs, it maps Z:=ExecutiveShare and Y:=OperationsShare under his access, when all he should get is X:=AccountingShare. Thoughts?
The script works fine but only when the the user is in the domain admin group, when is a "normal" user, the script doesn't work..... Anybody know what's going on and how i correct this? Thanks!
If you already have drives mapped and want to replace them, insert this code just above the drive mapping section
If (fso.DriveExists(drive)) and fso.GetDrive(drive).ShareName path) Then ' If the drive mapping is stale, remove it
WshNetwork.RemoveNetworkDrive drive,true,true
End If
If (fso.DriveExists(drive)) and (fso.GetDrive(drive).ShareName path) Then ' If the drive mapping is stale, remove it
WshNetwork.RemoveNetworkDrive drive,true,true
End If
Great script, but the drives will map for the users even if they are not part of the security group. They of course don't have access to do anything on the drives, but I would rather not show the drives to them.
Here's the contents of my grouplist.csv file
HR Users,H:,\\servername\hr
Acct Users,j:,\\servername\accounting
corporate,j:,\\servername\administration
corporate it,i:,\\servername\it
The login script itself is just as posted above in from the author.
This will allow you to use the %username% var. I didn't come up with this it was done here: http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_23585302.html#a22060312
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)
'*******************
*******************G
roup Mappings Based on Grouplist. csv*****************
****************
If fso. FileExists(logonpath
&"\Grouplist.csv" Then
Set grplist = Fso. OpenTextFile(logonpa
th&"\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 e...