【发布时间】:2017-04-19 00:46:47
【问题描述】:
我正在尝试编写一个脚本,如果用户不在特定 OU 中,则将其从安全组中删除。
我在比较来自 OU 的用户数组与来自安全组的用户数组时遇到问题。
为了测试,我遍历了 $testGroup 和 $userList 中的内容。两者看起来都与我相似,但很明显它们不能比较,因为仅输出 $userList -contains $user 会给我一堆 false 结果,即使它应该是真的。
$userList = @()
$testGroup = @()
#Get current members of group. Using this instead of get-adgroupmember due to speed
$testGroup = Get-AdGroup "testGroup" -properties member | select-object -ExpandProperty member | get-aduser
#Define OUs that we want to get members from
$OUlist = "OU1","OU2"
#Populate $userList with members of each OU
$OUlist | foreach {
$userList += get-aduser -filter {Enabled -eq $True} -SearchBase "OU=$_,DC=dc,DC=dc2,DC=dc3"
}
#Check the group for anyone no longer in one of the approved OUs
$testGroup | foreach {
if($userList -notcontains $user){
#remove the user from $testGroup
}
}
【问题讨论】:
-
不确定你是否知道,但是当你使用 $x | foreach,您需要开始使用管道。所以在这种情况下,个人用户是 $_(当前项目)。我不确定这里的 $user 是什么,但您可能需要用 $_ 替换它。
标签: powershell