Quantcast
Channel: わんくま同盟
Viewing all articles
Browse latest Browse all 994

Active Directoryデータのプロパティ出力(VB)

$
0
0

ドメインのユーザ、グループ、コンピュータなどの Active Directory データは DirectoryEntry で表されます。

どういうプロパティ(属性)があるかはオブジェクトによって異なります。

設定した項目は通常、プロパティ(DirectoryEntry.Properties プロパティ:PropertyCollection クラス)に含まれるので、簡単に取得できます。(設定してない項目は含まれないので取得できません。)

オプションのプロパティも ADSI の IADsClass.OptionalProperties プロパティから簡単に取得できます。

プロパティとオプションのプロパティをテキストファイルに出力するサンプルです。対象データと出力ファイルパスを受け取ります。

※ActiveDs、System.IO、System.Security.Principal、System.Text をインポート

 

Private Sub OutputProperties(entry AsDirectoryEntry, filePath As String)

  Dim props = entry.Properties.PropertyNames.Cast(Of String)().OrderBy(Function(s) s).ToList()  'プロパティ名のリスト

  Using writer As NewStreamWriter(filePath, False, Encoding.UTF8)

    For Each pname In props   'プロパティ数分

      Dim val = entry.Properties.Item(pname).Value

      If TypeOf val Is Byte() Then  'バイト配列の時

        Dim pstr = GetByteValue(pname, DirectCast(val, Byte()))   '値を取得

        writer.WriteLine("{0}:{1}", pname, pstr)

      Else  'バイト配列以外の時

        For Each pval In entry.Properties.Item(pname)   '値数分

          writer.WriteLine("{0}:{1}", pname, pval)

        Next

      End If

    Next

  End Using

End Sub

 

Private Sub OutputOptionalProperties(entry AsDirectoryEntry, filePath As String)

  Dim adsi = DirectCast(entry.NativeObject, IADs)   'ADSI オブジェクト

  Dim schema = DirectCast(entry.SchemaEntry.NativeObject, IADsClass'スキーマ オブジェクト

  Dim val As Object

  Using writer As NewStreamWriter(filePath, False, Encoding.UTF8)

    adsi.GetInfoEx(DirectCast(schema.OptionalProperties, Object()), 0'プロパティの値をディレクトリ ストアから読込

    For Each pname As String In DirectCast(schema.OptionalProperties, IEnumerable'オプションのプロパティ数分

      Try

        val = adsi.GetEx(pname)

      Catch

        writer.WriteLine("{0}:<未設定>", pname)

        Continue For

      End Try

 

      If TypeOf val Is Byte() Then  'バイト配列の時

        Dim bstr = BitConverter.ToString(DirectCast(val, Byte()))

        writer.WriteLine("{0}:{1}", pname, bstr)

      Else  'バイト配列以外の時

        For Each pval In DirectCast(val, IEnumerable)   '値数分

          If TypeOf pval Is Byte() Then  'バイト配列の時

            Dim pstr = GetByteValue(pname, DirectCast(pval, Byte()))   '値を取得

            writer.WriteLine("{0}:{1}", pname, pstr)

          Else  'バイト配列以外の時

            writer.WriteLine("{0}:{1}", pname, pval)

          End If

        Next

      End If

    Next

  End Using

End Sub

 

'値を取得

Private Function GetByteValue(name As String, value As Byte()) As String

  If name.Equals("objectSid") Then

    Return NewSecurityIdentifier(value, 0).ToString()

  ElseIf name.Equals("objectGUID") Then

    Return NewGuid(value).ToString()

  Else

    ReturnBitConverter.ToString(value)

  End If

End Function

 

COM Object になっている属性は型名(System.__ComObject)が出力されます。

Domain Admins グループの出力結果です。(左:プロパティ、右:オプションのプロパティ)

※どちらもクリックすると新しいウィンドウで拡大図が表示されます。

Properties OptionalProperties


Viewing all articles
Browse latest Browse all 994

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>