Powershell 从distributrion列表中获取多个详细信息
我是PowerShell的新手,但我想导出许多exchange组详细信息,并将这些详细信息导出到csv文件中 以下是我要导出的详细信息:Powershell 从distributrion列表中获取多个详细信息,powershell,office365,Powershell,Office365,我是PowerShell的新手,但我想导出许多exchange组详细信息,并将这些详细信息导出到csv文件中 以下是我要导出的详细信息: 团体名称 显示组的名称 组的PrimarySmtpAddress 接受者群体类型 组的成员计数 组的所有者(ManagedBy)-某些组有多个所有者 对于集团的每位所有者,我还需要: -所有者名称 -所有者显示名 -所有者电子邮件地址 我已经整理了下面的脚本,但是Csv是完全空白的,并且不完全确定问题所在 $GroupsCollection=@() $g
- 团体名称
- 显示组的名称
- 组的PrimarySmtpAddress
- 接受者群体类型
- 组的成员计数
- 组的所有者(ManagedBy)-某些组有多个所有者
$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
说明:
我删除了所有的addmember
,以便更简洁地实例化$GroupInformation
对象。您的添加成员-InputObject$DLInformation
值为空值,因为执行这些命令时,$DLInformation
不存在。这将抛出一个错误并导致没有属性被添加到对象中
$GroupInformationOwners
没有正确的初始值供您以后使用。由于它只包含由
字符串集合管理的,因此您无法从中获取其他Exchange属性。相反,我选择在ManagedBy
列表项上使用Get Group
,并将输出存储到$GroupInformationOwners
中。然后,剩余的属性可以像您之前编码的那样进行检索
我们不需要在这里使用+=
,因为您可以将foreach
循环的输出设置为一个变量$groupscolection
包含循环的输出,因为$GroupInformation
仅在一行上<代码>+=
通常是不好的做法,因为集合会增长,因为它读取一个数组对象,然后销毁该数组对象,最后输出一个新的数组对象,其中包含的数据比前一个对象多。这是一个比我们在这里所做的更低效、更昂贵的操作。您可以尝试下面的代码。
有关详细信息,请参阅您的脚本从未显示如何创建$DLInformation
addmember
不喜欢$null
的输入对象。应该用$GroupInformation
替换。感谢您指出这一点!缺少咖啡很可能导致我忽略了这一点-我已经修改了脚本,但重新运行后csv文件仍然是空的?您还有第二组{}
,这实际上创建了一个未执行的脚本块。这些应该被移除。您的$groupinformationowner
也不正确。检查我的回答,看看这是否有帮助。干杯-我们已经启动了CAC,并且我的地址还没有列入白名单-所以我回到办公室后会告诉你结果。ManagedBy是否包含用户或组?如果它是一个用户,那么你应该使用get-aduser
来代替。ManageBy包含用户-我使用get-recipient来代替。非常感谢你的脚本,它正是我想象的那样工作的。哈哈。我们的组织非常庞大,wifi连接可能比拨号更糟糕,所以只花了大约6个小时就可以运行,但最终还是成功了。我并没有期望太多,但你真的帮了我10/10,干杯谢谢分享!然而,许多DL有多个所有者,我需要获得每个所有者的用户详细信息——我得到的最接近的是下面的“一行”,它只适用于只有一个所有者的DL。获取组-anr“DL Name”|选择对象名称、DisplayName、primarySmtAddress、@{n=“ManagedBy”;e={get group-anr$.DisplayName | Select-expandproperty ManagedBy},@{n=“ManagedBy用户详细信息”;e={get group-anr$.DisplayName | Select-expandproperty ManagedBy |获取收件人|选择Name、DisplayName、PrimarySmtAddress}Export Csv-Path“File”-非类型信息
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
}