マイナーでもいいよね??

殆どVB系、でも .NET じゃない VB は知らないよん

目次

Blog 利用状況

書庫

日記カテゴリ

Active Directoryデータのプロパティ出力のCOM対応版(C#)

Active Directoryデータのプロパティ出力の COM 対応版の C# のコードです。

※2013/12/10 Trustee はユーザやグループの名前部分のみ出力、AceType は値とその列挙体文字列を出力するよう修正しました。

※2013/12/11 GetValue メソッドの name パラメータを削除しました。

以前からの変更点は VB のコードの方に書いてます。

 

public static 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 if (val is IADsSecurityDescriptor)   //セキュリティ記述子の時

      {

        var sd = (IADsSecurityDescriptor)val;

        var aceGroups = ((IADsAccessControlList)sd.DiscretionaryAcl).Cast<IADsAccessControlEntry>().OrderBy(

          ace => System.IO.Path.GetFileName(ace.Trustee)).ThenBy(ace => ace.AccessMask).ThenBy(ace => ace.AceFlags).ThenBy(

          ace => ace.AceType).ThenBy(ace => ace.Flags).GroupBy(ace => String.Format("{0}|{1}|{2}|{3}|{4}",

            ace.Trustee, ace.AccessMask, ace.AceFlags, ace.AceType, ace.Flags)).ToList();   //プロパティ値でグループ化したACE

        var ctr = 0;

        writer.WriteLine(pname);

        foreach (var aceGroup in aceGroups)   //ACE数分

        {

          var ace = aceGroup.First();

          ctr++;

          writer.WriteLine(" {0:D2}. Trustee   :{1}", ctr, System.IO.Path.GetFileName(ace.Trustee));

          writer.WriteLine(" {0:D2}. AccessMask:{1}", ctr, ToEnumValueText(ace.AccessMask, typeof(ADS_RIGHTS_ENUM)));

          writer.WriteLine(" {0:D2}. AceFlags  :{1}", ctr, ToEnumValueText(ace.AceFlags, typeof(ADS_ACEFLAG_ENUM)));

          writer.WriteLine(" {0:D2}. AceType   :{1}", ctr, ToEnumValueText(ace.AceType, typeof(ADS_ACETYPE_ENUM)));

          writer.WriteLine(" {0:D2}. Flags     :{1}", ctr, ToEnumValueText(ace.Flags, typeof(ADS_FLAGTYPE_ENUM)));

        }

      }

      else if (val is IADsLargeInteger)   //大きい整数の時

      {

        var li = GetLargeIntegerValue(pname, (IADsLargeInteger)val);  //大きい整数値を取得

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

      }

      else  //それ以外の時

      {

        foreach (var pval in entry.Properties[pname])   //値数分

        {

          var value = GetValue(pval);   //値を取得

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

        }

      }

    }

  }

}

 

public static void OutputOptionalProperties(DirectoryEntry entry, string filePath)

{

  var schema = (IADsClass)entry.SchemaEntry.NativeObject;   //スキーマ オブジェクト

  var props = (object[])schema.OptionalProperties;  //オプションのプロパティ

  using (var writer = new StreamWriter(filePath, false, Encoding.UTF8))

  {

    entry.Invoke("GetInfoEx", props, 0);  //プロパティをディレクトリ ストアから読込

    foreach (string pname in props)   //オプションのプロパティ数分

    {

      var pvcol = entry.Properties[pname];  //PropertyValueCollection

      if (pvcol.Value == null)   //値がない時

      {

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

        continue;

      }

 

      if (pvcol.Value is byte[])  //バイト配列の時

      {

        var bstr = GetByteValue(pname, (byte[])pvcol.Value);  //バイト値を取得

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

      }

      else if (pvcol.Value is IADsLargeInteger)   //大きい整数の時

      {

        var li = GetLargeIntegerValue(pname, (IADsLargeInteger)pvcol.Value);  //大きい整数値を取得

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

      }

      else  //それ以外の時

      {

        foreach (var pval in pvcol)   //値数分

        {

          if (pval is byte[])   //バイト配列の時

          {

            var pstr = GetByteValue(pname, (byte[])pval);   //バイト値を取得

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

          }

          else  //バイト配列以外の時

          {

            var value = GetValue(pval);   //値を取得

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

          }

        }

      }

    }

  }

}

 

//バイト値を取得

private static 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);

  }

}

 

//大きい整数値を取得

private static object GetLargeIntegerValue(string name, IADsLargeInteger value)

{

  //var lval = (long)((uint)value.LowPart + (((long)value.HighPart) << 32));

  var lval = Convert.ToInt64(value.HighPart.ToString("x8") + value.LowPart.ToString("x8"), 16);

