ドメインコンローラー内のオブジェクトを列挙
所属ドメコンで権限がある場合。
所属ドメコン内のオブジェクトを列挙。
参照設定:Com の「Active DS Type Library」と「System.DirectoryServices」が必要。
static void Main( string[] args ) { try { DirectoryEntry rootEntry = new DirectoryEntry( "LDAP://rootDSE" ); DirectoryEntry directoryEntry = new DirectoryEntry( string.Format( "LDAP://{0}/{1}" , rootEntry.Properties["dnsHostName"].Value , rootEntry.Properties["defaultNamingContext"].Value ) ); DirectorySearcher searcher = new DirectorySearcher( directoryEntry ); using ( SearchResultCollection collection = searcher.FindAll() ) { foreach ( SearchResult result in collection ) { DirectoryEntry resultEntry = result.GetDirectoryEntry(); foreach ( string propertyName in resultEntry.Properties.PropertyNames ) { Console.WriteLine( "{0}[{1}]", propertyName, ValueToString( resultEntry.Properties[propertyName].Value ) ); } } } } catch ( Exception e ) { Console.WriteLine( e ); Console.ReadLine(); } Console.WriteLine( "end" ); Console.ReadLine(); } private static string ValueToString( object obj ) { StringBuilder builder = new StringBuilder(); if ( obj is IADsDNWithBinary ) { return ( (IADsDNWithBinary)obj ).DNString; } if ( obj is IADsLargeInteger ) { ulong high = ( (ulong)( (IADsLargeInteger)obj ).HighPart ) << 32; uint low = (uint)( (IADsLargeInteger)obj ).LowPart; ulong value = high + low; return value.ToString(); } if ( obj is IADsSecurityDescriptor ) { return ( (IADsSecurityDescriptor)obj ).Control.ToString(); } if ( obj is byte ) { return ((byte)obj).ToString( "X" ); } if ( obj is byte[] ) { byte[] b = (byte[])obj; if ( b.Length == 0x10 ) { Guid guid = new Guid( b ); return guid.ToString(); } if ( ( b.Length >= 3 ) && ( b[0] == 1 ) ) { return new SecurityIdentifier( b, 0 ).ToString(); } foreach ( byte num in b ) { builder.Append( num.ToString( "X" ) ).Append( "-" ); } return builder.Remove( builder.Length - 1, 1 ).ToString(); } if ( !( obj is Array ) ) { return obj.ToString(); } IEnumerable enumerable = (IEnumerable)obj; foreach ( object obj2 in enumerable ) { builder.Append( ValueToString( obj2 ) ).Append( " " ); } builder.Remove( builder.Length - 1, 1 ); return builder.ToString(); }