C# 查询过去24小时内创建的所有计算机对象的LDAP

C# 查询过去24小时内创建的所有计算机对象的LDAP,c#,filter,active-directory,ldap,C#,Filter,Active Directory,Ldap,我试图使用LDAP查询返回在过去24小时内创建的所有计算机对象。我的代码当前如下所示: //Declare new DirectoryEntry and DirectorySearcher DirectoryEntry domainRoot = new DirectoryEntry("LDAP://rootDSE"); string rootOfDomain = domainRoot.Properties["rootDomainNamingContext"].Value.ToString();

我试图使用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();
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
这段代码不返回任何对象,我确信在过去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") + "))";

事实证明,答案就在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");
然后我像这样调用该方法:

//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();
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

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