[英]Successfully getting DL members-but how to check for and to list nested groups and members?
My script pulls members from each DL listed in my txt file, but there's a few that have security groups as members and I would like to list out the members in those groups as well.我的脚本从我的 txt 文件中列出的每个 DL 中提取成员,但有一些将安全组作为成员,我也想列出这些组中的成员。
set-adserversetting -viewentireforest $true
$groups = Get-Content -path 'C:\Users\test.txt'
$result =
foreach ($group in $groups) {
$members = Get-DistributionGroupMember -$group -ResultSize Unlimited
foreach ($user in $members) {
[PSCustomObject]@{
GroupName = $group
samAccountName = $user.samAccountName
distinguishedName = $user.distinguishedName
name = $user.name
}
}
}
$result | export-csv 'C:\Users\testing.csv' -notypeinformation
You need a recursive function for this.为此,您需要一个递归函数。
Try:尝试:
function Get-NestedDistributionGroupMembers {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string]$Group
)
# Identity can be Name, Alias, DistinguishedName, CanonicalName, EmailAddress or GUID
$searchGroup = Get-DistributionGroupMember -Identity $Group -ResultSize Unlimited
foreach ($member in $searchGroup) {
if ($member.RecipientTypeDetails.Value -match "Group") {
Get-NestedDistributionGroupMembers $member.DistinguishedName
}
else {
[PSCustomObject]@{
GroupName = $searchGroup.DisplayName
SamAccountName = $member.samAccountName
DistinguishedName = $member.distinguishedName
Name = $member.name
}
}
}
}
$groups = Get-Content -Path 'C:\Users\test.txt'
$result = foreach ($group in $groups) {
Get-NestedDistributionGroupMembers $group
}
$result | Export-Csv -Path 'C:\Users\testing.csv' -NoTypeInformation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.