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

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

目次

Blog 利用状況

書庫

日記カテゴリ

DirectoryAccess.GetMembers メソッド

DirectoryAccess クラスに追加した GetMembers メソッドのコードです。(これまでのコードはこちら VB C#

VB

'指定したドメイングループのメンバーを取得します。

Public Shared Function GetMembers(group As DomainGroup) As IList(Of DomainObject)

  If group Is Nothing Then

    Throw New ArgumentNullException("group", "group が Nothing です。")

  End If

  If CanConnectDomain = False Then  'ドメインに接続できない時

    Return New List(Of DomainObject)()

  End If

 

  Dim objects As New List(Of DomainObject)()

  Using root = GetRootEntry()   'ルートのDirectoryEntryを取得

    'このグループのメンバーを検索

    Dim filter = String.Format("(memberOf={0})", group.Entry.Properties.Item("distinguishedName").Value)

    Using searcher As New DirectorySearcher(root, filter)

      Using results = searcher.FindAll()

        For Each res As SearchResult In results

          objects.Add(DirectCast(CreateInstance(res.GetDirectoryEntry()), DomainObject))

        Next

      End Using

      'このグループをプライマリ グループとしているメンバーを検索

      searcher.Filter = String.Format("(&(|(objectCategory={0})(objectCategory={1}))(primaryGroupID={2}))",

        CategoryType.User, CategoryType.Computer, group.Token)

      Using results = searcher.FindAll()

        For Each res As SearchResult In results

          objects.Add(DirectCast(CreateInstance(res.GetDirectoryEntry()), DomainObject))

        Next

      End Using

    End Using

  End Using

  Return objects.OrderBy(Function(o) o.ToString()).ToList()

End Function

 

C#

//指定したドメイングループのメンバーを取得します。

public static IList<DomainObject> GetMembers(DomainGroup group)

{

  if (group == null)

  {

    throw new ArgumentNullException("group", "group が null です。");

  }

  if (CanConnectDomain == false//ドメインに接続できない時

  {

    return new List<DomainObject>();

  }

 

  var objects = new List<DomainObject>();

  using (var root = GetRootEntry())   //ルートのDirectoryEntryを取得

  {

    //このグループのメンバーを検索

    var filter = String.Format("(memberOf={0})", group.Entry.Properties["distinguishedName"].Value);

    using (var searcher = new DirectorySearcher(root, filter))

    {

      using (var results = searcher.FindAll())

      {

        foreach (SearchResult res in results)

        {

          objects.Add((DomainObject)CreateInstance(res.GetDirectoryEntry()));

        }

      }

      //このグループをプライマリ グループとしているメンバーを検索

      searcher.Filter = String.Format("(&(|(objectCategory={0})(objectCategory={1}))(primaryGroupID={2}))",

        CategoryType.User, CategoryType.Computer, group.Token);

      using (var results = searcher.FindAll())

      {

        foreach (SearchResult res in results)

        {

          objects.Add((DomainObject)CreateInstance(res.GetDirectoryEntry()));

        }

      }

    }

  }

  return objects.OrderBy(o => o.ToString()).ToList();

}

 

1 回目の検索はグループのメンバーの検索で、所属するグループがこのグループであるオブジェクトを検索してます。

プライマリ グループを除いて 所属するグループは memberOf 属性で取得できるので、この値がグループの distinguishedName 属性(識別名)の値に一致するものがあれば、メンバーはこのグループに所属していることになります。

例えば Enterprise Admins グループの distinguishedName 属性の値は次のようになってます。

 CN=Enterprise Admins,CN=Users,DC=proceed,DC=pbyk,DC=com

また Administrator の memberOf 属性の値は次のようになってます。

 CN=Group Policy Creator Owners,CN=Users,DC=proceed,DC=pbyk,DC=com

 CN=Domain Admins,CN=Users,DC=proceed,DC=pbyk,DC=com

 CN=Enterprise Admins,CN=Users,DC=proceed,DC=pbyk,DC=com

 CN=Schema Admins,CN=Users,DC=proceed,DC=pbyk,DC=com

 CN=Administrators,CN=Builtin,DC=proceed,DC=pbyk,DC=com

memberOf 属性も distinguishedName 属性も値の書式が同じなので、これを検索条件として使ってます。

 

2 回目の検索はこのグループをプライマリ グループとしているユーザーかコンピューターを検索してます。

既定では、プライマリ グループは次のようになります。

 ユーザー:Domain Users

 ドメイン コントローラー:Domain Controllers

 ドメイン コントローラー以外のコンピューター:Domain Computers

ユーザーかコンピューターの primaryGroupID 属性の値が グループの primaryGroupToken 属性の値(Token プロパティに保持)と一致すれば このグループに所属していることになります。

 

抽出されたメンバーは表示する名前でソートしてます。

 

内部で呼び出している CreateInstance メソッドを変更しました。太字部分の 1 行だけです。

VB

Private Shared Function CreateInstance(entry As DirectoryEntry) As DirectoryObject

  Dim category As CategoryType

  If [Enum].TryParse(Of CategoryType)(entry.SchemaClassName, True, category) = False Then

    Throw New ArgumentException("entry の種類が CategoryType に該当しません。", "entry")

  End If

 

  Select Case category

    Case CategoryType.User

      If CanConnectDomain Then    'ドメインに接続できる時

        Return New DomainUser(entry)

      Else    'ドメインに接続できない時

        Return New LocalUser(entry)

      End If

    Case CategoryType.Group

      If CanConnectDomain Then    'ドメインに接続できる時

        Return New DomainGroup(entry)

      Else    'ドメインに接続できない時

        Return New LocalGroup(entry)

      End If

    Case CategoryType.Computer

      Return New Computer(entry)

    Case CategoryType.PrintQueue

      Return New PrintQueue(entry)

    Case CategoryType.Volume

      Return New Volume(entry)

    Case Else

      Return New ForeignSecurityPrincipal(entry)

  End Select

End Function

 

C#

private static DirectoryObject CreateInstance(DirectoryEntry entry)

{

  CategoryType category;

  if (Enum.TryParse<CategoryType>(entry.SchemaClassName, true, out category) == false)

  {

    throw new ArgumentException("entry の種類が CategoryType に該当しません。", "entry");

  }

 

  switch (category)

  {

    case CategoryType.User:

      if (CanConnectDomain)    //ドメインに接続できる時

      {

        return new DomainUser(entry);

      }

      else    //ドメインに接続できない時

      {

        return new LocalUser(entry);

      }

    case CategoryType.Group:

      if (CanConnectDomain)    //ドメインに接続できる時

      {

        return new DomainGroup(entry);

      }

      else    //ドメインに接続できない時

      {

        return new LocalGroup(entry);

      }

    case CategoryType.Computer:

      return new Computer(entry);

    case CategoryType.PrintQueue:

      return new PrintQueue(entry);

    case CategoryType.Volume:

      return new Volume(entry);

    default:

      return new ForeignSecurityPrincipal(entry);

  }

}

 

 

Active Directory 関連 Blog

http://www.pbyk.com/blog/bloglist.html

投稿日時 : 2015年2月15日 14:05

コメントを追加

# buy ivermectin for humans uk http://stromectolabc.com/
cost of ivermectin 1% cream 2022/02/08 9:28 Busjdhj

buy ivermectin for humans uk http://stromectolabc.com/
cost of ivermectin 1% cream

# ivermectin usa price http://stromectolabc.com/
ivermectin purchase 2022/02/08 16:32 Busjdhj

ivermectin usa price http://stromectolabc.com/
ivermectin purchase

# doxycycline 100mg online https://doxycyline1st.com/
doxycycline generic 2022/02/26 8:15 Doxycycline

doxycycline 100mg online https://doxycyline1st.com/
doxycycline generic

# cheap doxycycline online https://doxycyline1st.com/
doxycycline 100mg capsules 2022/02/26 19:56 Doxycycline

cheap doxycycline online https://doxycyline1st.com/
doxycycline 100mg capsules

# buy prednisone online fast shipping https://prednisone20mg.icu/ 2022/10/15 13:16 Prednisone

buy prednisone online fast shipping https://prednisone20mg.icu/

# dating website https://datingtopreview.com/
singles dating 2022/10/17 20:33 Dating

dating website https://datingtopreview.com/
singles dating

# interracial dating site https://topdatingsites.fun/
local women dates 2022/11/15 0:22 DatingTop

interracial dating site https://topdatingsites.fun/
local women dates

# prednisone 40 mg rx https://prednisonepills.site/
compare prednisone prices 2022/11/28 23:46 Prednisone

prednisone 40 mg rx https://prednisonepills.site/
compare prednisone prices

# singles ads https://datingonlinehot.com/
dating chat free 2022/12/09 19:14 Dating

singles ads https://datingonlinehot.com/
dating chat free

# meds canada https://noprescriptioncanada.com/
canadian pharmacies list 2022/12/13 20:50 CanadaPh

meds canada https://noprescriptioncanada.com/
canadian pharmacies list

# mens ed pills https://edpills.science/
top rated ed pills 2023/01/07 13:45 EdPills

mens ed pills https://edpills.science/
top rated ed pills

# ourtime dating site https://datingonline1st.com/
tinder web 2023/01/17 22:26 Dating

ourtime dating site https://datingonline1st.com/
tinder web

# doxycycline pills - https://doxycyclinesale.pro/# 2023/04/22 3:58 Doxycycline

doxycycline pills - https://doxycyclinesale.pro/#

# prednisone 10mg tablets - https://prednisonesale.pro/# 2023/04/22 15:04 Prednisone

prednisone 10mg tablets - https://prednisonesale.pro/#

# buy cytotec online fast delivery - https://cytotecsale.pro/# 2023/04/28 23:26 Cytotec

buy cytotec online fast delivery - https://cytotecsale.pro/#

# over the counter health and wellness products https://overthecounter.pro/# 2023/05/08 18:11 OtcJikoliuj

over the counter health and wellness products https://overthecounter.pro/#

# medications without prescription https://pillswithoutprescription.pro/# 2023/05/14 22:08 PillsPresc

medications without prescription https://pillswithoutprescription.pro/#

# the best ed pill: https://edpills.pro/# 2023/05/16 3:13 EdPillsPro

the best ed pill: https://edpills.pro/#

# overseas pharmacies that deliver to usa https://pillswithoutprescription.pro/# 2023/05/16 4:41 PillsPro

overseas pharmacies that deliver to usa https://pillswithoutprescription.pro/#

# where can i buy prednisone without prescription https://prednisonepills.pro/# - prednisone 20 2023/06/05 5:14 Prednisone

where can i buy prednisone without prescription https://prednisonepills.pro/# - prednisone 20

# paxlovid cost without insurance https://paxlovid.store/
buy paxlovid online 2023/07/13 21:38 Paxlovid

paxlovid cost without insurance https://paxlovid.store/
buy paxlovid online

# Abortion pills online https://cytotec.ink/# - order cytotec online 2023/07/27 1:08 PillsFree

Abortion pills online https://cytotec.ink/# - order cytotec online

# online apotheke preisvergleich 2023/09/26 14:40 Williamreomo

http://onlineapotheke.tech/# internet apotheke
online apotheke preisvergleich

# п»їonline apotheke 2023/09/27 0:00 Williamreomo

https://onlineapotheke.tech/# online apotheke gГ?nstig
п»?online apotheke

# online apotheke gГјnstig 2023/09/27 1:57 Williamreomo

http://onlineapotheke.tech/# gГ?nstige online apotheke
internet apotheke

# online apotheke deutschland 2023/09/27 3:48 Williamreomo

https://onlineapotheke.tech/# п»?online apotheke
internet apotheke

# п»їonline apotheke 2023/09/27 4:16 Williamreomo

http://onlineapotheke.tech/# versandapotheke deutschland
gГ?nstige online apotheke

# п»їonline apotheke 2023/09/27 7:41 Williamreomo

http://onlineapotheke.tech/# online apotheke gГ?nstig
versandapotheke deutschland

# gГјnstige online apotheke 2023/09/27 9:41 Williamreomo

http://onlineapotheke.tech/# gГ?nstige online apotheke
gГ?nstige online apotheke

# п»їonline apotheke 2023/09/27 10:29 Williamreomo

https://onlineapotheke.tech/# versandapotheke
online apotheke preisvergleich

# п»їonline apotheke 2023/09/27 12:29 Williamreomo

http://onlineapotheke.tech/# online apotheke deutschland
online apotheke preisvergleich

# farmacia online piГ№ conveniente 2023/09/27 17:03 Rickeyrof

acheter sildenafil 100mg sans ordonnance

# comprare farmaci online all'estero 2023/09/27 21:04 Rickeyrof

acheter sildenafil 100mg sans ordonnance

# farmacia online senza ricetta 2023/09/27 22:42 Rickeyrof

acheter sildenafil 100mg sans ordonnance

# certified canadian pharmacies 2023/10/16 14:55 Dannyhealm

A harmonious blend of local care and global expertise. http://mexicanpharmonline.shop/# mexico drug stores pharmacies

# canadian oharmacy 2023/10/16 17:27 Dannyhealm

The staff ensures a seamless experience every time. https://mexicanpharmonline.shop/# pharmacies in mexico that ship to usa

# canadian pharnacy 2023/10/16 18:48 Dannyhealm

The best place for health consultations. http://mexicanpharmonline.shop/# mexico drug stores pharmacies

# online doctor prescription canada 2023/10/16 22:44 Dannyhealm

Always ahead of the curve with global healthcare trends. http://mexicanpharmonline.shop/# reputable mexican pharmacies online

# rx mexico online 2023/10/16 23:37 Dannyhealm

They offer invaluable advice on health maintenance. https://mexicanpharmonline.com/# pharmacies in mexico that ship to usa

# canada prescription online 2023/10/16 23:56 Dannyhealm

Their international shipment tracking system is top-notch. http://mexicanpharmonline.com/# mexico drug stores pharmacies

# canada meds com 2023/10/17 1:20 Dannyhealm

Bridging continents with their top-notch service. https://mexicanpharmonline.com/# mexican border pharmacies shipping to usa

# rx canada 2023/10/17 12:58 Dannyhealm

Efficient, effective, and always eager to assist. https://mexicanpharmonline.com/# mexican mail order pharmacies

# prescription online canada 2023/10/17 14:06 Dannyhealm

Leading with integrity on the international front. https://mexicanpharmonline.com/# mexico drug stores pharmacies

# canada pharm 2023/10/17 18:02 Dannyhealm

Generic Name. http://mexicanpharmonline.com/# mexico drug stores pharmacies

# no perscription needed 2023/10/18 1:25 Dannyhealm

Efficient, effective, and always eager to assist. https://mexicanpharmonline.shop/# mexican rx online

# canadian online rx 2023/10/18 4:47 Dannyhealm

п»?Exceptional service every time! http://mexicanpharmonline.shop/# mexican border pharmacies shipping to usa

# mail order prescriptions from canada 2023/10/18 10:03 Dannyhealm

Their global reach is unmatched. https://mexicanpharmonline.com/# mexican rx online

# no prescription needed 2023/10/18 17:37 Dannyhealm

The best in town, without a doubt. http://mexicanpharmonline.com/# mexico drug stores pharmacies

# Plavix generic price https://plavix.guru/ Clopidogrel 75 MG price 2023/10/23 21:13 Plavixxx

Plavix generic price https://plavix.guru/ Clopidogrel 75 MG price

# valtrex costs canada https://valtrex.auction/ buy valtrex cheap online 2023/10/24 17:41 Valtrex

valtrex costs canada https://valtrex.auction/ buy valtrex cheap online

# doxycycline hyc 100mg https://doxycycline.forum/ generic doxycycline 2023/11/25 9:04 Doxycycline

doxycycline hyc 100mg https://doxycycline.forum/ generic doxycycline

# paxlovid covid 2023/12/01 2:24 Mathewhip

paxlovid generic https://paxlovid.club/# paxlovid

# comprare farmaci online all'estero https://farmaciait.pro/ farmacie online sicure 2023/12/04 7:04 Farmacia

comprare farmaci online all'estero https://farmaciait.pro/ farmacie online sicure

# farmacias online seguras 2023/12/07 15:03 RonnieCag

http://farmacia.best/# farmacias online seguras

# farmacia envíos internacionales 2023/12/08 0:29 RonnieCag

https://farmacia.best/# farmacia online barata

# ï»¿farmacia online 2023/12/08 3:37 RonnieCag

https://vardenafilo.icu/# farmacia envíos internacionales

# farmacia online madrid 2023/12/08 9:27 RonnieCag

http://tadalafilo.pro/# farmacia online envío gratis

# farmacia barata 2023/12/08 15:04 RonnieCag

http://farmacia.best/# farmacia 24h

# farmacia online envío gratis 2023/12/09 12:44 RonnieCag

http://vardenafilo.icu/# farmacia barata

# farmacias online baratas 2023/12/09 22:19 RonnieCag

http://farmacia.best/# farmacia online madrid

# ï»¿farmacia online 2023/12/10 1:37 RonnieCag

http://vardenafilo.icu/# farmacia online barata

# farmacias baratas online envío gratis 2023/12/10 5:33 RonnieCag

http://tadalafilo.pro/# farmacia envíos internacionales

# farmacia online envío gratis 2023/12/10 11:48 RonnieCag

https://farmacia.best/# farmacia online 24 horas

# farmacias baratas online envío gratis 2023/12/11 17:00 RonnieCag

http://tadalafilo.pro/# farmacias online seguras en españa

# ï»¿farmacia online 2023/12/11 20:31 RonnieCag

http://vardenafilo.icu/# farmacia online envío gratis

# farmacias baratas online envío gratis 2023/12/12 3:31 RonnieCag

https://tadalafilo.pro/# farmacias online seguras

# farmacias online seguras en españa 2023/12/12 15:53 RonnieCag

http://vardenafilo.icu/# farmacias online seguras en españa

# farmacia online envío gratis 2023/12/12 22:46 RonnieCag

http://vardenafilo.icu/# farmacia barata

# farmacias baratas online envío gratis 2023/12/13 2:19 RonnieCag

https://sildenafilo.store/# se puede comprar sildenafil sin receta

# pharmacie ouverte 24/24 2023/12/14 12:23 Larryedump

http://pharmacieenligne.guru/# Pharmacie en ligne France

# Pharmacie en ligne sans ordonnance 2023/12/15 19:19 Larryedump

https://pharmacieenligne.guru/# Pharmacie en ligne livraison gratuite

# cheap erectile dysfunction pill https://edpills.tech/# non prescription ed pills 2023/12/23 4:38 EdPills

cheap erectile dysfunction pill https://edpills.tech/# non prescription ed pills

# prednisone cost us https://prednisone.bid/ fast shipping prednisone 2023/12/27 6:51 Prednisone

prednisone cost us https://prednisone.bid/ fast shipping prednisone

# vibramycin 100 mg 2024/01/04 23:40 BobbyHef

https://doxycyclinebestprice.pro/# how to order doxycycline

# cytotec pills buy online 2024/01/13 23:17 Keithturse

https://furosemide.pro/# furosemida 40 mg

# Misoprostol 200 mg buy online 2024/01/15 16:25 Keithturse

http://misoprostol.shop/# buy cytotec pills online cheap

# migliori farmacie online 2023 2024/01/15 20:22 Robertopramy

http://avanafilitalia.online/# farmacia online miglior prezzo

# farmacie online autorizzate elenco 2024/01/16 5:14 Wendellglaks

http://farmaciaitalia.store/# farmacia online più conveniente

# farmacie on line spedizione gratuita 2024/01/16 10:24 Wendellglaks

https://avanafilitalia.online/# acquisto farmaci con ricetta

# farmacia online migliore 2024/01/17 3:36 Robertopramy

http://avanafilitalia.online/# farmacia online senza ricetta

# how can i get clomid tablets 2024/01/20 15:34 AnthonyAnoth

https://prednisonepharm.store/# prednisone 60 mg tablet

# buying clomid prices 2024/01/20 21:47 LarryVoP

Their private consultation rooms are a great addition http://cytotec.directory/# buy cytotec online fast delivery

# tamoxifen hormone therapy 2024/01/21 9:24 Normantug

http://prednisonepharm.store/# can you buy prednisone over the counter in mexico

# tamoxifen dosage 2024/01/21 21:16 Normantug

https://prednisonepharm.store/# prednisone 20 mg purchase

# how to get cheap clomid price 2024/01/22 5:40 LarryVoP

A trusted partner in my healthcare journey http://prednisonepharm.store/# prednisone 2.5 mg price

# ivermectin nz 2024/01/30 23:34 Andrewamabs

https://prednisonetablets.shop/# over the counter prednisone pills

# zestril 5mg price in india 2024/02/20 20:10 Charlesmax

http://furosemide.guru/# furosemide 100 mg

# zestril 10mg price 2024/02/24 0:25 Charlesmax

https://stromectol.fun/# ivermectin 3 mg dose

# indian pharmacies safe 2024/03/01 6:59 JordanCrils

http://cytotec24.shop/# Cytotec 200mcg price

# pof dating app 2024/03/04 8:37 RodrigoGrany

https://sweetiefox.online/# sweeti fox

# dating seiten in schweiz 2024/03/04 22:40 Thomasjax

http://angelawhite.pro/# Angela White

# totally free chat dating site 2024/03/05 16:29 RodrigoGrany

https://evaelfie.pro/# eva elfie

# dating flirt site free 2024/03/06 8:58 Thomasjax

https://angelawhite.pro/# ?????? ????

# single dating sites 2024/03/10 8:23 HowardBox

best internet dating service: https://miamalkova.life/# mia malkova full video

# jogo de aposta 2024/03/14 7:38 BrianTop

https://jogodeaposta.fun/# melhor jogo de aposta para ganhar dinheiro

# mexican pharmaceuticals online 2024/03/18 10:59 ManuelMap

https://mexicanpharm24.shop/# mexican mail order pharmacies mexicanpharm.shop

# gates of olympus demo oyna 2024/03/28 19:21 KeithNaf

http://aviatoroyna.bid/# aviator hile

# how to buy clomid 2024/04/03 19:39 Robertsuela

https://clomidall.shop/# clomid prices

# where buy cheap clomid without insurance 2024/04/04 6:04 Robertsuela

https://clomidall.shop/# cost of cheap clomid pills

# where can i get clomid now 2024/04/04 10:16 Robertsuela

http://prednisoneall.shop/# 3000mg prednisone

# can you get clomid online 2024/04/04 14:22 Robertsuela

http://prednisoneall.shop/# prednisone without prescription 10mg

# cost generic clomid without insurance 2024/04/05 12:53 Robertsuela

https://prednisoneall.shop/# prednisone 2.5 mg cost

タイトル
名前
URL
コメント