2016-05-11 68 views
0

我試圖找出邏輯做這樣的事情:PowerShell命令AD用戶組成員比較基準

  • 查詢所有廣告組在特定的OU
  • 查詢所有用戶在一個特定的OU
  • 查詢該用戶的所有組成員
  • 如果任何用戶屬於在初始組查詢的一個或多個基團,輸出該信息
  • 如果任何用戶屬於沒有一個組中最初的組查詢,也輸出該信息

我在這個網站挖了一遍,發現一個腳本,大部分的作品,但我堅持我如何可以比較用戶的組成員身份原我正在拉的組查詢。看起來我可以使用compare-object cmdlet,但參數似乎沒有包含任何讓我能夠跟蹤兩個對象共有多少個組的內容。

我在網上找到的代碼如下:

$groups = Get-ADGroup -Filter * | where {$_.distinguishedname -like "*,OU=TUNE_TEST_GROUPS,OU=TUNE_TEST,DC=tune,DC=priv"} 
$users = Get-ADUser -Filter * | where {$_.distinguishedname -like "*,OU=TUNE_TEST_USERS,OU=TUNE_TEST,DC=tune,DC=priv"} 

foreach ($User in $Users) { 
    $userGroups = Get-ADPrincipalGroupMembership $User 
    if ($userGroups.Count -gt 1) { 
     "{0} is a member of the following {1} groups:" -f $User.SamAccountName, $userGroups.Count 
     foreach ($group in $userGroups) { 
      "`t{0}" -f $group.Name 
     } 
    } elseif ($userGroups.Count -lt 1) { 
     "{0} is a member of the following {1} groups:" -f $User.SamAccountName, $userGroups.Count 
     foreach ($group in $userGroups) { 
      "`t{0}" -f $group.Name 
     } 
     } 
} 

這樣做的問題是,我沒有在1號線我比較用戶組名的組查詢的名稱的方法也無法確定用戶屬於該列表中的一個或多個組。我不確定我是否可以使用相同的計數方法。

+0

實際上,我想我可能能夠只是通過他們與嵌套在一個if/else語句同時導出初始組和用戶查詢到CSV和環foreach循環。將這個嘗試並報告回去 –

回答

2

您可以驗證帳戶從您的參考列表的至少一個組的成員通過使用Compare-Object

foreach ($User in $Users) { 
    $userGroups = Get-ADPrincipalGroupMembership $User 
    if (!(Compare-Object $userGroups $groups -IncludeEqual -ExcludeDifferent)) { 
     "{0} doesn't belong to any reference group." -f $User.SamAccountName 
    } 
} 

邊注:使用的,而不是過濾的Get-ADUserGet-ADGroup的結果-SearchBase參數通過在專有名稱上的通配符匹配:

$groups = Get-ADGroup -Filter * -SearchBase 'OU=TUNE_TEST_GROUPS,OU=TUNE_TEST,DC=tune,DC=priv' -SearchScope Subtree 
$users = Get-ADUser -Filter * -SearchBase 'OU=TUNE_TEST_USERS,OU=TUNE_TEST,DC=tune,DC=priv' -SearchScope Subtree 
+0

不理會我以前的評論。這似乎運作良好。我只是扭轉了檢查,並添加+ $ userGroups.name追加它找到的每個匹配。考慮解決這個問題。 –

+0

奇怪的是它輸出列表中的「域用戶」組,即使該組不在參考列表中。 –

+0

實際上這隻能用於檢查兩個位置是否存在相同的組。如果我希望它實際輸出出現在兩個地方的組的名稱,它似乎不起作用。它只輸出用戶所屬的所有組。 –

0

我最終做了下面的事情這對我所需要的東西很有效。如果有人有興趣,示例代碼如下:

#gets a list of all groups in a given OU and stores the objects in the $groups variable 
$groups = Get-ADGroup -Filter * -SearchBase 'OU=TUNE_TEST_GROUPS,OU=TUNE_TEST,DC=tune,DC=priv' -Properties name | select name 

#pipe each group object into a foreach loop and output a string value of the same group name and stores it into the $groups_string variable 
$groups_string = $groups | % {$_.name} 

#gets a list of all users in a given OU and stores the objects in the $users variable 
$users = Get-ADUser -Filter * -SearchBase 'OU=TUNE_TEST_USERS,OU=TUNE_TEST,DC=tune,DC=priv' 


[email protected]{ 
"Username" = "" 
"Groupname" = "" 
} 

[email protected]() 

#iterates through every user in the $users variable and retrieves their group memberships 
foreach ($user in $users) { 
    #selects each group name and stores it in the $groupMembership variable 
    $groupMembership = Get-ADPrincipalGroupMembership $user | select name 

    #compares the names of each user's group to the baseline group name. 
    $groupMembership | foreach ($_) { 

     #If there is a match add the group name and the username to the $results hash table 
     if ($groups_string -contains $_.name) { 
      $results."Groupname" = $_.name 
      $results."Username" = $user.Name 

      #create a new PS object and supply the properties of the $results hash table to each object 
      $objresults = New-Object psobject -Property $results 

      #add each object to the $table array 
      $table += $objresults 
     } 
    } 

} 

#display/output the $table array and format it to fit 
$table | ft -AutoSize