Solved

Windows Login Script mapping User Home Folders

Posted on 2006-10-23
8
605 Views
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
Next

' 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
'-------------------------------------------------------------------
WScript.Quit


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!
0
Comment
Question by:jameso99
8 Comments
 
LVL 13

Expert Comment

by:2hype
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
   ect...

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.


 
0
 
LVL 5

Assisted Solution

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

Ray

' 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_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
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
  Err.Clear
  If Wscript.Version > 5 Then
    Wscript.Sleep 100
  End If
Loop
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
  Next
  Set objGroup = Nothing
End Sub

0
 
LVL 16

Accepted Solution

by:
kshays earned 85 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
            'msgbox(localDrives.item(i))
    End If
Next  

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
                  msgbox(GroupObj.Name)
      Next
      .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
WScript.Quit

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.


0
 
LVL 16

Expert Comment

by:kshays
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.

Kevin
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 21

Expert Comment

by:mcsween
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


'~~~~~~START~~~~~~~~
Option Explicit
Dim ADInfo, ADUser, desc

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

desc = ADUser.description
'~~~~~~~~~END~~~~~~~~~~

0
 
LVL 21

Expert Comment

by:mcsween
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.

Assumed:
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
0
 
LVL 6

Author Comment

by:jameso99
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.

Thanks,

James.
0
 
LVL 16

Expert Comment

by:kshays
ID: 17984205
Anytime, Good luck jameso99.

Kevin
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Forest Trust  vs ADFS 4 154
DNS Issue - incorrect IP shown for host 12 59
DHCP via Multiple VLANS ? 3 62
Increase size of DHCP scope? 16 60
Downtime reduced, data recovered by utilizing an Experts Exchange Business Account Challenge The United States Marine Corps employs more than 200,000 active-duty Marines with operations in four continents, all requiring complex networking system…
Many of us in IT utilize a combination of roaming profiles and folder redirection to ensure user information carries over from one workstation to another; in my environment, it was to enable virtualization without needing a separate desktop for each…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now