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

Need VB program to create home directories

We are using Active Directory and I would like a program that cycles thru the users in an OU and checks to see if they have a home directory on our server.  If they do not have a home directory, then it will create it and give the user rights.

For example, bsmith is in OU=Students,OU=Users,DC=MyDomain,DC=Local.  As the program cycles thru this OU, it grabs the username bsmith and checks if a folder named bsmith exists in \\myserver\users\students.  When it doesn't find a folder called bsmith, it creates it and gives bsmith rights to it.

Can someone help me with the code for this program?  I am most familiar with VB6, but I do have VS 2010 and I'm assuming it would be easier to accomplish in VB.net.  Please, I'm looking for code, not links.
  • 3
1 Solution
bpl5000Author Commented:
I should mention that I already have code to create the directory, but I need to check if the folder exists and if not, call the function to create the directory.  The function call to create the folder looks like this...

createHomeFolder(homePath, name, ADcontainername)
Luis PérezSoftware Architect in .NetCommented:
I would try something like this:

Imports System.DirectoryServices
Imports System.IO
Imports System.Security.Principal
Imports System.Security.AccessControl

Dim root As DirectoryEntry = New DirectoryEntry("DC=MyDomain,DC=Local")
Dim searcher As DirectorySearcher = New DirectorySearcher(root)
searcher.Filter = "(&(objectCategory=user)(memberOf=OU=Students,OU=Users))"
Dim results As SearchResultCollection = searcher.FindAll()

For Each result As SearchResult In results
    If Not result.GetDirectoryEntry.Properties("name").Value Is Nothing Then
        Dim folder As String = Path.Combine("\\myserver\users\students", result.GetDirectoryEntry.Properties("name").Value)
        If Not Directory.Exists(folder) Then
            Dim security As DirectorySecurity = New DirectorySecurity()
            security.SetAccessRuleProtection(True, True)
            Dim rule As FileSystemAccessRule = New FileSystemAccessRule("domain\" + result.GetDirectoryEntry.Properties("name").Value, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit Or InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow)

            Directory.CreateDirectory(folder, security)
        End If
    End If

Open in new window

Check the code and see if the permissions are according to which you want to use (for example, you may not want to use FullControl, but you want to use Read and Write).

Hope that helps.
bpl5000Author Commented:
Thanks Roland!  So when you use "memberOf=OU=Students,OU=Users", this will search for users in that OU?  I thought "memberOf" only referred to group membership.  Does it also refer to the OU the user is in?
bpl5000Author Commented:
When it gets to this line...

Dim results As SearchResultCollection = searcher.FindAll()

I get the following error...

"COMException was unhandled. Unspecified error"

Am I missing something?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

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