Active Directory将用户移动到不同的OU

本文关键字:OU 移动 Directory 用户 Active | 更新日期: 2023-09-27 17:49:53

我正在编写一个程序,该程序将自动为离开我们网络的用户分离过程。它执行的任务之一是将用户帐户从它所在的OU移动到Former Employees OU。我一直有这个步骤的问题,即使我没有任何问题与DirectoryServices做其他过程。这是我到目前为止的代码(注意:我知道我需要停止捕获和吃掉所有异常。这将在发布前得到解决和纠正。任何关于我应该捕获哪些异常和不应该捕获哪些异常的建议也将不胜感激):

private const string AD_DOMAIN_NAME = "domain.com";
private const string AD_NEW_PASSWORD = "TestPassword123";
private const string AD_FORMER_EMPLOYEES_OU = "LDAP://OU=Former Employees,DC=domain,DC=com";
static DirectoryEntry CreateDirectoryEntry(string connectionPath, 
        string adUserName, string adPassword)
{
    DirectoryEntry ldapConnection = null;
    try
    {
        ldapConnection = new DirectoryEntry(AD_DOMAIN_NAME, adUserName, adPassword);
        ldapConnection.Path = connectionPath;
        ldapConnection.AuthenticationType = AuthenticationTypes.Secure;                
    }
    catch (Exception ex)
    {
        MessageBox.Show("Exception Caught in createDirectoryEntry():'n'n" + ex.ToString());
    }
    return ldapConnection;
}
private void btnProcessSeparation_Click(object sender, EventArgs e)
{
    if (cboOffice.SelectedItem != null && lstUsers.SelectedItem != null)
    {
        string userOU = cboOffice.SelectedItem.ToString();
        string userName = lstUsers.SelectedItem.ToString();
        string userDn = "LDAP://OU=" + userOU + ",OU=Employees,DC=domain,DC=com";
        using (DirectoryEntry ldapConnection = CreateDirectoryEntry(userDn))
        {
            using (DirectorySearcher searcher = CreateDirectorySearcher(ldapConnection,
                SearchScope.OneLevel, "(samaccountname=" + userName + ")", "samaccountname"))
            {
                SearchResult result = searcher.FindOne();
                if (result != null)
                {
                    using (DirectoryEntry userEntry = result.GetDirectoryEntry())
                    {
                        if (userEntry != null)
                        {
                            using (DirectoryEntry formerEmployees = CreateDirectoryEntry(
                                AD_FORMER_EMPLOYEES_OU))
                            {
                                userEntry.MoveTo(formerEmployees); // This line throws an DirectoryServicesCOMException.
                            }
                            userEntry.CommitChanges();
                            userEntry.Close();
                            MessageBox.Show("Separation for {0} has completed successfully.", userName);
                        }
                    }
                }
            }
        }
    }
    else
    {
        MessageBox.Show("Error, you did not select an OU or a user. Please try again.");
    }
}

上面的代码工作得很好,直到userEntry.MoveTo(formerEmployees);行。这一行抛出了一个DirectoryServicesCOMException,附加信息说An invalid dn syntax has been specified.,这很奇怪,因为我使用的格式与其他DirectoryEntry相同,工作得很好。我添加了一个断点,并确认formerEmployees被设置为:LDAP://OU=Former Employees,DC=domain,DC=com。我直接从活动目录中的OU的distinguishedName属性复制了LDAP://之后的所有内容,以确保它是正确的。

是OU名称中的空格导致问题吗?我让它正常工作了一次,然后转移到其他任务,一定是改变了什么东西破坏了它。我一直在看代码太多,我想,只是似乎看不出为什么它认为我发送一个无效的dn。

感谢所有的帮助!

Active Directory将用户移动到不同的OU

希望对您有所帮助:

 DirectoryEntry eLocation = Conexion.Conectar(Localitation);
 DirectoryEntry nLocation =Conexion.Conectar(NewLocalitation);
                        string newName = eLocation.Name;
                        eLocation.MoveTo(nLocation, newName);
                        nLocation.Close();
                        eLocation.Close();

@David为我指出了正确的方向,确保我对OU拥有正确的权限,之后我发现了问题。我添加了一个重载的CreateDirectoryEntry方法,该方法使用用户名和密码(这就是我在上面的代码中放入的内容)。但是,如果您注意到上面的代码,我调用的方法只接受连接路径。

谢谢你的帮助@David!