I have a script that I've put together to gather stats on item count and size for two custom folders we've deployed to all mailboxes. It seems to work fine for the most part but some mailboxes it doesn't return any data.
Correct results were returned on a test mailbox. I renamed the folders and tested and got errors. Then renamed them back and I get the error below, there still seem to be null values returned. There are items in these folders in my test mailbox. If I pull out the individual get-mailboxfolderstatistics commands and look at those results the data looks correct. Looking for ideas as to what might be going on as to why the script isn't looking at the current data.
Stats Script:
$mailboxes = @(Get-Mailbox mailboxA) <---- Typically this is a full list of all mailboxes but I'm limiting it to one for testing
$report = @()
foreach ($mailbox in $mailboxes)
{
$FolderA = Get-MailboxFolderStatistics $mailbox | where {($_.Name -like "Folder A")}
$FolderB = Get-MailboxFolderStatistics $mailbox | where {($_.Name -like "Folder B")}
$mbObj = New-Object PSObject
$mbObj | Add-Member -MemberType NoteProperty -Name "Display Name" -Value $mailbox.DisplayName
$mbObj | Add-Member -MemberType NoteProperty -Name "FolderA (Mb)" -Value $FolderA.FolderandSubFolderSize.ToMB()
$mbObj | Add-Member -MemberType NoteProperty -Name "FolderB (Mb)" -Value $FolderB.FolderandSubFolderSize.ToMB()
$mbObj | Add-Member -MemberType NoteProperty -Name "FolderA Items" -Value $FolderA.ItemsinFolderandSubfolders
$mbObj | Add-Member -MemberType NoteProperty -Name "FolderB Items" -Value $FolderB.ItemsinFolderandSubfolders
$report += $mbObj
}
$report
Output:
You cannot call a method on a null-valued expression.
At C:\Scripts\Folder Statistics\folders.ps1:11 char:122
+ $mbObj | Add-Member -MemberType NoteProperty -Name "FolderA" -Value $FolderA.FolderandSubFolderSize.
ToMB <<<< ()
+ CategoryInfo : InvalidOperation: (ToMB:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Scripts\Folder Statistics\folders.ps1:12 char:120
+ $mbObj | Add-Member -MemberType NoteProperty -Name "FolderB" -Value $FolderB.FolderandSubFolderSize.To
MB <<<< ()
+ CategoryInfo : InvalidOperation: (ToMB:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull