Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mapping Users Personal Folders according to Site.

Status
Not open for further replies.

mo2783

Programmer
Nov 16, 2003
68
GB
I have a script which maps drives and creates or maps to the users personal folder. We have 2 sites one in Warwick and another in Basildon.

I want to be able to map the users personal folder according to the site they are from (I.e. Warwick, Basildon) as the user folder are located on different servers.

So if a person from Warwick was in Basildon then I would want there personal user folder to be mapped to Warwick server and vice versa. Any ideas how this could be achieved?

At present all users are part of a group (I.e. IT, Sales etc) in active directory. The only way to tell whether they belong to Warwick or Basildon is by Email User Groups.

Code:
'On Error Resume Next

Dim WSHShell, WSHNetwork, objDomain, DomainString, UName, UserObj
Dim Path, strComputer, objFSO, objFolder

Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")

' Create FileSystemObject. So the createFolder method can be appllied .

Set objFSO = CreateObject("Scripting.FileSystemObject")


'Automatically find the domain name
Set objDomain = getObject("LDAP://rootDse")
DomainString = objDomain.Get("dnsHostName")

'Find the Windows Directory
WinDir = WSHShell.ExpandEnvironmentStrings("%WinDir%")

'Get the user name
UName = WSHNetwork.UserName


'Bind to the user object to get user name and check for group memberships later
Set UserObj = GetObject("WinNT://" & DomainString & "/" & UName)

'Get the computer name for use in add-on code later

strComputer = WSHNetwork.ComputerName