  if (name.Equals("lockoutTime"))   //ロックアウトしたことがある時

  {

    if (lval > 0)   //ロックアウト中又はロックアウト期間が過ぎただけの時

    {

      return String.Format("{0}({1})", lval, DateTime.FromFileTime(lval));  //判り易いよう日付も付ける

    }

    return lval;   //明示的にロック解除又はロックアウト期間が過ぎてログオン成功しているので値は0

  }

 

  if (IsInteger(name, lval))  //整数の時

  {

    return lval;

  }

  if ((lval == 0) || (lval == Int64.MaxValue))

  {

    return "(なし)";

  }

  return DateTime.FromFileTime(lval);   //eq. new DateTime(1601, 1, 1).AddTicks(lval).ToLocalTime()

}

 

//整数かどうか

private static bool IsInteger(string name, long value)

{

  if (name.Equals("maxStorage"))

  {

    return true;

  }

  if (name.StartsWith("msDS-"))

  {

    return false;

  }

 

  if (name.StartsWith("uSN"))

  {

    return true;

  }

  return false;

}

 

//値を取得

private static object GetValue(object value)

{

  if ((value is DateTime) == false)   //日付ではない時

  {

    return value;

  }

 

  var d = Convert.ToDateTime(value);

  return d.Date.Equals(new DateTime(1601, 1, 1)) ? d : d.ToLocalTime();

}

 

//列挙体のプロパティ値をテキスト化

private static string ToEnumValueText(int value, Type enumType)

{

  if (enumType == typeof(ADS_ACETYPE_ENUM))   //AceTypeの時

  {

    return String.Format("{0}({1})", value, Enum.ToObject(enumType, value));

  }

 

  Func<int, string> selector = e => Enum.ToObject(enumType, e).ToString();

  var values = Enum.GetValues(enumType).Cast<int>().Where(

    e => (value & e) == e).OrderBy(selector).Select(selector).ToList();   //設定されている値の列挙体文字列

 

  if (values.Count == 0)   //設定されている値がない時

  {

    return value.ToString();

  }

   return String.Format("{0}({1})", value, String.Join(" | ", values));

}

投稿日時 : 2013年12月5日 22:58

コメントを追加

# Illikebuisse uuiav 2021/07/04 0:48 pharmacepticacom

cloroquin https://www.pharmaceptica.com/

# re: Active Directory????????????COM???(C#) 2021/07/06 12:11 hydrochoriquine

is chloroquine a sulfa drug https://chloroquineorigin.com/# hydroxychloroquine sulfate 200mg

# re: Active Directory????????????COM???(C#) 2021/07/12 11:02 hydroxyclorine

chloroquinolone https://chloroquineorigin.com/# what is hydroxychloroquine made of

# re: Active Directory????????????COM???(C#) 2021/07/23 16:15 plaquenil hydroxychloroquine sulfate

cloroquine https://chloroquineorigin.com/# where to get hydroxychloroquine

# If some one wants expert view regarding running a blog after that i propose him/her to pay a visit this web site, Keep up the fastidious work. 2021/09/02 19:38 If some one wants expert view regarding running a

If some one wants expert view regarding running a blog after that
i propose him/her to pay a visit this web site, Keep up the fastidious work.

# As the admin of this website is working, no uncertainty very quickly it will be famous, due to its quality contents. 2021/09/05 12:15 As the admin of this website is working, no uncert

As the admin of this website is working, no uncertainty very quickly it will
be famous, due to its quality contents.

# Hi there, just wanted to mention, I loved this article. It was practical. Keep on posting! quest bars https://www.iherb.com/search?kw=quest%20bars quest bars 2021/09/13 21:26 Hi there, just wanted to mention, I loved this art

Hi there, just wanted to mention, I loved this article.
It was practical. Keep on posting! quest bars https://www.iherb.com/search?kw=quest%20bars quest bars

# Hi there, just wanted to mention, I loved this article. It was practical. Keep on posting! quest bars https://www.iherb.com/search?kw=quest%20bars quest bars 2021/09/13 21:27 Hi there, just wanted to mention, I loved this art

Hi there, just wanted to mention, I loved this article.
It was practical. Keep on posting! quest bars https://www.iherb.com/search?kw=quest%20bars quest bars

# Hi there, just wanted to mention, I loved this article. It was practical. Keep on posting! quest bars https://www.iherb.com/search?kw=quest%20bars quest bars 2021/09/13 21:28 Hi there, just wanted to mention, I loved this art

Hi there, just wanted to mention, I loved this article.
It was practical. Keep on posting! quest bars https://www.iherb.com/search?kw=quest%20bars quest bars

