Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# OU时间戳对象_C#_Com_Active Directory - Fatal编程技术网

C# OU时间戳对象

C# OU时间戳对象,c#,com,active-directory,C#,Com,Active Directory,下面是我的一些示例代码,可以在OU中查找所有计算机对象。当我打印出属性字段时,我会得到一个系统。对于一些值,例如lastLogon,lastLogonTimestamp,pwdLastSet,uSNChanged,等等,我假设这些都是某种日期类型的值 如何从中获取日期值?我想要c#解决方案,而不是像这样的powershell解决方案: 谢谢 using (DirectoryEntry entry = new DirectoryEntry("LDAP://" + ou)) { using

下面是我的一些示例代码,可以在OU中查找所有计算机对象。当我打印出属性字段时,我会得到一个
系统。对于一些值,例如
lastLogon
lastLogonTimestamp
pwdLastSet
uSNChanged
,等等,我假设这些都是某种日期类型的值

如何从中获取日期值?我想要c#解决方案,而不是像这样的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)
从中获取日期。

您需要做的是向“活动DS类型库”添加COM引用

然后,下面的代码将为其中一个字段设置日期时间,例如“pwdLastSet”

iadslargeint=(IADsLargeInteger)computer.Properties[“pwdLastSet”][0];
long datelong=((long)largeInt.HighPart):如果只需要此接口,则无需添加对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);
var largeInt=(IAdsLargeInteger)directoryEntry.Properties[propertyName].Value;
var datelong=(largeInt.HighPart
var largeInt = (IAdsLargeInteger)directoryEntry.Properties[propertyName].Value;
var datelong = (largeInt.HighPart << 32) + largeInt.LowPart;
var dateTime = DateTime.FromFileTimeUtc(datelong);