从通讯组列表中获取多个详细信息
Get multiple details from distributrion list
我是 PowerShell 的新手,但我想导出一些交换组详细信息并将这些详细信息导出到 csv 文件中。
以下是我要导出的详细信息:
- 群组名称
- 组的显示名称
- 组的主 Smtp 地址
- 组的收件人类型
- 群组成员数
- 组的所有者 (ManagedBy) - 有些有多个所有者
对于组的每个所有者,我还需要:
- 业主姓名
- 所有者显示名称
- 所有者电子邮件地址
我整理了以下脚本,但 Csv 完全空白,不能完全确定问题出在哪里。
$GroupsCollection=@()
$groups = Get-Group -ResultSize Unlimited
foreach ($group in $Groups) {
{
$GroupInformation = New-Object -typename PSObject
Add-Member -InputObject $DLInformation -MemberType NoteProperty -Name DLName -Value ""
Add-Member -InputObject $DLInformation -MemberType NoteProperty -Name DLDisplayName -Value ""
Add-Member -InputObject $DLInformation -MemberType NoteProperty -Name DLEmailAddress -Value ""
Add-Member -InputObject $DLInformation -MemberType NoteProperty -Name DLRecipientType -Value ""
Add-Member -InputObject $DLInformation -MemberType NoteProperty -Name DLMemberCount -Value ""
Add-Member -InputObject $DLInformation -MemberType NoteProperty -Name DLOwners -Value ""
Add-Member -InputObject $DLInformation -MemberType NoteProperty -Name DLOwnerName -Value ""
Add-Member -InputObject $DLInformation -MemberType NoteProperty -Name DLOwnerDisplayName -Value ""
Add-Member -InputObject $DLInformation -MemberType NoteProperty -Name DLOwnerEmailAddress -Value ""
$DLInformation.DLName = $group.Name
$DLInformation.DLDisplayName = $group.DisplayName
$DLInformation.DLEmailAddress =$group.PrimarySmtpAddress
$DLInformation.DLRecipientType = $group.RecipientType
$DLInformation.DLMemberCount = ($group.Members | measure).count
$GroupInformationOwners = ($group.DisplayName | select -ExpandProperty ManagedBy)
$DLInformation.DLOwners = $GroupInformationOwners |Out-String
$DLInformation.DLOwnerName = $GroupInformationOwners.name |out-string
$DLInformation.DLOwnerDisplayName = $GroupInformationOwners.displayname |out-string
$DLInformation.DLOwnerEmailAddress = $GroupInformationOwners.PrimarySmtpAddress |out-string
$GroupsCollection += $GroupInformation
}
}
$GroupsCollection |ConvertTo-Csv -NoTypeInformation
$GroupsCollection |Export-Csv -Path "file" -NoTypeInformation
这应该有助于提供更有利的结果。
$groups = Get-Group -ResultSize Unlimited
$GroupsCollection = foreach ($group in $Groups) {
$GroupInformation = [PSCustomObject]"" | Select-Object DLName,DLDisplayName,DLEmailAddress,DLRecipientType,DLMemberCount,DLOwners,DLOwnerName,DLOwnerDisplayName,DLOwnerEmailAddress
$GroupInformationOwners = $group.ManagedBy | Foreach-Object { Get-Group $_ }
$GroupInformation.DLName = $group.Name
$GroupInformation.DLDisplayName = $group.DisplayName
$GroupInformation.DLEmailAddress = $group.PrimarySmtpAddress
$GroupInformation.DLRecipientType = $group.RecipientType
$GroupInformation.DLMemberCount = ($group.Members | measure).count
$GroupInformation.DLOwners = $group.ManagedBy | Out-String
$GroupInformation.DLOwnerName = $GroupInformationOwners.name | Out-String
$GroupInformation.DLOwnerDisplayName = $GroupInformationOwners.displayname | Out-String
$GroupInformation.DLOwnerEmailAddress = $GroupInformationOwners.PrimarySmtpAddress | Out-String
$GroupInformation
}
$GroupsCollection |Export-Csv -Path "file" -NoTypeInformation
解释:
我删除了所有 Add-Member
以支持更简洁的 $GroupInformation
对象实例化。您的 Add-Member
-InputObject $DLInformation
值为空值,因为 $DLInformation
在执行这些命令时不存在。这将引发错误并导致没有属性被添加到您的对象。
$GroupInformationOwners
没有正确的初始值供您稍后使用。由于它仅包含 ManagedBy
字符串集合,因此您无法从中获取其他 Exchange 属性。相反,我选择在 ManagedBy
列表项上使用 Get-Group
并将输出存储到 $GroupInformationOwners
中。然后可以像您之前编码的那样检索剩余的属性。
我们不需要在这里使用 +=
,因为您可以设置可以将 foreach
循环的输出分配给变量。 $GroupsCollection
包含循环的输出,因为 $GroupInformation
仅在一行中。随着集合的增长,+=
通常是不好的做法,因为它读取一个数组对象,然后销毁该数组对象,最后输出一个新的数组对象,其数据比前一个对象多。这是一个比我们在这里所做的更低效和昂贵的操作。
您可以试试下面的代码。
Get-DistributionGroup -ResultSize Unlimited | Foreach{
$DisplayName=$_.DisplayName
$Alias=$_.Alias
$EmailAddress=$_.PrimarySmtpAddress
$GroupType=$_.GroupType
$ManagedBy=$_.ManagedBy
$Members=Get-DistributionGroupMember -ResultSize Unlimited -Identity $DisplayName
$MembersCount=($Members.name).Count
$Result=@{'DisplayName'=$DisplayName;'PrimarySmtpAddress'=$EmailAddress;'Alias'=$Alias;'GroupType'=$GroupType;'Manager'=$ManagedBy;$Members;'GroupMembersCount'=$MembersCount}
$Results= New-Object PSObject -Property $Result
$Results | Select-Object DisplayName,PrimarySmtpAddress,Alias,GroupType,Manager,Members,GroupMembersCount| Export-Csv -Path $ExportSummaryCSV -Notype -Append
}
更详细的信息可以参考this blog
我是 PowerShell 的新手,但我想导出一些交换组详细信息并将这些详细信息导出到 csv 文件中。
以下是我要导出的详细信息:
- 群组名称
- 组的显示名称
- 组的主 Smtp 地址
- 组的收件人类型
- 群组成员数
- 组的所有者 (ManagedBy) - 有些有多个所有者
对于组的每个所有者,我还需要: - 业主姓名 - 所有者显示名称 - 所有者电子邮件地址
我整理了以下脚本,但 Csv 完全空白,不能完全确定问题出在哪里。
$GroupsCollection=@()
$groups = Get-Group -ResultSize Unlimited
foreach ($group in $Groups) {
{
$GroupInformation = New-Object -typename PSObject
Add-Member -InputObject $DLInformation -MemberType NoteProperty -Name DLName -Value ""
Add-Member -InputObject $DLInformation -MemberType NoteProperty -Name DLDisplayName -Value ""
Add-Member -InputObject $DLInformation -MemberType NoteProperty -Name DLEmailAddress -Value ""
Add-Member -InputObject $DLInformation -MemberType NoteProperty -Name DLRecipientType -Value ""
Add-Member -InputObject $DLInformation -MemberType NoteProperty -Name DLMemberCount -Value ""
Add-Member -InputObject $DLInformation -MemberType NoteProperty -Name DLOwners -Value ""
Add-Member -InputObject $DLInformation -MemberType NoteProperty -Name DLOwnerName -Value ""
Add-Member -InputObject $DLInformation -MemberType NoteProperty -Name DLOwnerDisplayName -Value ""
Add-Member -InputObject $DLInformation -MemberType NoteProperty -Name DLOwnerEmailAddress -Value ""
$DLInformation.DLName = $group.Name
$DLInformation.DLDisplayName = $group.DisplayName
$DLInformation.DLEmailAddress =$group.PrimarySmtpAddress
$DLInformation.DLRecipientType = $group.RecipientType
$DLInformation.DLMemberCount = ($group.Members | measure).count
$GroupInformationOwners = ($group.DisplayName | select -ExpandProperty ManagedBy)
$DLInformation.DLOwners = $GroupInformationOwners |Out-String
$DLInformation.DLOwnerName = $GroupInformationOwners.name |out-string
$DLInformation.DLOwnerDisplayName = $GroupInformationOwners.displayname |out-string
$DLInformation.DLOwnerEmailAddress = $GroupInformationOwners.PrimarySmtpAddress |out-string
$GroupsCollection += $GroupInformation
}
}
$GroupsCollection |ConvertTo-Csv -NoTypeInformation
$GroupsCollection |Export-Csv -Path "file" -NoTypeInformation
这应该有助于提供更有利的结果。
$groups = Get-Group -ResultSize Unlimited
$GroupsCollection = foreach ($group in $Groups) {
$GroupInformation = [PSCustomObject]"" | Select-Object DLName,DLDisplayName,DLEmailAddress,DLRecipientType,DLMemberCount,DLOwners,DLOwnerName,DLOwnerDisplayName,DLOwnerEmailAddress
$GroupInformationOwners = $group.ManagedBy | Foreach-Object { Get-Group $_ }
$GroupInformation.DLName = $group.Name
$GroupInformation.DLDisplayName = $group.DisplayName
$GroupInformation.DLEmailAddress = $group.PrimarySmtpAddress
$GroupInformation.DLRecipientType = $group.RecipientType
$GroupInformation.DLMemberCount = ($group.Members | measure).count
$GroupInformation.DLOwners = $group.ManagedBy | Out-String
$GroupInformation.DLOwnerName = $GroupInformationOwners.name | Out-String
$GroupInformation.DLOwnerDisplayName = $GroupInformationOwners.displayname | Out-String
$GroupInformation.DLOwnerEmailAddress = $GroupInformationOwners.PrimarySmtpAddress | Out-String
$GroupInformation
}
$GroupsCollection |Export-Csv -Path "file" -NoTypeInformation
解释:
我删除了所有 Add-Member
以支持更简洁的 $GroupInformation
对象实例化。您的 Add-Member
-InputObject $DLInformation
值为空值,因为 $DLInformation
在执行这些命令时不存在。这将引发错误并导致没有属性被添加到您的对象。
$GroupInformationOwners
没有正确的初始值供您稍后使用。由于它仅包含 ManagedBy
字符串集合,因此您无法从中获取其他 Exchange 属性。相反,我选择在 ManagedBy
列表项上使用 Get-Group
并将输出存储到 $GroupInformationOwners
中。然后可以像您之前编码的那样检索剩余的属性。
我们不需要在这里使用 +=
,因为您可以设置可以将 foreach
循环的输出分配给变量。 $GroupsCollection
包含循环的输出,因为 $GroupInformation
仅在一行中。随着集合的增长,+=
通常是不好的做法,因为它读取一个数组对象,然后销毁该数组对象,最后输出一个新的数组对象,其数据比前一个对象多。这是一个比我们在这里所做的更低效和昂贵的操作。
您可以试试下面的代码。
Get-DistributionGroup -ResultSize Unlimited | Foreach{
$DisplayName=$_.DisplayName
$Alias=$_.Alias
$EmailAddress=$_.PrimarySmtpAddress
$GroupType=$_.GroupType
$ManagedBy=$_.ManagedBy
$Members=Get-DistributionGroupMember -ResultSize Unlimited -Identity $DisplayName
$MembersCount=($Members.name).Count
$Result=@{'DisplayName'=$DisplayName;'PrimarySmtpAddress'=$EmailAddress;'Alias'=$Alias;'GroupType'=$GroupType;'Manager'=$ManagedBy;$Members;'GroupMembersCount'=$MembersCount}
$Results= New-Object PSObject -Property $Result
$Results | Select-Object DisplayName,PrimarySmtpAddress,Alias,GroupType,Manager,Members,GroupMembersCount| Export-Csv -Path $ExportSummaryCSV -Notype -Append
}
更详细的信息可以参考this blog