当前位置: 首页 > 编程语言 > C#

如何列出所有电脑和上次登录AD的时间?分享

时间:2023-04-11 12:18:25 C#

如何列出所有电脑和上次登录AD的时间?我正在尝试从ActiveDirectory检索计算机名称列表和他们上次登录的日期,并将它们返回到数据表中。获取名称很容易,但是当我尝试添加如下所示的“lastLogon”或“lastLogonTimestamp”时,我为lastLogonTimestamp获得的唯一值是“System._ComObject”publicDataTableGetListOfComputers(stringdomainName){://DC="+域名+",DC=com");DirectorySearchersearch=newDirectorySearcher(入口);字符串查询=“(对象类=计算机)”;search.Filter=查询;search.PropertiesToLoad.Add("名称");search.PropertiesToLoad.Add("lastLogonTimestamp");SearchResultCollectionmySearchResultColl=search.FindAll();DataTable结果=newDataTable();结果.Columns.Add("名称");结果.Columns.Add("lastLogonTimestamp");foreach(mySearchResultColl中的SearchResultsr){DataRowdr=results.NewRow();DirectoryEntryde=sr.GetDirectoryEntry();dr["name"]=de.Properties["Name"].Value;dr["lastLogonTimestamp"]=de.Properties["lastLogonTimestamp"].Value;结果.Rows.Add(博士);de.Close();}返回结果;如果我使用像LDP这样的工具查询AD,我可以看到该属性存在并且填充了数据。我怎样才能得到这些信息?使用System.DirectoryServices.AccountManagement中的ComputerPrincipal类和PrincipalSearcher会更容易。PrincipalContextpc=newPrincipalContext(ContextType.Domain,domainName);PrincipalSearcherps=newPrincipalSearcher(newComputerPrincipal(pc));PrincipalSearchResultpsr=ps.FindAll();foreach(ComputerPrincipalcpinpsr){DataRowdraw(results.NewRow);博士[“姓名”]=cp.姓名;dr["lastLogonTimestamp"]=cp.LastLogon;结果.Rows.Add(博士);}**处理从DirectoryEntry检索到的属性“lastLogonTimestamp”的方法是将其转换为IADSLargeInteger来自:http://www.dotnet247.com/247reference/msgs/31/159934.aspx**The__ComObjectreturnedfor这些类型是用于数值的IADsLargeInteger和用于SecurityDescriptors的IADsSecurityDescriptor。您可以在COM选项卡上包含对ActiveDS类型库的引用,并获取这些接口的定义或手动定义它们。这是一个示例:usingSystem;使用System.DirectoryServices;使用System.Runtime.InteropServices;//这是在ActiveDs.tlb[ComImport][Guid("9068270B-0939-11D1-8BE1-00C04FD8D503")][InterfaceType(ComInterfaceType.InterfaceIsDual)]internalinterfaceIADsLargeInteger{[DispId(0x00000002)]intHighPart{get;设置;}[DispId(0x00000003)]intLowPart{get;设置;}}classClass1{[STAThread]staticvoidMain(string[]args){DirectoryEntryentry=newDirectoryEntry("LDAP://cn=user,cn=users,dc=domain,dc=com");if(entry.Properties.Contains("lastLogon")){IADsLargeIntegerli=(IADsLargeInteger)entry.Properties["lastLogon"][0];longdate=(long)li.HighPartDavidStucki微软件开发人员支持尝试使用IADsLargeInteger(来源)DirectoryEntryuser=DirectoryEntry("LDAP://"+strDN);if(user.Properties.Contains("lastlogontimestamp")){//lastlogontimestamp是一个IADsLargeIntegerIADsLargeIntegerli=(IADsLargeInteger)使用r.Properties["lastlogontimestamp"][0];longlastlogonts=(long)li.HighPart原始问题的简单答案是访问搜索结果中的属性:sr.Properties["lastLogonTimestamp"][0].ToString()DateTime.FromFileTimeUTC(long.Parse(sr.Properties["lastLogonTimestamp"][0].ToString()))获取日期时间值我遇到了类似的问题,我可以访问SearchResult的lastLogonTimestamp属性并获取索引结果中的值,但是在使用SearchResult时。GetDirectoryEntry()我无法访问DirectoryEntry上lastLogonTimestamp属性的有效结果其他人是否遇到过SearchResult.GetDirectoryEntry()返回的DirectoryEntry的问题,因为它与访问lastLogonTimestamp属性有关?以上是C#学习教程:如何列出所有计算机及其最后登录的AD?如果分享的所有内容对您有用,需要了解更多C#学习教程,希望您多多关注---本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:

最新推荐
猜你喜欢