下面是一些示例代码,它可以在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)来获取它的日期。
发布于 2018-12-04 11:37:40
已经在这里回答了:如果您只需要这个接口,就不需要添加对COM类型库的引用。
要使用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数据
发布于 2015-05-15 00:32:03
您需要做的是向"Active DS类型库“添加一个COM引用
然后,下面的代码将使其中一个字段的日期超时,例如"pwdLastSet“
IADsLargeInteger largeInt = (IADsLargeInteger)computer.Properties["pwdLastSet"][0];
long datelong = (((long)largeInt.HighPart) << 32) + largeInt.LowPart;
DateTime pwSet = DateTime.FromFileTimeUtc(datelong);https://stackoverflow.com/questions/30249417
复制相似问题