Sponsored Links

Login Form






Lost Password?

Syndicate

Home arrow Knowledgebase arrow Windows arrow Map Network Drive and Printers based on Group Membership
Map Network Drive and Printers based on Group Membership PDF Print E-mail
User Rating: / 14
PoorBest 
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.

Comments
Add NewSearch
Viper_iii - Thank you   | Registered | 16-10-2006 11:13:18
This single script has made my life much easier...

I thank you so much for posting it!!!
JohnnyBear - interesting and useful!   | Registered | 07-12-2006 04:35:20
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!
easy1ndian - rename mapped drives   | 195.229.227.xxx | 13-02-2007 12:04:38
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
zaed78 - What about server 2003 with xp   | 212.116.203.xxx | 19-11-2007 03:11:59
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
Edward Bonello   | 194.106.69.xxx | 19-11-2007 10:43:20
Hi,

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

Thank YOU!
zaed78   | 212.116.203.xxx | 20-11-2007 07:06:22
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
zaed78   | 212.116.203.xxx | 20-11-2007 09:14:32
It is OK guys , I correct my CSV file and it is work now .....
Thanks
LazyNetworkAdmin - sorry     | 75.45.168.xxx | 20-11-2007 20:28:46
Sorry I didnt reply to you earlier. My notifications stopped working.
Glad to hear it is all working well now.
Dave   | 12.86.105.xxx | 04-12-2007 13:14:53
I receive a Windows Script Host error:
Error: Invalid Syntax
Code: 800401E4

This references the line: Set UserObj = GetObject ("WINDOWS://" & wshNetwork.UserDomain & "/" & WshNetwork.UserName)
Josh   | 72.54.180.xxx | 04-12-2007 16:20:43
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?
Daniel - Doubt   | 200.216.254.xxx | 02-01-2008 07:57:23
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!
Bob Nicksic - remapping drives to different   | 69.209.46.xxx | 22-02-2008 02:53:04
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
Bob Nicksic - Sorry, missing parenthesis   | 69.209.46.xxx | 28-02-2008 00:16:33
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
Mosca18   | 72.165.37.xxx | 03-04-2008 21:26:22
If you add
if Drive ="!#" then
WSHNetwork. AddWindowsPrinterCon
nection Path
WSHNetwork.SetDefaultPrinter Path
wscript.sleep 1000

end if
After
If Drive = "!!" then
WSHNetwork. AddWindowsPrinterCon
nection Path
wscript.sleep 1000
end if

End If
you can make a certain Printer a default printer
slj   | 216.17.21.xxx | 06-06-2008 10:03:12
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.

Any help would be greatly appreciated!
Carl Leiker - Desktop Engineer   | 65.196.65.xxx | 07-07-2008 14:38:33
This works, but it will not map if I use a varible %username% to map the drive. Anyone figure out a solution for that?
big_grasshopper - IT Manager   | 198.234.164.xxx | 22-07-2008 13:31:22
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...
big_grasshopper   | 198.234.164.xxx | 22-07-2008 13:33:11
Replace
Path = Mid(mapline,InStr(ma
pline,","+1) ' Extract the path
With
Path = Replace(Mid(mapline,
InStr(mapline,","+1), "%username%", WshNetwork.UserName) ' Extract the path
big_grasshopper   | 198.234.164.xxx | 22-07-2008 13:34:12
Code:
Replace
Path = Mid(mapline,InStr(ma
pline,","+1) ' Extract the path
With
Path = Replace(Mid(mapline,
InStr(mapline,","+1), "%username%", WshNetwork.UserName) ' Extract the path
cvnmm     | 222.247.150.xxx | 27-08-2008 09:13:25
A maiden with many wooers often chooses the worst.All the treasures of the earth would not bring back one lost moment.
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