我正在尝试记录来自邮箱的统计信息,并且一直在使用以下功能,在这种情况下是共享邮箱
Function Shared
{
$strUserDetails=@()
$Filename = $Save_Path + $SFilename
$Shared_MBX = (Get-Mailbox -recipienttype sharedmailbox -ResultSize Unlimited -SortBy Name)
foreach ($mbx in $Shared_MBX)
{
$upn = Get-Mailbox $mbx.DisplayName | Select UserPrincipalName
$UserMailbox = get-mailboxstatistics -Identity $($mbx.DisplayName) | Select DisplayName, ItemCount,TotalItemSize
$ItemSizeString = $UserMailbox.TotalItemSize.ToString()
$strUserDetails = @(
$UserName = $upn
$ItemCount = $UserMailbox.ItemCount
$strMailboxSize = "{0:N2}" -f ($ItemSizeString.SubStrin(($ItemSizeString.IndexOf("(") + 1),($itemSizeString.IndexOf(" bytes") - ($ItemSizeString.IndexOf("(") + 1))).Replace(",",""))
)
$strUserDetails += New-Object psobject -Property @{Items=$ItemCount;Size=$strMailboxSize;Name=$UserName}
}
$strUserDetails | Export-Csv -NoTypeInformation -Append -Path $Filename
}
当我在完成后检查csv时,我会期望所有共享邮箱的UPN,Itemcount,大小,但我只能获得一个
请告诉我哪里出错了
答案 0 :(得分:0)
$strUserDetails += New-Object psobject -Property @{Items=$ItemCount;Size=$strMailboxSize;Name=$UserName}
这是你的问题,+ =这里什么都不做,并且充当=。您正在为每个邮箱创建一个新对象,并覆盖。结果只会获得最后一个邮箱。
方法调用失败,因为[System.Management.Automation.PSObject]不包含名为' op_Addition'的方法。
在foreach循环之前创建一个数组。然后,为每个循环添加一个PSCustomObject。例如:
$strUserDetails = @()
foreach ($mbx in $Shared_MBX)
{
$strUserDetails += [PSCustomObject]@{
Items=$ItemCount
Size=$strMailboxSize
Name=$UserName
}
}
$strUserDetails
这是用MCVE解决问题的一个例子。
答案 1 :(得分:0)
使用您提供的帮助
Function Shared
{
write-host "Running Shared Mailbox extract." -ForegroundColor Cyan
$Mailboxes = (Get-Mailbox -recipienttype sharedmailbox -ResultSize Unlimited)
$Filename = $Save_Path + $SFilename
Write-Host "This will save the extract file to " $Filename
foreach ($mbx in $Mailboxes)
{
$strUserDetails = @()
$upn = Get-Mailbox $mbx.DisplayName | Select UserPrincipalName
$UserMailbox = get-mailboxstatistics -Identity $($mbx.DisplayName) | Select DisplayName, ItemCount,TotalItemSize
$ItemSizeString = $UserMailbox.TotalItemSize.ToString()
$UserName = $upn.UserPrincipalName
$ItemCount = $UserMailbox.ItemCount
$strMailboxSize = "{0:N2}" -f ($ItemSizeString.SubString(($ItemSizeString.IndexOf("(") + 1),($itemSizeString.IndexOf(" bytes") - ($ItemSizeString.IndexOf("(") + 1))).Replace(",",""))
#Write-Host "Accessing stats for mailbox:" $username
$strUserDetails = [PSCustomObject]@{
Name=$UserName
Items=$ItemCount
Size=$strMailboxSize
}
$strUserDetails | Export-Csv -NoTypeInformation -Append -Path $Filename
}
Write-host "Completed Shared Extract" -ForegroundColor Cyan
}