2012-05-29 56 views
1

我試圖接收當前連接到AD的所有計算機,以及哪些用戶已登錄到AD。我已經嘗試了ComputerPrincipal.LastLogon屬性,但我得到的價值完全是關閉的,大概一週。確定AD中的計算機是否有用戶登錄

我想知道AD中的哪些計算機可用。有另一種方法可以使用嗎?

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "192.168.0.101:389", "Administrator", "XXXX"); 

// 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 = new DateTime(); 
     lastLogon = cp.LastLogon; 
     DateTime convertedDate = DateTime.SpecifyKind(DateTime.Parse(cp.LastLogon.ToString()), DateTimeKind.Utc); 

     var kind = convertedDate.Kind; 
     DateTime dt = convertedDate.ToLocalTime(); 

     Response.Write(cp.Name+" : "+ dt.ToString()+ "<br />"); 
    } 
} 

編輯:

我想打印出來是這樣的:

電腦1:真 電腦2:假 電腦3:假 電腦4:真

查詢電腦當前是否登錄不可能?我只需要一個布爾,對或錯。

+0

提供的一個例子你得到了什麼以及你的期望。 –

+0

Active Directory是一個**靜態**資源 - 它沒有「動態」運行時信息,例如誰在哪臺計算機上登錄。恐怕您無法從Active Directory中檢索您要查找的信息。 –

+0

lastLogon發生了什麼 - 爲什麼當您立即用'cp.LastLogin'替換'new DateTime()'時, '?同樣,當你剛剛在前一行指定時,爲什麼要訪問'convertedDate.Kind'? –

回答

0

這是一個經典的MCSE問題。 lastlogon字段對於該特定DC是本地的,並且不是全局複製的。

您需要查詢每個AD域控制器並查找每個AD域控制器的最近日期。

+0

我只有一個域控制器。 –

0

您需要查詢forest/domain中所有域控制器上的安全事件日誌,以確保該用戶已登錄某臺計算機。然後應使用WMI聯繫該工作站以檢查用戶是否仍登錄。

您可能感興趣的事件是Logon events,其中包含交互式登錄類型(登錄ID 4624,登出4634)。

但是,當PC與域(筆記本非常常見)和用戶註銷失去連接時,沒有任何域控制器會收到註銷事件。

用戶可以在沒有域的情況下實際登錄以前已經在PC上創建了他的帳戶的本地緩存。

正如有人說lastlogon和的lastLogonTimestamp不能用於可靠的方法來追蹤用戶登錄,檢查thisthis

一些示例here

等詳細信息herehere

+0

我想查詢安全事件日誌有點棘手? 你有什麼例子嗎? 謝謝! –

+0

查看我的更新回答 – rkosegi

相關問題