2010-07-07 25 views
0

如何更新不同的電話清單,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; 
    } 

回答

0

我猜你已經抓住了別人的代碼,不知道如何使用它?

您應該明白,由於DirectoryEntry資源未正確關閉,因此此代碼可能(將會)導致嚴重的服務器問題。

Main方法中的每個DirectoryEntry變量都應該包含在using(){}語句中。

0

嘗試是這樣的:

你定義一個類CSVRecord持有從CSV數據 - 讀取,在使用FileHelpers。這個類看起來像這樣:

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(); 
      } 
     } 
    } 
} 

這是否適合你?