web-dev-qa-db-ja.com

CmdLetsを使用せずにPowerShellスクリプトを使用して、広告グループ内のユーザーを再帰的に一覧表示する

PowerShellでCmdLetsを使用せずに、ActiveDirectoryのセキュリティグループの全員を一覧表示しようとしています。私のスクリプトの奇妙な点は、ディレクトリ全体をリストすると機能することですが、LDAPクエリでリストしたいものを指定しようとすると、機能しません。別の同様のvbsで使用したことがあるので、LDAPクエリが正しいことはわかっています。コメントされた行は、私がクエリに入れようとした場所です。

$strFilter = "(&(objectCategory=person)(objectClass=user))"
#$strFilter = "(&(objectCategory=person)(objectClass=user)(memberOf=CN=Common Name,OU=User Groups,...,DC=ad,DC=domain,DC=com))" #... is just left out part of query

#$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://CN=Common Name,OU=User Groups,...,DC=ad,DC=domain,DC=com") #... is just left out part of query

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"

$colProplist = "name"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

$colResults = $objSearcher.FindAll()

foreach ($objResult in $colResults)
    {$objItem = $objResult.Properties; $objItem.name}
11
yoyomommy

これは、Active-Directory 2003SP2および2008R2で機能するものです。 ADSIとMicrosoftを使用しています LDAP_MATCHING_RULE_IN_CHAIN 。グループのすべてのユーザーを再帰的に(ただし1つのクエリで)検索します(セキュリティおよび配布グループからユーザーを返すように注意してください)

Clear-Host
$dn = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://WM2008R2ENT:389/dc=dom,dc=fr","[email protected]","PWD")

# To find all the users member of groups "MonGrpPlusSec"  : 
# Set the base to the groups container DN; for example root DN (dc=societe,dc=fr)  
# Set the scope to subtree 
# Use the following filter : 
# (member:1.2.840.113556.1.4.1941:=CN=MonGrpPlusSec,OU=ForUser1,DC=dom,DC=fr) 

$dsLookFor = new-object System.DirectoryServices.DirectorySearcher($dn)
$dsLookFor.Filter = "(&(memberof:1.2.840.113556.1.4.1941:=CN=MonGrpPlusSec,OU=ForUser1,DC=dom,DC=fr)(objectCategory=user))"; 
$dsLookFor.SearchScope = "subtree"; 
$n = $dsLookFor.PropertiesToLoad.Add("cn"); 
$n = $dsLookFor.PropertiesToLoad.Add("distinguishedName");
$n = $dsLookFor.PropertiesToLoad.Add("sAMAccountName");

$lstUsr = $dsLookFor.findall()
foreach ($usrTmp in $lstUsr) 
{
  Write-Host $usrTmp.Properties["samaccountname"]
}
9
JPBlanc

これにより、ネストされたメンバーを含む、ドメインAdministratorsグループのすべてのメンバーが取得されます(.NET 3.5が必要です)。

$Recurse = $true

Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$group=[System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($ct,'Administrators')
$group.GetMembers($Recurse)
8
Shay Levy

グループ名を知っている限り、次の(醜い)準ワンライナーを実行できます。

## List Members in a Group
$groupname = 'GroupNameHere'
(New-Object System.DirectoryServices.DirectoryEntry((New-Object System.DirectoryServices.DirectorySearcher("(&(objectCategory=Group)(name=$($groupname)))")).FindOne().GetDirectoryEntry().Path)).member | % { (New-Object System.DirectoryServices.DirectoryEntry("LDAP://"+$_)) } | Sort-Object sAMAccountName | SELECT @{name="User Name";expression={$_.Name}},@{name="User sAMAccountName";expression={$_.sAMAccountName}}

また、一方を他方なしで実行することはめったにないため、同じ基本的なアプローチを使用して、ユーザーのすべてのグループを一覧表示する方法も含めます。

## List Groups for a Username
$username = 'UsernameHere'
(New-Object System.DirectoryServices.DirectorySearcher("(&(objectCategory=User)(samAccountName=$($username)))")).FindOne().GetDirectoryEntry().memberOf | % { (New-Object System.DirectoryServices.DirectoryEntry("LDAP://"+$_)) } | Sort-Object sAMAccountName | SELECT @{name="Group Name";expression={$_.Name}},@{name="Group sAMAccountName";expression={$_.sAMAccountName}}

これらは両方とも現在のドメインを照会し、ドメイン資格を必要とせず、モジュールや追加のライブラリをインストールする必要もありません。また、ADを検索する必要がある最小限のアクセス許可で、かなりのバニラ環境で作業していることもあります。これら2つのコマンドは、それをかなり助けてくれます。

2
John Eisbrener