0

我的脚本从我的 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
4

1 回答 1

0

为此,您需要一个递归函数。

尝试:

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
于 2021-06-25T09:24:41.990 回答