我正在尝试列出包含来自特定 OU 的成员的所有组。为此,我正在读取一个包含所有组名列表的文件,并使用 get-ADGroupMember $groupName 来查找每个组的成员。我将每个成员的值转换为字符串并与 $member.indexOf("specific OU") 进行比较。问题是,我还没有弄清楚如何获取 $member[$i].indexOf("specific OU")。我的代码如下。
编辑:如果我使用 for-each 循环,我可以正确地遍历成员,但我不能使用 break,它可以防止重复。
Import-Module ActiveDirectory
#declaring end result group array
$results = @()
#sets path for files
$pathName = $MyInvocation.MyCommand.Path
$pathLen = $pathName.LastIndexOf("\")
$pathStr = $PathName.Substring(0,$pathLen +1)
$APgroupFile = $pathStr + "APGroups.csv"
$groupFile = $pathStr + "GGroups.csv"
#gets the list of group names and loops through them
get-content $groupFile | foreach-object{
#sets all of the group names
#start of if1
if($_.IndexOf("CN=") -ge 0){
$nameStart = $_.IndexOf("CN=")+3
$nameEnd = $_.indexOf(",")-$nameStart
$name = $_.substring($nameStart, $nameEnd)
#issue starts here
#goal is to find member of "specific OU".
#If found, add group to $results. If not, go to next group
$members = get-ADGroupMember -Identity $name
if($members.length -gt 0){
$i=0
for($i=0; $i -le ($members.length -1); $i++){
#need to check users OU for specific OU. If a user is member, save group to .txt. If none are members, move to next group
$OU = $members.toString()
if($OU.indexOf("OU=specific OU") -ge 0){#start of if OU
$results += New-Object psObject -Property @{'GroupName'=$name; 'Member'=$OU}
break
}#end if OU
}#end for mem.length
}#end if mem.length
}#end if1
}#end foreach
$results | Export-Csv $APgroupFile -NoTypeInformation