For Each GroupObj In UserObj.Groups
	
	Select Case (GroupObj.Name)

	Case "IT"

		if objFSO.FolderExists("\\Server\Users\" & Uname ) = False then
			set objFolder = objFSO.CreateFolder("\\Server\Users\" & Uname)
			WSHNetwork.MapNetworkDrive "U:", "\\Server\Users\"  & Uname	
		Else
			WSHNetwork.MapNetworkDrive "U:", "\\Server\Users\"  & Uname	
		end If
		
		WSHNetwork.MapNetworkDrive "L:", "\\Server\server"
		WSHNetwork.MapNetworkDrive "T:", "\\Server\server"

	End Select

Next




'Clean Up Memory
set UserObj = Nothing
set GroupObj = Nothing
set WSHNetwork = Nothing
set DomainString = Nothing
set WSHSHell = Nothing
Set WSHPrinters = Nothing
  
'Quit the Script
wscript.quit
 
This should show you the site....in the script associate each site name with a server and map accordingly

Dim objADSysInfo : Set objADSysInfo = CreateObject("ADSystemInfo")
WScript.Echo objADSysInfo.SiteName

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Now do you mean a dist list is the only thing that distingishes what user is from a certain site? You pretty much have the code there.

Code:
For Each GroupObj In UserObj.Groups
    
    Select Case (GroupObj.Name)

    Case "IT"

        if objFSO.FolderExists("\\Server\Users\" & Uname ) = False then
            set objFolder = objFSO.CreateFolder("\\Server\Users\" & Uname)
            WSHNetwork.MapNetworkDrive "U:", "\\Server\Users\"  & Uname    
        Else
            WSHNetwork.MapNetworkDrive "U:", "\\Server\Users\"  & Uname    
        end If
        
        WSHNetwork.MapNetworkDrive "L:", "\\Server\server"
        WSHNetwork.MapNetworkDrive "T:", "\\Server\server"

    Case "<Warwick Group Name>"
        UserObj.Put "homedrive","L:"
        UserObj.Put "homedirectory","\\<WarwickServerName>\" + Uname
        UserObj.SetInfo
    Case "<Basildon Group Name>"
         UserObj.Put "homedrive","L:"
         UserObj.Put "homedirectory","\\<BasildonServerName>\" + Uname
         UserObj.SetInfo
    End Select

Next

This is if you want their homedrive letter to be "L:"

Note the syntax for your UserObj = GetObject("WinNT://") may have to be changed to UserObj = GetObject("LDAP://" & DistinguishedName). Here is a function you can place in your code to get that

Code:
'__________________________________________________________________________________________________
' This Function Returns the "distinguishedName" of the user.  Just change the LDAP://dc=<domainname>,dc=<local>
'to meet your needs i.e. LDAP://dc=google,dc=com

Function DistinguishedName(strUserID)
On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2

Set objConn2 = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConn2.Provider = "ADsDSOObject"
objConn2.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConn2

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 

objCommand.CommandText = "SELECT distinguishedName FROM 'LDAP://dc=<domainname>,dc=<local>' WHERE sAMAccountName='" & strUserID  & "'" 


Set objRS1 = objCommand.Execute

objRS1.MoveFirst
Do Until objRS1.EOF
	strOU = objRS1.Fields("distinguishedName").Value	
	objRS1.MoveNext
Loop
DistinguishedName = strOU

objRS1.Close
objConn2.Close
end Function

So your getobject would look like this
Code:
Set UserObj = GetObject("LDAP://" & DistinguishedName(UName))
 
ADSystemInfo also let you get the users distinguished name so you don't have to query AD for it.

i.e.
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Doesn't that only work for the current user logged in? I was assuming that this wasn't a logon script. It doesn't really make sense as a logon script anyways.
 
Thanks guys for the help, still working on it. It is a logon script which will map drives according to location and printers. At present i am trying to workout how to map drives accroding to site.

Mo
 
well I guess they way I wrote it it is a stand alone script and if you just put the info in to active directory as the home drive it will map it as such and it will auto map the home drive when they log in.

That is an active directory setting so you shouldn't have to make a logon script for that, as long as you have some way to identify who is at what site, which you have stated you did.

The only way to tell whether they belong to Warwick or Basildon is by Email User Groups.

 
w33mhz

I was talking to the network administrator, and he claims that the only way to tell if a person is part of the Warwick or Basildon group is by the email user group they belong to.

I am aware that you can map to the drive using homedirectory within active directory, but i have other drives that need mapping dependent upon site.

Any ideas help you can offer? would be much appreciated, i have been trying to work this one out for a few days.

Thanks

Mo
 
Did you try to code I posted to see if it shows you site information that might be usable for the purpose you have stated?

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
dm4ever

I am unsure how you would associate site name to server in the script can you please help?

This should show you the site....in the script associate each site name with a server and map accordingly

Dim objADSysInfo : Set objADSysInfo = CreateObject("ADSystemInfo")
WScript.Echo objADSysInfo.SiteName


Mo
 
If objADSysInfo.SiteName shows you the site information then there are a few ways you could associate the name with a server...

If LCase(objADSysInfo.SiteName) = "site abc" Then
strServerName = "server for site abc"
End If

...code...map to server specified above

or use a Select statement instead...

or build a dictionary

Dim dictSites: Set dictSites= CreateObject("Scripting.Dictionary")
dictSites.CompareMode = vbTextCompare

dictSites.Add "site abc", "server for site abc"
dictSites.Add "side def", "server for site def"

strServerName = dictSites(objADSysInfo.SiteName)

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
dm4ever

When using the following code

Code:
Dim objADSysInfo : Set objADSysInfo = CreateObject("ADSystemInfo")
WScript.Echo objADSysInfo.SiteName

i get the following messgae "default-first-site-name" at both sites. not sure if this means anything?

Mo
 
It probably means the subnets have not been associated with appropriate site names in Active Directory...you'll have to talk to you AD administrator.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
mo2783

That may be correct but you can look that up. All I am saying is that if you know that email groups you can then you can run the code snippet and put in the approriate group, drive path, and drive letter it will change the AD property for that user to the corresponding folder path. You can still have a logon script to map other drives. Anyways if you don't want to do it that way you can still use the code to produce a logon script based off of group membership.
 
Mo2783, take a look at the following two FAQs which I think will help you out.

faq329-5798
faq329-5908

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top