我正在尝试接收当前连接到AD的所有计算机,以及其中哪些计算机具有登录到AD的用户。我尝试过使用ComputerPrincipal的.LastLogon属性,但得到的值是完全关闭的,大约一周。
我想知道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:真
如果计算机当前已登录,是否无法查询该计算机?我只需要一个布尔值,真或假。
发布于 2012-05-29 20:19:53
这是一个经典的MCSE问题。lastlogon字段是特定DC的本地字段,不是全局复制的。
您需要查询每个AD域控制器,并查找每个控制器的最新日期。
发布于 2012-05-29 20:29:56
https://stackoverflow.com/questions/10798437
复制相似问题