Upload
michio-koyama
View
805
Download
0
Embed Size (px)
DESCRIPTION
ユーザやグループなどの属性とその値を出力
Citation preview
Active Directory データのプロパティ出力
ユーザやグループなどの属性とその値を出力する
小山 三智男mitchin
2
アプリケーションの参照設定
• .NET から Active Directory の色々な情報にアクセスするために System.DirectoryServices アセンブリを参照する必要があります。
• ドメインやサイト関連は System.DirectoryServices. ActiveDirectory 名前空間にそれらを表すクラスがあり、 Active Directory の管理タスクを自動化するために使用されます。
• Active Directory 内のデータにアクセスするために使用されるのは System.DirectoryServices 名前空間で、オブジェクトをカプセル化する DirectoryEntry クラスやクエリを実行する DirectorySearcher クラスなどがあります。
• ADSI(Active Directory Services Interfaces) を使用してネイティブなオブジェクトを扱う場合は Active DS Type Libraryを参照する必要があります。
3
対象のデータ
管理ツール「 Active Directory ユーザとコンピュータ」で管理する以下のオブジェクト• ユーザ• グループ• コンピュータ• 組織単位( OU)• プリンタ• 共有フォルダ
4
ADSI のインターフェイス
基本インターフェイスは IADs インターフェイスで、各オブジェクトはこのインターフェイスを継承しています。
オブジェクトとそれに対応するインターフェイス
DirectoryEntry.NativeObject プロパティの値を上記インターフェイスにキャストできます。
ユーザ IADsUser
グループ IADsGroup
コンピュータ IADsComputer
組織単位( OU) IADsOU
プリンタ IADsPrintQueue
共有フォルダ
5
Windows Server 2008 以上なら
管理ツール「 Active Directory ユーザとコンピュータ」で確認できます。「表示」メニューの「拡張機能」にチェックを入れます。
6
属性エディタ
「属性エディタ」タブを開きます。
7
値が設定されているものだけ表示するには
「フィルタ」ボタンをクリックして「値を持つ属性のみを表示」にチェックを入れます。(フィルタ設定は維持されます)
8
属性の割り出し
属性の名前とその値から設定項目を割り出します。
9
プログラムから確認するには
DirectoryEntry.Properties プロパティ( PropertyCollection クラス)からプロパティ(属性)とその値を列挙して取得します。未設定の属性や通常取得できないオプションの属性を取得するには、スキーマ オブジェクト( SchemaEntry プロパティ)のネイティブ ADSI オブジェクト( IADsClass )の OptionalProperties からプロパティ(属性)を列挙して、ディレクトリ ストアからサポートされているプロパティの値を読み込んで取得します。
サンプルコードは次の名前空間をインポートしています。•ActiveDs•System.IO•System.Security.Principal•System.Text
10
属性の出力サンプル( VB )
Private Sub OutputProperties(entry As DirectoryEntry, filePath As String)
Dim props = entry.Properties.PropertyNames.Cast( Of String)().OrderBy(Function(s) s).ToList() ' プロパティ名のリスト Using writer As New StreamWriter(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 UsingEnd Sub
11
属性の出力サンプル( C# )
private void OutputProperties(DirectoryEntry entry, string filePath) { var props = entry.Properties.PropertyNames.Cast<string>().OrderBy( s => s).ToList(); // プロパティ名のリスト using (var writer = new StreamWriter(filePath, false, Encoding.UTF8)) { foreach (var pname in props) { // プロパティ数分 var val = entry.Properties[pname].Value; if (val is byte[]) { // バイト配列の時 var pstr = GetByteValue(pname, (byte[])val); // 値取得 writer.WriteLine("{0} : {1}", pname, pstr); } else { // バイト配列以外の時 foreach (var pval in entry.Properties[pname]) { // 値数分 writer.WriteLine("{0} : {1}", pname, pval) } } } }}
12
オプションの属性の出力サンプル( VB )
Private Sub OutputOptionalProperties( entry As DirectoryEntry, filePath As String) Dim adsi = DirectCast(entry.NativeObject, IADs) 'ADSI オブジェクト Dim schema = DirectCast(entry.SchemaEntry.NativeObject,
IADsClass) Dim val As Object Using writer As New StreamWriter(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
13
オプションの属性の出力サンプル( VB )
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 UsingEnd Sub
14
オプションの属性の出力サンプル( C# )
private void OutputOptionalProperties( DirectoryEntry entry, string filePath) { var adsi = (IADs)entry.NativeObject; //ADSI オブジェクト var schema = (IADs)entry.SchemaEntry.NativeObject; object val; using (var writer = new StreamWriter(filePath, false, Encoding.UTF8))
{ // プロパティの値を読込 adsi.GetInfoEx((object[])schema.OptionalProperties, 0); // オプションのプロパティ数分 foreach (string pname in (IEnumerable)schema.OptionalProperties)
{ try { val = adsi.GetEx(pname); } catch { writer.WriteLine("{0} : < 未設定 >", pname); continue; }
15
オプションの属性の出力サンプル( C# )
if (val is byte[]) { // バイト配列の時 var bstr = BitConverter.ToString((byte[])val); writer.WriteLine("{0} : {1}", pname, bstr); } else { // バイト配列以外の時 foreach (var pval in (IEnumerable)val) { // 値数分 if (pval is byte[]) { // バイト配列の時 var pstr = GetByteValue(pname, (byte[])val); // 値取得 writer.WriteLine("{0} : {1}", pname, pstr); } else { // バイト配列以外の時 writer.WriteLine("{0} : {1}", pname, pval) } } } } }}
16
バイト配列の値取得サンプル( VB )
Private Function GetByteValue( name As String, value As Byte()) As String
If name.Equals("objectSid") Then Return New SecurityIdentifier(value, 0).ToString() ElseIf name.Equals("objectGUID") Then Return New Guid(value).ToString() Else Return BitConverter.ToString(value) End IfEnd Function
17
バイト配列の値取得サンプル( C# )
private string GetByteValue(string name, byte[] value){ if (name.Equals("objectSid")) { return new SecurityIdentifier(value, 0).ToString(); } else if (name.Equals("objectGUID")) { return new Guid(value).ToString(); } else { return BitConverter.ToString(value); }}
18
属性の出力結果
19
オプションの属性の出力結果
20
詳細や関連情報はブログ等で
.NET から Active Directory にアクセス ~ユーザ情報の取得と表示~http://www.slideshare.net/mitchin227/display-user
.NET から Active Directory データにアクセス ~グループ情報を表示する~http://www.slideshare.net/mitchin227/display-group
ユーザやグループの検索http://blogs.wankuma.com/mitchin/archive/2013/06/26/327958.aspx
ネイティブ ADSI オブジェクトhttp://blogs.wankuma.com/mitchin/archive/2013/07/01/327981.aspx
ドメインユーザのプロパティ画面の項目と属性の対応(全般タブ)http://blogs.wankuma.com/mitchin/archive/2013/07/21/328010.aspx
ドメインユーザのプロパティ画面の項目と属性の対応(所属するグループタブ)http://blogs.wankuma.com/mitchin/archive/2013/08/19/328068.aspx
Active Directory データのプロパティ出力http://blogs.wankuma.com/mitchin/archive/2013/09/19/328123.aspxhttp://blogs.wankuma.com/mitchin/archive/2013/09/20/328126.aspx