views:

120

answers:

1

We have a company intranet with staff management functions. I would like to begin automating setup of new staff members, e.g. creating Windows accounts for them, creating their home folders, setting up shares, and the like. When a staff member leaves the company I would like to automatically remove their user.

I have been battling to find any good references or libraries for administering users using .Net 2.0.

I am willing to write ADSI code or even WMI code, but need some sample code to kick start the process.

A: 

I discovered the following sample on the Code Project, showing how to add a new user using DirectoryServices:

private void AddUser(string strDoamin, string strLogin, string strPwd)
{
  DirectoryEntry obDirEntry = null;
  try
  {
    obDirEntry = new DirectoryEntry("WinNT://" + strDoamin);
    DirectoryEntries entries = obDirEntry.Children;
    DirectoryEntry obUser = entries.Add(strLogin, "User");
    obUser.Properties["FullName"].Add("Amigo");
    object obRet = obUser.Invoke("SetPassword", strPwd);
    obUser.CommitChanges();
  }
  catch (Exception ex)
  {
    Trace.Warn(ex.Message);
  }
}

But a real breakthrough has come via me signing up on Safari Books Online, and discovering a book there called "The .NET Developer's Guide to Directory Services Programming" - ISBN 10: 0-321-35017-0; ISBN 13: 978-0-321-35017-6

This book seems tailor made for my dilemma as it explains all the basics of programming directory services, then gives specific examples for adding users, setting permissions, etc.

Bork Blatt
Watch out though - the WinNT:// provider is only good for local server user accounts - it will *NOT* work for Active Directory accounts. For those, you'll need to use the LDAP:// provider, which is also extensively documented in Joe Kaplan's excellent book.
marc_s