我需要将以下内容导出到csv
或excel
文件中
-Distribution Group (PrimarySmtpAddress)
-Distribution Group members and each primarysmtpaddress
我尝试添加
Group email: $($group) | PrimarySmtpAddress
在下面的代码中,但没有添加。
#This is what works but is missing
$groups = Get-DistributionGroup -ResultSize Unlimited | Select -ExpandProperty name
ForEach ($group in $groups)
{
"Group Name: $($group)`nGroup Members:`n"
Get-DistributionGroupMember $group |ft name,alias,primarysmtpaddress
}
我缺少通讯组的主要smtp地址吗?
答案 0 :(得分:0)
正如Lee_Daily所评论的那样,您通过执行Name
除去了Select -ExpandProperty name
以外的所有属性。接下来,如果要导出到CSV文件,请不要使用Format-Table
(ft
),因为那只是将结果格式化到控制台。
您应该做的是创建一个对象数组,并将其传递到Export-Csv
cmdlet,如下面的(未经测试的)代码所示:
$outputFile = '<PATH AND FILENAME FOR THE EXPORTED CSV FILE>'
Get-DistributionGroup -ResultSize Unlimited | ForEach-Object {
# The Identity parameter for Get-DistributionGroupMember specifies the distribution group
# or mail-enabled security group. You can use any value that uniquely identifies the group.
# The cmdlet also accepts the Identity parameter as pipeline input, so
# $_ | Get-DistributionGroupMember will also work.
$Members = Get-DistributionGroupMember -Identity $($_.PrimarySmtpAddress)
foreach ($member in $Members) {
[PSCustomObject]@{
GroupName = $_.DisplayName
GroupAlias = $_.Alias
GroupEmail = $_.PrimarySMTPAddress
MemberName = $member.DisplayName
MemberEmail = $member.PrimarySMTPAddress
# Maybe also add RecipientType to distinguish between users and groups?
# MemberType = $member.RecipientType
}
}
} | Export-Csv -Path $outputFile -NoTypeInformation
希望有帮助