如何将所有电子邮件分发中的所有用户的Active Directory列表保存到.CSV?

时间:2018-03-20 22:06:10

标签: powershell active-directory powershell-v4.0

我找到了这个例子,但我不确定如何将输出正确保存到.csv。

Import-Module ActiveDirectory

$Groups = Get-ADGroup -Filter {GroupCategory -eq "Distribution"} -Properties Members

ForEach ($g in $Groups) {
    Write-Host $g.name
    Write-Host $g.members `n
}

我尝试了以下内容:

Import-Module ActiveDirectory

$Groups = Get-ADGroup -Filter {GroupCategory -eq "Distribution"} -Properties Members

ForEach ($g in $Groups) {
    $g.name | Export-CSV C:\log.csv -notypeinformation -Append
    $g.members | Export-CSV C:\log.csv -notypeinformation -Append
}

它只将1列保存到CSV,称为长度。

这也让我删除了Write-Host $ g.members`n

末尾的'n'

有没有办法可以抓取这些数据并将其妥善保存到.csv?

更新

在TheMadTechnician的帮助下,https://blogs.technet.microsoft.com/heyscriptingguy/2013/07/22/export-user-names-and-proxy-addresses-to-csv-file/我能够更接近我想要的东西。

Import-Module ActiveDirectory

$Groups = Get-ADGroup -Filter {GroupCategory -eq "Distribution"} -Properties Members

ForEach ($g in $Groups) {
    $g.name | Export-CSV C:\log.csv -notypeinformation -Append
    $g.members | Export-CSV C:\log.csv -notypeinformation -Append
}
$Groups | Select Name,@{L='Members_1'; E={$_.members[0]}}, @{L='Members_2';E={$_.Members[1]}}, @{L='Members_3';E={$_.Members[2]}}, @{L='Members_4';E={$_.Members[3gq]}} | Export-Csv C:\log.csv -notype

这在我的CSV中为我提供了以下输出:

Name        Members_1                                     Members_2             ETC...                                                         

NameOfGroup CN=Stormy Daniels,OU=IT,DC=DomainName,DC=com  CN=Joe Bob,OU=IT,DC=DomainName,DC=com

现在用户列表可能很大,所以我必须继续创建Members_3,Members_4等...

我不确定是否有办法指定所有用户或循环

@{L='Members_1'; E={$_.members[0]}}

并递增数字,直到显示所有用户。

我也只需要带名字的CN。我不需要Ou =或Dc =。

1 个答案:

答案 0 :(得分:2)

啊这证明比我预期的更难 - 由于成员计数(你必须做一个可以与整数相比的计数)。我添加了一种限制结果大小的可能性,就像大型查询一样,活动目录会产生超时。

$limit_result_size = 10

$group_name = Get-ADGroup -Filter {GroupCategory -eq "Distribution"} -Properties Name, Members -ResultSetSize:$limit_result_size | Select-object name

ForEach ($name in $group_name.name) {
    If ((![String]::IsNullOrEmpty("$name")) -And ("$name" -notlike 'index')) {
        $count_members = Get-ADGroupMember -Identity "$name" | Measure-Object | Select-Object Count
        Write-Output "The AD group $name has $($count_members.Count) members.`n"
        For($counter = 0; $counter -lt $count_members.Count; $counter++) {
            $person = Get-ADGroup -Filter {Name -eq $name} -Properties Name, Members | Select-Object Name, @{N='Members';E={$_.Members[$counter]}}
            $person.Members = $person.Members | Select-String 'CN=[0-9a-zA-Z]+' -AllMatches | % { $_.Matches } | % { $_.Value }
            $person | export-csv -NoTypeInformation -Append -Path '<your_path>\log.csv'
        }
    }
}

简短说明:

AD小组应满足的

(![String]::IsNullOrEmpty("$name")) -And ("$name" -notlike 'index'))条件。

Select-String 'CN=[0-9a-zA-Z]+' -AllMatches | % { $_.Matches } | % { $_.Value }仅选择CN=string_with_numbers。如果您愿意,可以将其替换为CN=\w+

该脚本在CV AD groupCN=user_name中生成一对。如果还有其他不清楚的地方,请询问。

修改

如果 Common Names (CN)的名称中有空格,则必须将正则表达式调整为CN=[0-9a-zA-Z\s]+

编辑2 添加用户的电子邮件地址。

由于你的问题在电子邮件的标题请求中有,我将在这里回答,没有新的问题。请注意,此解决方案在regexp中使用lookbehind从输出中排除CN=,以便它可以用作用户查询的源。它还使用PSCustomObject将所有信息收集在一起。我已经重命名了一些变量,以便在用户详细信息的上下文中更好地理解。

$limit_result_size = 10

$group_name = Get-ADGroup -Filter {GroupCategory -eq "Distribution"} -Properties Name, Members -ResultSetSize:$limit_result_size | Select-object name

ForEach ($name in $group_name.name) {
    If ((![String]::IsNullOrEmpty("$name")) -And ("$name" -notlike 'index')) {
        $count_members = Get-ADGroupMember -Identity "$name" | Measure-Object | Select-Object Count
        Write-Output "The AD group $name has $($count_members.Count) members.`n"
        For($counter = 0; $counter -lt $count_members.Count; $counter++) {
            $person = Get-ADGroup -Filter {Name -eq $name} -Properties Name, Members | Select-Object Name, @{N='Members';E={$_.Members[$counter]}}
            $person.Members = $person.Members | Select-String '(?<=CN=)[0-9a-zA-Z\s]+' -AllMatches | % { $_.Matches } | % { $_.Value }
            $person_details = Get-AdUser -filter {name -eq $member} -Properties mail | Select-Object mail
            $person_additional_details = [PSCustomObject]@{ group_name = $group.Name
                                                            user_name  = $group.Members
                                                            email      = $person_details.mail
                                                      }
            If ([String]::IsNullOrEmpty($($person_additional_details.email))) {
                $person_additional_details.psobject.properties["email"].value = '<empty>'
            }

        # For user to see the written data
        Write-Output "AD Group: $($person_additional_details.group_name) `
AD User: $($person_additional_details.user_name) `
Users`'s email: $($person_additional_details.email)`n"

        # writing into the CSV file
        $person_additional_details | export-csv -NoTypeInformation -Append -Path '<your_path>\log.csv'
        }
    }
}