【问题标题】:Get multiple details from distributrion list从分发列表中获取多个详细信息
【发布时间】:2019-07-31 10:10:55
【问题描述】:

我是 PowerShell 的新手,但我想导出一些交换组详细信息并将这些详细信息导出到 csv 文件中。

以下是我要导出的详细信息:

  • 组名
  • 组的显示名称
  • 组的PrimarySmtpAddress
  • 组的收件人类型
  • 组的成员数
  • 组的所有者(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

【问题讨论】:

  • 您的脚本永远不会显示$DLInformation 是如何创建的。 Add-Member 不会喜欢 $null 的输入对象。那应该替换为$GroupInformation
  • 谢谢指出!缺少咖啡很可能导致我忽略了这一点 - 我已经修改了脚本,但重新运行后 csv 文件仍然是空白的?
  • 您还有第二组{},它有效地创建了一个未执行的脚本块。那些应该被删除。您的 $GroupInformationOwners 也不正确。检查我的回答,看看是否有帮助。

标签: powershell office365


【解决方案1】:

你可以试试下面的代码。

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

【讨论】:

  • 感谢分享!然而,许多 DL 有多个所有者,我需要获取多个所有者中每个所有者的用户详细信息 - 我得到的最接近的是下面的“单线”,它仅适用于只有一个所有者的 DL。 get-group -anr "DL 名称" |选择对象名称,显示名称,PrimarySmtpAddress,@{n="ManagedBy";e={get-group -anr $_.DisplayName | select -expandproperty ManagedBy}},@{n="ManagedBy 用户详细信息";e={get-group -anr $_.DisplayName |选择-expandproperty ManagedBy |获得收件人 |选择名称、显示名称、PrimarySmtpAddress} | Export-Csv -Path "File" -NoTypeInformation
【解决方案2】:

这应该有助于提供更有利的结果。

$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 仅在一行上。 += 通常是不好的做法,因为集合会增长,因为它会读取一个数组对象,然后销毁该数组对象,然后最终输出一个新的数组对象,其数据比前一个对象多。与我们在这里所做的相比,这是一种效率更低、成本更高的操作。

【讨论】:

  • 干杯 - 我们已经启动并运行了 CAC,我的地址还没有被列入白名单 - 所以当我回到办公室时会告诉你结果。
  • ManagedBy 是否包含用户或组?如果是用户,那么你应该改用get-aduser
  • ManageBy 包含用户 - 我使用了 get-recipient。
  • 非常感谢您的脚本,它完全按照我的设想运行,哈哈。我们的组织规模很大,wifi 连接可能比拨号更差 - 所以它花了大约 6 个小时来运行,但最终还是奏效了。我没想到太多,但你 10/10 对我很有帮助,干杯
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-11
  • 1970-01-01
  • 2022-08-02
  • 2012-06-02
  • 1970-01-01
  • 2022-08-19
相关资源
最近更新 更多