得到的所有计算机的列表,并如果它被记录到公元公元、计算机、列表

2023-09-08 13:25:47 作者:傻逼二号

我想recive我的广告的所有计算机,也是其中哪些卫生组织当前登录的,我已经tryed这样做,通过检查lastLogonStamp,但返回错误的值,说我的服务器登录到AD八天前。即使我重新启动服务器,它说的一样。我从另外一个问题在这里的code:

How列出所有计算机和上一次他们登录到公元?

 公开数据表GetListOfComputers(字符串域名,用户名字符串,字符串密码)
    {
        的DirectoryEntry条目=新的DirectoryEntry(LDAP://+域名,
                用户名,密码,AuthenticationTypes.Secure);
        DirectorySearcher从搜索=新DirectorySearcher从(输入);
        查询字符串=(对象类=计算机);
        sea​​rch.Filter =查询;

        sea​​rch.PropertiesToLoad.Add(姓名);
        sea​​rch.PropertiesToLoad.Add(的lastLogonTimestamp);

        SearchResultCollection mySearchResultColl = search.FindAll();

        DataTable的结果=新的DataTable();
        results.Columns.Add(姓名);
        results.Columns.Add(的lastLogonTimestamp);

        的foreach(在mySearchResultColl信息搜索结果SR)
        {
            DataRow的博士= results.NewRow();
            的DirectoryEntry德= sr.GetDirectoryEntry();
            博士[名称] = de.Properties [名称]值;
            博士[的lastLogonTimestamp] = DateTime.FromFileTimeUtc(long.Parse(sr.Properties [的lastLogonTimestamp] [0]的ToString()));
            results.Rows.Add(DR);
            de.Close();
        }

        返回结果;
    }
 

解决方案

如果您使用的是.NET 3.5,你可以使用 PrincipalSearcher 和查询逐例如主要做你的搜索:

  //创建域上下文
PrincipalContext CTX =新PrincipalContext(ContextType.Domain);

//定义一个查询通过例如委托 - 在这里,我们搜索ComputerPrincipal
ComputerPrincipal qbeComputer =新ComputerPrincipal(CTX);

//创建你的本金搜索传入QBE校长
PrincipalSearcher SRCH =新PrincipalSearcher(qbeComputer);

//找到所有匹配
的foreach(VAR在srch.FindAll发现())
{
    //做任何在这里 - 发现的类型是主 - 它可以是用户,组,计算机.....
    ComputerPrincipal CP =发现为ComputerPrincipal;

    如果(CP!= NULL)
    {
       字符串计算机名= cp.Name;
       日期时间lastLogon = cp.LastLogon;
    }
}
 
怎样在同一个电脑查找上网痕迹

如果您还没有 - 绝对阅读MSDN文章管理目录安全主体在.NET Framework 3.5 这很好地说明如何使新功能的最佳使用 System.DirectoryServices.AccountManagement 。还是看在System.DirectoryServices.AccountManagement 命名空间。

I'm trying to recive all computers in my AD and also which of them whos currently logged in. I've tryed doing this by checking the "lastLogonStamp" but that returns the wrong value, saying my server was logged into AD eight days ago. Even if I restart the server it says the same. I got the code from another question here:

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

解决方案

If you're using .NET 3.5 and up, you can use a PrincipalSearcher and a "query-by-example" principal to do your searching:

// 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;
    }
}

If you haven't already - absolutely read the MSDN article Managing Directory Security Principals in the .NET Framework 3.5 which shows nicely how to make the best use of the new features in System.DirectoryServices.AccountManagement. Or see the MSDN documentation on the System.DirectoryServices.AccountManagement namespace.

 
精彩推荐
图片推荐