【发布时间】: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