2016-05-16 57 views
2

我正在處理將AD用戶的組成員身份與現有列表進行比較的腳本。我想要檢查的一件事是,如果用戶不屬於列表中的任何組,並且僅限於所有域用戶都屬於的默認「域用戶」組,則它應該輸出一條消息,如,「TestUser01不是任何組的成員」可能測試一個變量是否僅包含特定值?

是否有一個運算符可以檢查是否存在唯一的值?我的腳本可能有點奇怪,因爲它不是針對對象進行測試,而是對象值的字符串等效,所以這可能是我的問題所在。我不太確定。部分腳本輸出如下。我這樣做是因爲這是我可以比較組名的唯一方法。當我試圖比較object.name值,我不能讓它工作:

#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' 

#hash table containing the $table properties 
[email protected]{ 
"Username" = "" 
"Groupname" = "" 
} 

#empty array to store each user/group output object 
[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) { 

      $props."Groupname" = $_.name 
      $props."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 $props 

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


    }  
} 

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

我已經刪除了else語句,因爲這就是我目前試圖找出。現在,我只能在匹配時輸出用戶名和組名。我需要完成/弄清楚的是,使其他測試用戶不屬於除域用戶組以外的任何組,不會成爲任何主要組的一部分。在我的測試過程中,唯一一次我能夠做到這一點的時候,同樣的消息也會出現在我的其他測試用戶身上。這對於查看報告的人沒有任何意義,因爲這些測試用戶確實屬於主組列表中的組,但是因爲他們是Domain Users的一部分,所以輸出的對象表示他們不屬於任何當它遍歷域用戶組時,主要組。

我玩過一些比較運算符的不同組合,但無法正常工作。任何建議,將不勝感激。

+0

看起來簡單的路線是'if($ groupMembership.Count -le 1 -and $ groupMembership [0] .Name -ne「allDomainUsers」){#Do stuff; }其他{寫主機「$用戶沒有任何權利!」; }' –

+0

@奧斯汀法國看起來不像。在我寫這篇文章之前,我嘗試了類似的東西,但它沒有輸出我想要的內容。我也試了一下你的建議,看起來和我做的事情一樣。它輸出與主查詢匹配的所有組,但也會顯示消息,說明TestUser01不是來自主組查詢的任何組的一部分,只要該組查詢的每個用戶都默認了Domain Users組。 –

+0

另外,在您的代碼塊中,您使用$ groupMembership變量中的.count和[0] .name。在我的我有每個對象在變量管道通過一個for循環,所以我改變了語法if($ _。count -le 1和$ _ [0] .name -ne「域用戶」)...仍然和我的結果一樣。 –

回答

1

使用Compare-Object就像我告訴你,爲了響應你previous question,展開InputObject屬性來獲得可以在兩個數組相等(組)對象,然後展開對象的Name財產。

$common = Compare-Object $userGroups $groups -IncludeEqual -ExcludeDifferent | 
      Select-Object -Expand InputObject | 
      Select-Object -Expand Name 

現在你可以檢查結果只包含名稱Domain Users這樣的:

if ($common -contains 'Domain Users' -and $common.Count -eq 1) { 
    ... 
} else { 
    ... 
} 

或像這樣:

if ($common.GetType().Name -eq 'String' -and $common -eq 'Domain Users') { 
    ... 
} else { 
    ... 
} 

你也剛剛從選擇的第一個對象的名字清單:

$common = Compare-Object $userGroups $groups -IncludeEqual -ExcludeDifferent | 
      Select-Object -Expand InputObject | 
      Select-Object -First 1 -Expand Name 

if ($common -eq 'Domain Users') { 
    ... 
} else { 
    ... 
} 
0

謝謝Ansgar。我嘗試了你的建議的變體,我不能爲我需要的工作。我結束了我的混合原始腳本的部分與我上面張貼的腳本,並與下面的腳本就出來了:

#queries all groups in a specified OU 
$groups = Get-ADGroup -Filter * -SearchBase 'OU=TUNE_TEST_GROUPS,OU=TUNE_TEST,DC=tune,DC=priv' 

#iterates through each group object and outputs just the string value of the name property 
$groups_string = $groups | % {$_.name} 

#queries all users in a specified OU 
$users = Get-ADUser -Filter * -SearchBase 'OU=TUNE_TEST_USERS,OU=TUNE_TEST,DC=tune,DC=priv' 



foreach ($user in $users) { 
    #iterates through each user, find their group memberships, and outputs just the name property value 
    $userGroups = Get-ADPrincipalGroupMembership $user | select name 

    #if user belongs to more than one group, add following string 
    if ($userGroups.Count -gt 1){ 
     "{0} is a member of the following groups:" -f $user.SamAccountName 

     #iterates through each group, if group name matches group name in original query, output the matching group name 
     $userGroups | foreach ($_) { 
     if ($groups_string -contains $_.name){ 
      "`t{0}" -f $_.Name}   
     } 
    } 

    #if user belongs to 1 or lesser groups perform the next check 
    elseif ($userGroups.Count -le 1) { 

     #iterates through each group, if group name in check matches "Domain Users", output string saying user doesn't belong to any groups 
     $userGroups | foreach ($_) { 
     if ($_.name -contains "Domain Users") {  
      "{0} is not a member of any groups" -f $user.SamAccountName} 
     } 
    } 
} 

這其實很適合我的需要。我能夠通過將一些測試用戶添加到我查詢的基本OU中的不同組來測試。我可以確認它只輸出我關心的組,這些匹配基準查詢中的組。它還允許我輸出一條消息,指出用戶只屬於默認Domain Users組的一部分時不屬於任何基準組,並且還會將其他用戶從此支票中排除,這些用戶是域用戶默認情況下也屬於基線檢查組。爲感興趣的人發佈最終腳本。

相關問題