Get-AD___
には、ActiveDirectoryモジュールを入手するためのRSATツールが必要です。これは、@ Rohin Sidharthのコメントのように、エンドユーザーのワークステーションにとってはほとんど想定されていません。
@James C.さんの現在受け入れられている回答では、再帰的グループメンバーシップ(-Recursive
パラメータが必要です)は処理されませんが、両方のグループのすべてのメンバーをリストアップすることも含まれます。それは配列の追加の貧しい習慣を持っています。
@Baconビットの答えは「少ないデータを取得」が、まだ再帰的なグループメンバーシップを処理しませんし、まだのActiveDirectoryモジュールに依存しているために優れているユーザーのグループメンバーシップを取得します。
RSATを回避するには、ADSIのようなものを使用できます。これは、System.DirectoryServices.AccountManagementでラップされています。 Richard Siddawayによるhereについて議論しました。
壊れているように見えるユーザーのためのグループメンバーをリストアップするための良い方法がある - 類似した質問hereにテリーTsayのC#の答えからピンチを、Iこれにポートの彼のコードが、私は現在に焦点を当ててきました既定でユーザーと配布グループが含まれます。
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
Function IsUserInGroup([string] $groupName)
{
# Remove DOMAIN\ from the start of the groupName.
$groupName = $groupName -replace '^.*\\'
# Get an AD context for the current user's domain
$context = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList 'Domain', $ENV:USERDOMAIN
# Find the current user account in AD, and refresh the security and distribution groups
$user = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($context, 'SAMAccountName', $env:USERNAME)
$userEntry = [System.DirectoryServices.DirectoryEntry] $user.GetUnderlyingObject()
$userEntry.RefreshCache(@('tokenGroupsGlobalAndUniversal'))
# Get all the security and distribution groups the user belongs to, including nested memberships
$usersGroupSIDs = foreach ($sid in $userEntry.Properties.tokenGroupsGlobalAndUniversal.Value)
{
New-Object System.Security.Principal.SecurityIdentifier -ArgumentList $sid, 0
}
# Get the AD details for the group to test, and test membership
$group = [System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($context, 'SamAccountName', $groupName)
$usersGroupSIDs.Contains($group.Sid)
}
凝縮または単純な、しかしそれだけで.NET Frameworkを使用して、特にメンバーのグループが増加の数、および追加のモジュールにはあまり必要とオーバーヘッド接続以下ADとのより多くの条件を処理する必要がありますされていない
PS C:\> IsUserInGroup 'parent-nested-group-here'
True
。
私の他の答えはあなたが凝縮され、あなたのコードをしたいならば、「それをしない」ですが、その後、あなたは
$group2 = [System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($context, 'SamAccountName', $group2Name)
$usersGroupSIDs.Contains($group.Sid) -or $usersGroupSIDs.Contains($group2.Sid)
作業コードがオフトピックであり、作業コードの変更/提案のためのSEサイトがhttps://codereview.stackexchange.com/ – TessellatingHeckler
であるため、この質問を議論の対象外とすることにしました。エンドユーザーのコンピュータにADモジュールがインストールされていることを確認します。ログオンスクリプトとして設計する場合は、その問題にぶつかります。 –