【问题标题】:Query LDAP for all computer objects created in the last 24 hours查询过去 24 小时内创建的所有计算机对象的 LDAP
【发布时间】:2012-05-10 14:16:35
【问题描述】:

我正在尝试使用 LDAP 查询来返回过去 24 小时内创建的所有计算机对象。我的代码目前如下所示:

//Declare new DirectoryEntry and DirectorySearcher
DirectoryEntry domainRoot = new DirectoryEntry("LDAP://rootDSE");
string rootOfDomain = domainRoot.Properties["rootDomainNamingContext"].Value.ToString();
DirectorySearcher dsSearch = new DirectorySearcher(rootOfDomain);

//Set the properties of the DirectorySearcher
dsSearch.Filter = "(&(objectClass=Computer)(whenCreated>" + dateFilter.ToString() + "))";
dsSearch.PageSize = 2000;
dsSearch.PropertiesToLoad.Add("distinguishedName");
dsSearch.PropertiesToLoad.Add("whenCreated");
dsSearch.PropertiesToLoad.Add("description");
dsSearch.PropertiesToLoad.Add("operatingSystem");
dsSearch.PropertiesToLoad.Add("name");

//Execute the search
SearchResultCollection computersFound = dsSearch.FindAll();

此代码不返回任何对象,并且我确定在过去 24 小时内创建了帐户。

编辑:我使用以下代码解决了这个问题:

GetCompList(DateTime.Now.AddDays(-1)); //This sets the filter to one day previous

//Declare new DirectoryEntry and DirectorySearcher
DirectoryEntry domainRoot = new DirectoryEntry("LDAP://rootDSE");
string rootOfDomain = domainRoot.Properties["rootDomainNamingContext"].Value.ToString();
DirectorySearcher dsSearch = new DirectorySearcher(rootOfDomain);

//Set the properties of the DirectorySearcher
dsSearch.Filter = "(&(objectClass=Computer)(whenCreated>=" + dateFilter.ToString("yyyyMMddHHmmss.sZ") + "))";
dsSearch.PageSize = 2000;
dsSearch.PropertiesToLoad.Add("distinguishedName");
dsSearch.PropertiesToLoad.Add("whenCreated");
dsSearch.PropertiesToLoad.Add("description");
dsSearch.PropertiesToLoad.Add("operatingSystem");
dsSearch.PropertiesToLoad.Add("name");


//Execute the search
SearchResultCollection computersFound = dsSearch.FindAll();

秘诀就是:

dsSearch.Filter = "(&(objectClass=Computer)(whenCreated>=" + dateFilter.ToString("yyyyMMddHHmmss.sZ") + "))";

【问题讨论】:

  • 是的,您的问题是什么?你遇到了什么错误?虽然我是用 Java 做的,但想法是一样的。
  • 请不要在标题前加上“C# -”之类的前缀。这就是标签的用途。
  • 我已经解决了这个问题。抱歉原版太差了,我一整天都在忙着打架。
  • 我重新打开了这个问题。请发布您的解决方案(连同解释 - 需要日期的特定格式)作为答案。谢谢。
  • 请把您所说的部分作为您的修复并将其发布为答案;您将能够将其标记为答案,以便其他人更容易找到可能遇到与您一样头痛的问题!

标签: c# filter active-directory ldap


【解决方案1】:

原来答案在于 whenCreated 过滤器的格式。根据This blogpost,whenCreated 的过滤器必须格式化为“yyyyMMddHHmmss.sZ”,其中 Z 是与 UTC 的偏移量。我所做的是创建了一个名为

的方法
private void GetCompList(DateTime dateFilter) //This overloaded version of GetCompList takes a parameter of type DateTime, and only returns computers that were built after dateFilter
    {
        try
        {
            //Convert the dateFilter to a format appropriate for an LDAP query
            int offset = -8;
            //string strDateFilter = convertToCrazyFormat(dateFilter, offset);

            //string strDateFilter = dateFilter.ToString("yyyyMMddhhmmss");

            //Declare new DirectoryEntry and DirectorySearcher
            DirectoryEntry domainRoot = new DirectoryEntry("LDAP://rootDSE");
            string rootOfDomain = domainRoot.Properties["rootDomainNamingContext"].Value.ToString();
            DirectorySearcher dsSearch = new DirectorySearcher(rootOfDomain);

            //Set the properties of the DirectorySearcher
            dsSearch.Filter = "(&(objectClass=Computer)(whenCreated>=" + dateFilter.ToString("yyyyMMddHHmmss.s" + offset.ToString()) + "))";
            dsSearch.PageSize = 2000;
            dsSearch.PropertiesToLoad.Add("distinguishedName");
            dsSearch.PropertiesToLoad.Add("whenCreated");
            dsSearch.PropertiesToLoad.Add("description");
            dsSearch.PropertiesToLoad.Add("operatingSystem");
            dsSearch.PropertiesToLoad.Add("name");

然后我这样调用方法:

GetCompList(DateTime.Now.AddDays(-1));//Pass in a negative value that represents the time period you want objects from, in this case the last day

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 2012-07-05
    • 2011-03-15
    相关资源
    最近更新 更多