We help IT Professionals succeed at work.
Get Started

Exchange 2010 PowerShell script throwing errors on specific users.

108 Views
Last Modified: 2016-06-26
I have been given the task to track the size of each users inbox, deleted items, sent items, and calendar. I do not find much on the googles for reporting just inbox size, rather most IT seem to report the entire mailbox size. I did however find and slightly modify the script below that works perfectly on 776/822 of our users. with the other 46 users I get the error below. I have tried for longer then I am willing to admit trying to get this script and others to work. Possibly there is a different way to loop this, I can manually pull information from the users, just not with the scripting methods I have tried.  

You cannot call a method on a null-valued expression.
At line:10 char:127
+            $mbObj | Add-Member -MemberType NoteProperty -Name "Inbox Size (Mb)" -Value $inboxstats.FolderandSubFolder
Size.ToMB <<<< ()
    + CategoryInfo          : InvalidOperation: (ToMB:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull



The error repeats for all 4 boxes. Here is the script I am using



$mailboxes = @(Get-Mailbox -ResultSize Unlimited)
$report = @()

foreach ($mailbox in $mailboxes)
    {
          $inboxstats = Get-MailboxFolderStatistics $mailbox -FolderScope Inbox | Where {$_.FolderPath -eq "/Inbox"}
          $deletedstats = Get-MailboxFolderStatistics $mailbox | Where {$_.FolderPath -eq "/Deleted Items"}
          $sentitemstats = Get-MailboxFolderStatistics $mailbox | Where {$_.FolderPath -eq "/Sent Items"}
          $calendarstats = Get-MailboxFolderStatistics $mailbox | Where {$_.FolderPath -eq "/Calendar"}

           Write-Host "Getting Stats for " $mailbox.DisplayName -ForegroundColor Green

           $mbObj = New-Object PSObject
           $mbObj | Add-Member -MemberType NoteProperty -Name "Display Name" -Value $mailbox.DisplayName
           $mbObj | Add-Member -MemberType NoteProperty -Name "Inbox Size (Mb)" -Value $inboxstats.FolderandSubFolderSize.ToMB()
           $mbObj | Add-Member -MemberType NoteProperty -Name "Deleted Items(MB)" -Value $deletedstats.FolderAndSubfolderSize.ToMB()
           $mbObj | Add-Member -MemberType NoteProperty -Name "Sent Items(MB)" -Value $sentitemstats.FolderAndSubfolderSize.ToMB()
           $mbObj | Add-Member -MemberType NoteProperty -Name "Calendar(MB)" -Value $calendarstats.FolderAndSubfolderSize.ToMB()

           $report += $mbObj

      }

$report | Export-Csv c:\Temp\Report.csv -NoTypeInformation
Comment
Watch Question
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015
Commented:
This problem has been solved!
Unlock 1 Answer and 21 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE