Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Windows Login Script mapping User Home Folders

Posted on 2006-10-23
Medium Priority
Last Modified: 2008-01-09
I would like to map my users' home data folder (i.e. \\server\d\data\James_Files) to the Z drive in my login script.  Right now I just map the drives in this fasion:

' Login Script v 0.3
' Modified 9/29/2006

' Set up
Dim WshShell
Dim WshNetwork
Dim UserIn

Set WshShell = CreateObject("WScript.Shell")
Set WshNetwork = CreateObject("WScript.Network")

' Map network drives

' Disconnect all network drives first
On Error Resume Next
Set colDrives = WshNetwork.EnumNetworkDrives()

For i = 0 to colDrives.Count-1 Step 2
    WshNetwork.RemoveNetworkDrive colDrives.Item(i), True

' Now Map the New ones
WshNetwork.MapNetworkDrive "M:", "\\server\D"
WshNetwork.MapNetworkDrive "N:", "\\server\Archive"

' Map user's home directory
'UserIn = WshNetwork.UserName
'WshNetwork.MapNetworkDrive "Z:","\\server\D\Data\" & UserIn & "$"
'WScript.Echo "Welcome " & UserIn

' Add Networked printers from the server

My problem is that the user name (i.e. "james") is not the same as the folder in the DATA folder I have set up ("James_Files" or whatever).  I did not originally set this up, otherwise I would have made it a lot easier on myself!

Is there a way I can map the user's home folder, using AD or something, to this drive letter?

Thanks SO much people!
Question by:jameso99
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 13

Expert Comment

ID: 17792954
How many users do you have?  Are the Data files standard?  can you not map a drive to "\\Servername\D\Data\" & UserIn & "_Files$"

If james logs in will his home directory be james_files
   Sue logs in sue_files

You could go into each user in Active Directory, Go to Profiles - Go to Home folder and connect Z:  to there home drive for each user.


Assisted Solution

usacadena earned 160 total points
ID: 17793377
Took me a long time to to get this script, look forward to the points.


' ads_home_drv.vbs
' VBScript logon script program.
' ----------------------------------------------------------------------

' You have a royalty-free right to use, modify, reproduce, and
' distribute this script file in any way you find useful, provided that
' you agree that the copyright owner above has no warranty, obligations,
' or liability for such use.

Option Explicit

Dim objRootDSE, objTrans, strNetBIOSDomain, objNetwork, strNTName
Dim strUserDN, strComputerDN, objGroupList, objUser, strDNSDomain
Dim strComputer, objComputer
Dim strHomeDrive, strHomeShare

' Constants for the NameTranslate object.
Const ADS_NAME_TYPE_1779 = 1

Set objNetwork = CreateObject("Wscript.Network")

' Loop required for Win9x clients during logon.
strNTName = ""
On Error Resume Next
Do While strNTName = ""
  strNTName = objNetwork.UserName
  If Wscript.Version > 5 Then
    Wscript.Sleep 100
  End If
On Error GoTo 0

' Determine DNS domain name from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

' Use the NameTranslate object to find the NetBIOS domain name from the
' DNS domain name.
Set objTrans = CreateObject("NameTranslate")
objTrans.Init ADS_NAME_INITTYPE_GC, ""
objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain
strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)
' Remove trailing backslash.
strNetBIOSDomain = Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1)

' Use the NameTranslate object to convert the NT user name to the
' Distinguished Name required for the LDAP provider.
objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strNTName
strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)

' Bind to the user object in Active Directory with the LDAP provider.
Set objUser = GetObject("LDAP://" & strUserDN)

' Map user home directory.
strHomeShare = objUser.homeDirectory
If strHomeShare <> "" Then
  strHomeDrive = objUser.homeDrive
  If strHomeDrive = "" Then
    strHomeDrive = "H:"
  End If
  On Error Resume Next
  objNetwork.MapNetworkDrive strHomeDrive, strHomeShare
  If Err.Number <> 0 Then
    On Error GoTo 0
    objNetwork.RemoveNetworkDrive strHomeDrive, True, True
    objNetwork.MapNetworkDrive strHomeDrive, strHomeShare
  End If
  On Error GoTo 0
End If

' Map a network drive if the user is a member of the group.
If IsMember(objUser, "Domain Admin") Then
  On Error Resume Next
  objNetwork.MapNetworkDrive "M:", "\\filesrv01\admin"
  If Err.Number <> 0 Then
    On Error GoTo 0
    objNetwork.RemoveNetworkDrive "M:", True, True
    objNetwork.MapNetworkDrive "M:", "\\filesrv01\admin"
  End If
  On Error GoTo 0
End If

' Use the NameTranslate object to convert the NT name of the computer to
' the Distinguished name required for the LDAP provider. Computer names
' must end with "$".
strComputer = objNetwork.computerName
objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strComputer & "$"
strComputerDN = objTrans.Get(ADS_NAME_TYPE_1779)

' Bind to the computer object in Active Directory with the LDAP
' provider.
Set objComputer = GetObject("LDAP://" & strComputerDN)

' Add a printer connection if the computer is a member of the group.
If IsMember(objComputer, "Room 231") Then
  objNetwork.AddPrinterConnection "LPT1:", "\\PrintServer\Printer3"
End If

' Clean up.
Set objRootDSE = Nothing
Set objTrans = Nothing
Set objNetwork = Nothing
Set objGroupList = Nothing
Set objUser = Nothing
Set objComputer = Nothing

Function IsMember(objADObject, strGroup)
' Function to test for group membership.
' objADObject is a user or computer object.
' strGroup is the NT Name of the group to test.
' objGroupList is a dictionary object with global scope.
' Returns True if the user or computer is a member of the group.
' Subroutine LoadGroups is called once for each different objADObject.

  If IsEmpty(objGroupList) Then
    Set objGroupList = CreateObject("Scripting.Dictionary")
  End If
  If Not objGroupList.Exists(objADObject.sAMAccountName & "\") Then
    Call LoadGroups(objADObject, objADObject)
    objGroupList(objADObject.sAMAccountName & "\") = True
  End If
  IsMember = objGroupList.Exists(objADObject.sAMAccountName & "\" _
    & strGroup)
End Function

Sub LoadGroups(objPriADObject, objSubADObject)
' Recursive subroutine to populate dictionary object with group
' memberships. When this subroutine is first called by Function
' IsMember, both objPriADObject and objSubADObject are the user or
' computer object. On recursive calls objPriADObject still refers to the
' user or computer object being tested, but objSubADObject will be a
' group object. The dictionary object objGroupList keeps track of group
' memberships for each user or computer separately. For each group in
' the MemberOf collection, first check to see if the group is already in
' the dictionary object. If it is not, add the group to the dictionary
' object and recursively call this subroutine again to enumerate any
' groups the group might be a member of (nested groups). It is necessary
' to first check if the group is already in the dictionary object to
' prevent an infinite loop if the group nesting is "circular".

  Dim colstrGroups, objGroup, j
  objGroupList.CompareMode = vbTextCompare
  colstrGroups = objSubADObject.memberOf
  If IsEmpty(colstrGroups) Then
    Exit Sub
  End If
  If TypeName(colstrGroups) = "String" Then
    Set objGroup = GetObject("LDAP://" & colstrGroups)
    If Not objGroupList.Exists(objPriADObject.sAMAccountName & "\" _
        & objGroup.sAMAccountName) Then
      objGroupList(objPriADObject.sAMAccountName & "\" _
        & objGroup.sAMAccountName) = True
      Call LoadGroups(objPriADObject, objGroup)
    End If
    Set objGroup = Nothing
    Exit Sub
  End If
  For j = 0 To UBound(colstrGroups)
    Set objGroup = GetObject("LDAP://" & colstrGroups(j))
    If Not objGroupList.Exists(objPriADObject.sAMAccountName & "\" _
        & objGroup.sAMAccountName) Then
      objGroupList(objPriADObject.sAMAccountName & "\" _
        & objGroup.sAMAccountName) = True
      Call LoadGroups(objPriADObject, objGroup)
    End If
  Set objGroup = Nothing
End Sub

LVL 16

Accepted Solution

Kevin Hays earned 340 total points
ID: 17798724
Heck, i'll throw my little login script that maps via group membership and home folder, even though my home folders are on a linux based system.

Dim objNet, objADInfo, objUser
Dim strDC, strFileServer

set objNet            = CreateObject("Wscript.Network")                   'create network object
set objADInfo            = CreateObject("ADSystemInfo")                          'create AD object
set objUser             = GetObject("LDAP://" & objADInfo.UserName)      'create user object
Set localDrives            = objNet.EnumNetworkDrives

sIT_Windows            = "\\servername\IT\"
sIT_Linux                = "\\servername\it"
sPublic_Windows             = "\\servername\public"
sPublic_Linux                 = "\\servername\Public"
sFileServer                = "\\servername\"
sHome                      = "\\servername\" & objNet.UserName   'Home Folder

'Remove all network drives allready mapped
For i = 0 to localDrives.Count -1 step 2
      If Not localDrives.item(i) = "" Then
            objNet.RemoveNetworkDrive localDrives.Item(i), True, True
    End If

Wscript.sleep 200

With objNet
      For Each GroupObj in objUser.Groups
      Select Case LCase(GroupObj.Name)
                  case "cn=information technology"
                        .MapNetworkDrive "I:", sIT_Windows                  'DFS share on DC
                        .MapNetworkDrive "J:", sPublic_Windows                   'public on DC
                        .MapNetworkDrive "K:", sFileServer & "it"              'IT on FileServer
                        Wscript.sleep 400

                  case "cn=sales"
                        .MapNetworkDrive "P:", sFileServer & "sales"             'Map /sales/ folder
                        Wscript.sleep 400

                  case "cn=customer service"
                        .MapNetworkDrive "M:", sFileServer & "customer_service"      'Map /customer_service/ folder
                        Wscript.sleep 400

                  case "cn=marketing"
                        .MapNetworkDrive "O:", sFileServer & "marketing"      'Map /marketing/ folder
                        Wscript.sleep 400

                  case "cn=human resource"
                        .MapNetworkDrive "Q:", sFileServer & "human_resource"      'Map /human_resource/ folder
                        Wscript.sleep 400

            End Select
      .MapNetworkDrive "Y:", sPublic_Linux             'Map public folder in linux
      .MapNetworkDrive "Z:", sHome                  'Map home folder

End With

'cleanup resources
set objNet                   = Nothing
set objADInfo                    = Nothing
set objUser                  = Nothing
set sFileServer                   = Nothing
set sPublic_Windows       = Nothing
set sPublic_Linux            = Nothing
set sHome                   = Nothing
set sIT_Windows              = Nothing
set sIT_Linux                  = Nothing

'quit wscript

I don't claim to be the best scripting guy out there though, but this serves my purpose and it's small also :)  It just goes through each group that the user belongs to and validates each one in the case select then maps based on if it matches that criteria or not.

There is a directory in the linux server in the format of first.lastname which is the user account in AD.  I just use that to map to the (objNet.UserName) property.

Flexible connectivity for any environment

The KE6900 series can extend and deploy computers with high definition displays across multiple stations in a variety of applications that suit any environment. Expand computer use to stations across multiple rooms with dynamic access.

LVL 16

Expert Comment

by:Kevin Hays
ID: 17798743
Really, you probably need a way to modify large groups of user objects with a script that will modify the "home" directory to "\\servername\home\%username%" instead of having to go through each one manually though.

LVL 22

Expert Comment

ID: 17799253
I see what's going on here.  This should be pretty easy to fix but you will have to add a comment for each user in ADUC.

Add the folder path to the description field of the user object then use the following lines to query this value.  The variable desc will hold whatever string is typed into the description field in the AD user Object.

You will have to incorporate this into your logon script and will use a line like"
WshNetwork.MapNetworkDrive "Z:",desc
to map the drive

Option Explicit
Dim ADInfo, ADUser, desc

Set ADInfo = CreateObject("ADSystemInfo")
Set ADUser = GetObject("LDAP://" & ADInfo.UserName)

desc = ADUser.description

LVL 22

Expert Comment

ID: 17799272
If this is not what you want then please let me know.  I'm assuming the user's data folders are all over the place or do not follow any standard naming.  If they follow some kind of standard naming and are all in the same share let me know and I can do something much easier for you.

Users data folders are setup something like this

Username: James
data folder: \\server1\data\james_files

Username: Sue
data folder: \\server1\data\sue_data

Username: Fred
Data Folder: \\server2\someshare\Fred_files

Author Comment

ID: 17982639
Thanks guys.  I liked kshay's implementation with using the AD groups from LDAP AD.  Sounds like I may just rename some folders and totally redo this whole thing.


LVL 16

Expert Comment

by:Kevin Hays
ID: 17984205
Anytime, Good luck jameso99.


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enterprise networks where VoIP phones have been deployed frequently use port configurations that allow both a computer and an IP phone to be plugged into the same switch port but use different VLANs. On Cisco equipment I'm referring to the "native V…
This article offers some helpful and general tips for safe browsing and online shopping. It offers simple and manageable procedures that help to ensure the safety of one's personal information and the security of any devices.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question