簡體   English   中英

Powershell - 如果不是多個組的成員,則將 OU 中的用戶廣告到安全組

[英]Powershell - Ad user from OU to Security groups if not members of several groups

我正在編寫一個腳本來檢查來自特定 OU 的用戶是否不是組 1 或組 2 或組 3 或組 4 的成員。

我已經嘗試過這個,但有些用戶在他們不應該被列出的情況下被列出。

get-aduser -filter * -searchbase "$Ou" | where-object {((get-aduser $_.samaccountname -properties memberof).memberof -ne "$grp1") -or ((get-aduser $_.samaccountname -properties memberof).memberof -ne "grp2") -or ((get-aduser $_.samaccountname -properties memberof).memberof -ne "grp3") -or ((get-aduser $_.samaccountname -properties memberof).memberof -ne "grp4")} | Select SamAccountName

不確定我是否遵循,但聽起來你在要求這樣的事情:

$ou = 'OU=crowleytest,DC=contoso,DC=local'
$group1 = 'CN=group1,OU=crowleytest,DC=contoso,DC=local'
$group2 = 'CN=group2,OU=crowleytest,DC=contoso,DC=local'
$group3 = 'CN=group3,OU=crowleytest,DC=contoso,DC=local'
$group4 = 'CN=group4,OU=crowleytest,DC=contoso,DC=local'

$users = Get-ADUser -SearchBase $ou -Filter * -Properties memberof

$results = $users | where {
    $_.memberof -notcontains $group1 -and
    $_.memberof -notcontains $group2 -and
    $_.memberof -notcontains $group3 -and
    $_.memberof -notcontains $group4
}

$results

e - 也可以將此過濾器向左移動到-filter 參數中以獲得更好的性能,但這需要不同的語法。 如果您的用戶列表不是很大,那么上面的示例就足夠了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM