Sponsored Links

Login Form






Lost Password?

Syndicate

Home arrow RSS Feeds
Map Network Drive and Printers based on Group Membership PDF Print E-mail
User Rating: / 17
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 New
Viper_iii  - Thank you   |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!   |07-12-2006 04:35:20
Very useful article, thanks.
I've used something like
that before I started using link:http://www.scr iptlogic.com/product s/DesktopAuthority/. No
w 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.118 |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.102 |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.130 |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...

Than k YOU!
zaed78   |212.116.203.102 |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
"\\serve rname\sharedfold er" or even"\\s
ervername.domainname .com\
sharedfol der" ,even I can access both in
run command !!!! please help me in this
zaed78   |212.116.203.102 |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.56 |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.142 |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.UserDomai n & "/" &
WshNetwork.UserName)
Josh   |72.54.180.238 |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\Acco untingShare,
Operations (with Y:=Server01\Oper ationsShare), and Executive (with
Z:=Server01\Exec utiveShare). 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.198 |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.194 |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(dri ve)) and
fso.GetDrive(drive). ShareName path) Then ' If the drive mapping is stale,
remove it
WshNetwork.Remo veNetworkDrive drive,true,true
End If
Bob Nicksic  - Sorry, missing parenthesis   |69.209.46.194 |28-02-2008 00:16:33
If (fso.DriveExists(dri ve)) and (fso.GetDrive(drive) .ShareName path) Then ' If
the drive mapping is stale, remove it
WshNetwork.Remov eNetworkDrive
drive,true,true
End If
Mosca18   |72.165.37.195 |03-04-2008 21:26:22
If you add
if Drive ="!#" then
WSHNetwork.AddWindow
sPrinterConnection Path
WSHNetwork.S etDefaultPrinter Path

wscript.sleep 1000

end if
After
If Drive = "!!" then

WSHNetwork.AddWindow sPrinterConnection Path

wscript.sleep 1000
end if

End If
you can make a certain Printer
a default printer
slj   |216.17.21.101 |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:,\\s ervername\hr
Ac ct
Users,j:,\\s ervername\accoun ting
corporate,j:,&
#92;\servername& #92;administration
corporate it,i:,\\serv
ername\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.250 |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.126 |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-e xchange.com/Programm ing/Languages/Visual
_Basic/VB_Script/Q_2 3585302.html#a220603 12

On Error Resume Next

Dim
GroupList
Set fso = CreateObject("Sc ripting.FileSystemOb ject"
Set
WshShell = CreateObject("WS cript.Shell"
Se t WshNetwork =
WScript.CreateObject ("WScript.Networ k"

GetGroupInf o()

LogonPath =
fso.GetParentFolderN ame(WScript.ScriptFu llName)
'***********
******************** *******Group Mappings Based on Grouplist.csv*******
******************** ******
If fso.FileExists(logon path&"\Group
list.csv" Then
Set grplist = Fso.OpenTextFile(log onpath&"\Gro
uplist.csv"
' make File into an Array
aGroup = Split(grplist.Readal
l,vbcrlf)
For I = 0 to UBound(GroupList) ' Check Every Group Membership the
user is in (populated into Grouplist)
...
big_grasshopper   |198.234.164.126 |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.126 |22-07-2008 13:34:12
Replace
Path = Mid(mapline,InStr(ma pline,","+1&nbsp '
Extract the path
With
Path
= Replace(Mid(mapline, InStr(mapline,", "+1), "
%username%" , WshNetwork.UserName) ' Extract the path
Viper_iii  - IT Guy     |76.88.205.49 |02-11-2008 16:29:10
Script works and doesn't work...

I have a Gbit 100% Network. Script Runs
about 80% of the time. However I will get calls that the drives aren't showing
on client machines and I don't have an answer. Not a particular machine and the
issue moves around the network.

Could be misconfigured ad dns I use 2 of them
so that could be my issue I guess. The gpo runs the vbs script at logon and the
server could be busy but the logons are processed by others users just fine...


also when I run the script direct the drives map just fine as the
user.

\\s ervername\netlog on\logon.vbs

Also used this on
a nice small 10/100 managed network and the same issue is happening. Ideas for
fixes? The small network only has one dns which is the everything server at
that office.
Write comment
Name:
Email:
 
Title:
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Please input the anti-spam code that you can read in the image.

3.26 Copyright (C) 2008 Compojoom.com / 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