OU timestamp ComObject
本文关键字:ComObject timestamp OU | 更新日期: 2023-09-27 17:49:14
下面是我找到OU中所有计算机对象的一些示例代码。当我打印出属性字段时,我得到几个值的System.__ComObject
,如lastLogon
、lastLogonTimestamp
、pwdLastSet
、uSNChanged
等。我假设这些都是日期类型的值。
如何获得日期值?我想要一个c#解决方案,而不是像这样的powershell解决方案:https://sweeneyops.wordpress.com/2012/06/11/active-directory-timestamp-conversion-through-powershell/
感谢using (DirectoryEntry entry = new DirectoryEntry("LDAP://" + ou))
{
using (DirectorySearcher searcher = new DirectorySearcher(entry))
{
searcher.Filter = ("(objectClass=computer)");
searcher.SizeLimit = int.MaxValue;
searcher.PageSize = int.MaxValue;
foreach (SearchResult result in searcher.FindAll())
{
DirectoryEntry computer = result.GetDirectoryEntry();
foreach(string propName in computer.Properties.PropertyNames)
{
foreach(object value in computer.Properties[propName])
{
Console.WriteLine($"{propName}: {value}");
}
}
}
}
}
我知道有一个很长的对象,我可以使用DateTime.FromFileTime(longType)
来获取日期。
这里已经回答了:如果只需要这个接口,则不需要添加对COM Types库的引用。
要使用COM类型,您可以在自己的代码中定义接口:
[ComImport, Guid("9068270b-0939-11d1-8be1-00c04fd8d503"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
internal interface IAdsLargeInteger
{
long HighPart
{
[SuppressUnmanagedCodeSecurity] get; [SuppressUnmanagedCodeSecurity] set;
}
long LowPart
{
[SuppressUnmanagedCodeSecurity] get; [SuppressUnmanagedCodeSecurity] set;
}
}
,并以同样的方式使用:
var largeInt = (IAdsLargeInteger)directoryEntry.Properties[propertyName].Value;
var datelong = (largeInt.HighPart << 32) + largeInt.LowPart;
var dateTime = DateTime.FromFileTimeUtc(datelong);
还有一篇很好的文章,解释了如何解释ADSI数据
你需要做的是添加一个COM引用到"Active DS Type Library"
然后下面的代码将从其中一个字段中生成一个日期时间,例如"pwdLastSet"
IADsLargeInteger largeInt = (IADsLargeInteger)computer.Properties["pwdLastSet"][0];
long datelong = (((long)largeInt.HighPart) << 32) + largeInt.LowPart;
DateTime pwSet = DateTime.FromFileTimeUtc(datelong);