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.
Thanks,
BPL
LVL 5
bpl5000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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)
0
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))"
searcher.PropertiesToLoad.Add("name")
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)
            security.AddAccessRule(rule)
            security.SetAccessRule(rule)

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

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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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?
0
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?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.