• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 615
  • Last Modified:

Windows Login Script mapping User Home Folders

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
jameso99
Asked:
jameso99
2 Solutions
 
2hypeCommented:
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
 
usacadenaCommented:
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
 
Kevin HaysIT AnalystCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Kevin HaysIT AnalystCommented:
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
 
mcsweenSr. Network AdministratorCommented:
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
 
mcsweenSr. Network AdministratorCommented:
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
 
jameso99Author Commented:
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
 
Kevin HaysIT AnalystCommented:
Anytime, Good luck jameso99.

Kevin
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now