2012-05-10 69 views
0

我試圖recive在我的廣告中的所有計算機,以及他們當中哪些whos當前登錄。我試着通過檢查「lastLogonStamp」但是返回錯誤的值,說我的服務器在八天前登錄到了AD。即使我重啓服務器,它也是如此。我從另一個問題在這裏代碼:獲取所有計算機的列表,並且如果它登錄到AD

How to list all computers and the last time they were logged onto in AD?

public DataTable GetListOfComputers(string domain, string userName, string password) 
    { 
     DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, 
       userName, password, AuthenticationTypes.Secure); 
     DirectorySearcher search = new DirectorySearcher(entry); 
     string query = "(objectclass=computer)"; 
     search.Filter = query; 

     search.PropertiesToLoad.Add("name"); 
     search.PropertiesToLoad.Add("lastLogonTimestamp"); 

     SearchResultCollection mySearchResultColl = search.FindAll(); 

     DataTable results = new DataTable(); 
     results.Columns.Add("name"); 
     results.Columns.Add("lastLogonTimestamp"); 

     foreach (SearchResult sr in mySearchResultColl) 
     { 
      DataRow dr = results.NewRow(); 
      DirectoryEntry de = sr.GetDirectoryEntry(); 
      dr["name"] = de.Properties["Name"].Value; 
      dr["lastLogonTimestamp"] = DateTime.FromFileTimeUtc(long.Parse(sr.Properties["lastLogonTimestamp"][0].ToString())); 
      results.Rows.Add(dr); 
      de.Close(); 
     } 

     return results; 
    } 
+0

感謝您的快速響應! 我在哪裏通過憑據?在我的例子中,我將IP地址傳遞給AD服務器,「管理員」,「密碼」。我如何驗證自己? PS:我的網絡服務器不在AD中。我只是連接到它外部。 DS。 –

回答

0

如果你使用.NET 3.5,你可以使用一個PrincipalSearcher和「查詢通過例如」主要做你的搜索:

// create your domain context 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

// define a "query-by-example" principal - here, we search for a ComputerPrincipal 
ComputerPrincipal qbeComputer = new ComputerPrincipal(ctx); 

// create your principal searcher passing in the QBE principal  
PrincipalSearcher srch = new PrincipalSearcher(qbeComputer); 

// find all matches 
foreach(var found in srch.FindAll()) 
{ 
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....   
    ComputerPrincipal cp = found as ComputerPrincipal; 

    if(cp != null) 
    { 
     string computerName = cp.Name; 
     DateTime lastLogon = cp.LastLogon; 
    } 
} 

如果您還沒有 - 絕對看MSDN文章Managing Directory Security Principals in the .NET Framework 3.5這很好地說明如何使新功能的最佳使用System.DirectoryServices.AccountManagement。或者查看MSDN documentation on the System.DirectoryServices.AccountManagement命名空間。

相關問題