微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

解决方法

事实证明,答案是在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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