# Hi there, just wanted to mention, I loved this article. It was practical. Keep on posting! quest bars https://www.iherb.com/search?kw=quest%20bars quest bars 2021/09/13 21:29 Hi there, just wanted to mention, I loved this art

Hi there, just wanted to mention, I loved this article.
It was practical. Keep on posting! quest bars https://www.iherb.com/search?kw=quest%20bars quest bars

# Genuinely when someone doesn't know afterward its up to other visitors that they will help, so here it happens. 2021/10/26 2:38 Genuinely when someone doesn't know afterward its

Genuinely when someone doesn't know afterward its up to
other visitors that they will help, so here it happens.

# Genuinely when someone doesn't know afterward its up to other visitors that they will help, so here it happens. 2021/10/26 2:39 Genuinely when someone doesn't know afterward its

Genuinely when someone doesn't know afterward its up to
other visitors that they will help, so here it happens.

# igpfleenidvd 2021/12/04 2:49 cegousjc

what is chloroquine https://chloroquinemil.com/

# Hello! Someone in my Facebook group shared this website with us so I came to take a look. I'm definitely enjoying the information. I'm bookmarking and will be tweeting this to my followers! Excellent blog and brilliant design and style. 2021/12/05 20:24 Hello! Someone in my Facebook group shared this we

Hello! Someone in my Facebook group shared this website with us
so I came to take a look. I'm definitely enjoying the information. I'm bookmarking
and will be tweeting this to my followers!

Excellent blog and brilliant design and style.

# stromectol drug http://stromectolabc.com/
stromectol price in india 2022/02/08 2:55 Busjdhj

stromectol drug http://stromectolabc.com/
stromectol price in india

# stromectol http://stromectolabc.com/
ivermectin 6 mg tablets 2022/02/08 9:42 Busjdhj

stromectol http://stromectolabc.com/
ivermectin 6 mg tablets

# ivermectin 4 tablets price http://stromectolabc.com/
stromectol 3 mg tablets price 2022/02/08 16:46 Busjdhj

ivermectin 4 tablets price http://stromectolabc.com/
stromectol 3 mg tablets price

# buy doxycycline https://doxycyline1st.com/
doxycycline hydrochloride 100mg 2022/02/26 8:33 Doxycycline

buy doxycycline https://doxycyline1st.com/
doxycycline hydrochloride 100mg

# best treatment for ed https://erectiledysfunctionpills.shop/ 2022/10/14 22:53 Erectile

best treatment for ed https://erectiledysfunctionpills.shop/

# date online https://topdatingsites.fun/
matchmaking services 2022/11/15 0:33 DatingTop

date online https://topdatingsites.fun/
matchmaking services

# dating services contact germany https://datingsiteonline.site/
onlinelovedating 2022/12/05 23:52 Tading

dating services contact germany https://datingsiteonline.site/
onlinelovedating

# generic aralen 250 mg 2022/12/25 17:34 MorrisReaks

generic chloroquine 250mg https://hydroxychloroquinex.com/

# best ed pill https://edpills.science/
top ed pills 2023/01/07 13:55 EdPills

best ed pill https://edpills.science/
top ed pills

# website dating service https://datingonline1st.com/
silver singles dating site 2023/01/17 22:33 Dating

website dating service https://datingonline1st.com/
silver singles dating site

# doors2.txt;1 2023/03/14 14:45 lxKWFIjKYHKViheJX

doors2.txt;1

# doors2.txt;1 2023/03/14 16:21 FBycJhfoHzZNcLyYA

doors2.txt;1

# canadian drug store legit https://pillswithoutprescription.pro/# 2023/05/14 22:17 PillsPresc

canadian drug store legit https://pillswithoutprescription.pro/#

# international pharmacies https://pillswithoutprescription.pro/# 2023/05/16 4:49 PillsPro

international pharmacies https://pillswithoutprescription.pro/#

# new ed treatments https://edpills.ink/# - erection pills online 2023/07/26 20:09 EdPills

new ed treatments https://edpills.ink/# - erection pills online

# farmacie online autorizzate elenco https://farmaciait.pro/ acquisto farmaci con ricetta 2023/12/04 7:08 Farmacia

farmacie online autorizzate elenco https://farmaciait.pro/ acquisto farmaci con ricetta

# fox sweetie https://sweetiefox.pro/ - sweetie fox new
2024/03/06 20:27 SwitieFox

fox sweetie https://sweetiefox.pro/ - sweetie fox new

# gates of olympus slot - https://gatesofolympus.auction/ gates of olympus demo oyna 2024/03/27 19:09 Olympic

gates of olympus slot - https://gatesofolympus.auction/ gates of olympus demo oyna

タイトル
名前
URL
コメント