我正在處理將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的一部分,所以輸出的對象表示他們不屬於任何當它遍歷域用戶組時,主要組。
我玩過一些比較運算符的不同組合,但無法正常工作。任何建議,將不勝感激。
看起來簡單的路線是'if($ groupMembership.Count -le 1 -and $ groupMembership [0] .Name -ne「allDomainUsers」){#Do stuff; }其他{寫主機「$用戶沒有任何權利!」; }' –
@奧斯汀法國看起來不像。在我寫這篇文章之前,我嘗試了類似的東西,但它沒有輸出我想要的內容。我也試了一下你的建議,看起來和我做的事情一樣。它輸出與主查詢匹配的所有組,但也會顯示消息,說明TestUser01不是來自主組查詢的任何組的一部分,只要該組查詢的每個用戶都默認了Domain Users組。 –
另外,在您的代碼塊中,您使用$ groupMembership變量中的.count和[0] .name。在我的我有每個對象在變量管道通過一個for循環,所以我改變了語法if($ _。count -le 1和$ _ [0] .name -ne「域用戶」)...仍然和我的結果一樣。 –