LDAP查询在过去24小时内创建的所有计算机对象(Query LDAP for all comput

2019-09-16 22:18发布

我试图使用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") + "))";

Answer 1:

事实证明,答案是在whenCreated过滤器的格式。 据该博文 ,为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


文章来源: Query LDAP for all computer objects created in the last 24 hours