Active directory 正在更新多个用户的属性

Active directory 正在更新多个用户的属性,active-directory,properties,Active Directory,Properties,如何使用此更新不同电话、IPPhone的列表 static void Main(string[] args) { Console.Write("Enter userid : "); // I would pass this in from the first //Field in the .csv file 2439009 String use

如何使用此更新不同电话、IPPhone的列表

    static void Main(string[] args)
    {
        Console.Write("Enter userid      : "); // I would pass this in from the first 
                                               //Field in the .csv file 2439009
        String username = Console.ReadLine();

        try
        {
            DirectoryEntry myLdapConnection = createDirectoryEntry();

            DirectorySearcher search = new DirectorySearcher(myLdapConnection);
            search.Filter = "(cn=" + uid + ")";
            search.PropertiesToLoad.Add("Telephone","IPPhone");

            SearchResult result = search.FindOne();

            if (result != null)
            {
                // create new object from search result

                DirectoryEntry entryToUpdate = result.GetDirectoryEntry();

                // show existing title

                Console.WriteLine("Current title   : " + entryToUpdate.Properties["Telephone][0].ToString());
                Console.Write("\n\nEnter new title : ");

                // get new title and write to AD

                String newTitle = Console.ReadLine();

                entryToUpdate.Properties["Telephone"].Value = newTelePhone;
                entryToUpdate.Properties["IPPhone"].Value = newIPPhone;

                entryToUpdate.CommitChanges();

                Console.WriteLine("\n\n...new title saved");
            }

            else Console.WriteLine("User not found!");
        }

        catch (Exception e)
        {
            Console.WriteLine("Exception caught:\n\n" + e.ToString());
        }
    }

    static DirectoryEntry createDirectoryEntry()
    {
        // create and return new LDAP connection with desired settings

        DirectoryEntry ldapConnection = new DirectoryEntry("mydomain.dm.com");
        ldapConnection.Path = "LDAP://OU=myusers,DC=sales,DC=US,DC=US";
        ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
        return ldapConnection;
    }

我猜你抓到了别人的代码,却不知道如何使用

您应该了解,由于DirectoryEntry资源未正确关闭,此代码可能(将?)导致严重的服务器问题


Main
方法中的每个DirectoryEntry变量都应该用
using(){}
语句包装起来。

尝试以下方法:

public class CSVRecord
{
    public string EmployeeNumber { get; set; }
    public string TelephoneNumber { get; set; }
    public string IPPhoneNumber { get; set; }
}
您定义了一个类
CSVRecord
,该类保存来自CSV的数据-使用该类读取数据。该类如下所示:

public class CSVRecord
{
    public string EmployeeNumber { get; set; }
    public string TelephoneNumber { get; set; }
    public string IPPhoneNumber { get; set; }
}
读入该类后,需要迭代其元素,并对每个元素进行更新

CSVRecord[] listOfEmployees = (read in via FileHelpers)

// define root for searching your user accounts    
using (DirectoryEntry root = new DirectoryEntry("LDAP://dc=yourcompany,dc=com"))
{
    // set up directory searcher to find users by employeeId
    using (DirectorySearcher searcher = new DirectorySearcher(root))
    {
       searcher.SearchScope = SearchScope.Subtree;

       // iterate over all entries in your list of employees     
       foreach (CSVRecord csvEntry in listOfEmployees)
       {
           searcher.Filter = string.Format("(&(objectCategory=user)(employeeId={0}))", csvEntry.EmployeeNumber);

           // search for that employee         
           SearchResult result = searcher.FindOne();

           // if found - access the DirectoryEntry      
           if (result != null)
           {
               DirectoryEntry foundUser = result.GetDirectoryEntry();

               // update properties from values in CSV
               foundUser.Properties["telephoneNumber"].Value = csvEntry.TelephoneNumber;
               foundUser.Properties["ipPhone"].Value = csvEntry.IPPhoneNumber;

               // save changes back to directory
               foundUser.CommitChanges();
            }
        }
    }
}
这对你有用吗