Exchange 2010 PowerShell script throwing errors on specific users.

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
meenkeyAsked:
Who is Participating?
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
So you are not using PS3 or above (you really should update, if possible!). Then we have to write the checks slightly different:
$mailboxes = @(Get-Mailbox -ResultSize Unlimited)
$report = @()

foreach ($mailbox in $mailboxes)
{
  $mbstat        = Get-MailboxFolderStatistics $mailbox
  $inboxstats    = $mbstat | Where {"/Inbox"        , "/Bandeja de entrada"   -contains $_.FolderPath}
  $deletedstats  = $mbstat | Where {"/Deleted Items", "/Elementos eliminados" -contains $_.FolderPath}
  $sentitemstats = $mbstat | Where {"/Sent Items"   , "/Elementos enviados"   -contains $_.FolderPath}
  $calendarstats = $mbstat | Where {"/Calendar"     , "/Calendario"           -contains $_.FolderPath}

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

  $report += New-Object PSObject -Property @{
    "Display Name"      = $mailbox.DisplayName
    "Inbox Size (Mb)"   = $inboxstats   .FolderandSubFolderSize  / 1mb
    "Deleted Items(MB)" = $deletedstats .FolderAndSubfolderSize  / 1mb
    "Sent Items(MB)"    = $sentitemstats.FolderAndSubfolderSize  / 1mb
    "Calendar(MB)"      = $calendarstats.FolderAndSubfolderSize  / 1mb
  }
}
$report | Export-Csv c:\Temp\Reportvdimas.csv -NoTypeInformation

Open in new window

0
 
Narender GakkaAWS / DevOps / Cloud ConsultantCommented:
can you replace

Add-Member -MemberType NoteProperty -Name "Inbox Size (Mb)" -Value $inboxstats.FolderandSubFolder
Size.ToMB


by

Add-Member -MemberType NoteProperty -Name "Inbox Size (Mb)" -Value $inboxstats.FolderandSubFolder
Size  / 1mb
0
 
meenkeyAuthor Commented:
Narender,

I tried this
$mailboxes = @(Get-Mailbox <username>)
$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  / 1mb
           $mbObj | Add-Member -MemberType NoteProperty -Name "Deleted Items(MB)" -Value $deletedstats.FolderAndSubfolderSize  / 1mb
           $mbObj | Add-Member -MemberType NoteProperty -Name "Sent Items(MB)" -Value $sentitemstats.FolderAndSubfolderSize  / 1mb
           $mbObj | Add-Member -MemberType NoteProperty -Name "Calendar(MB)" -Value $calendarstats.FolderAndSubfolderSize  / 1mb

           $report += $mbObj

      }


and got this error


Add-Member : A positional parameter cannot be found that accepts argument '1mb'.
At line:10 char:31
+            $mbObj | Add-Member <<<<  -MemberType NoteProperty -Name "Inbox Size (Mb)" -Value $inboxstats.FolderandSub
FolderSize  / 1mb
    + CategoryInfo          : InvalidArgument: (:) [Add-Member], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.AddMemberCommand
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
Narender GakkaAWS / DevOps / Cloud ConsultantCommented:
Sorry, can you please try to remove  .ToMB() function completely from your initial version and try if it succeeds.

If yes, then you can convert the string value manually, it seems that the value which is returning for the 46 users is not a string.
0
 
meenkeyAuthor Commented:
Yes, that is what my last post showed??  is this what you are looking for ?


$mailboxes = @(Get-Mailbox <user>)
$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  / 1mb()
           $mbObj | Add-Member -MemberType NoteProperty -Name "Deleted Items(MB)" -Value $deletedstats.FolderAndSubfolderSize  / 1mb()
           $mbObj | Add-Member -MemberType NoteProperty -Name "Sent Items(MB)" -Value $sentitemstats.FolderAndSubfolderSize  / 1mb()
           $mbObj | Add-Member -MemberType NoteProperty -Name "Calendar(MB)" -Value $calendarstats.FolderAndSubfolderSize  / 1mb()

           $report += $mbObj

      }

I have this changed out "......FolderandSubFolderSize  / 1mb()"  


with this script I get this error


An expression was expected after '('.
At line:10 char:130
+            $mbObj | Add-Member -MemberType NoteProperty -Name "Inbox Size (Mb)" -Value $inboxstats.FolderandSubFolder
Size  / 1mb( <<<< )
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedExpression
0
 
Narender GakkaAWS / DevOps / Cloud ConsultantCommented:
I meant to remove /mb too so the new looks like below.

$mailboxes = @(Get-Mailbox <user>)
$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
           $mbObj | Add-Member -MemberType NoteProperty -Name "Deleted Items(MB)" -Value $deletedstats.FolderAndSubfolderSize
           $mbObj | Add-Member -MemberType NoteProperty -Name "Sent Items(MB)" -Value $sentitemstats.FolderAndSubfolderSize
           $mbObj | Add-Member -MemberType NoteProperty -Name "Calendar(MB)" -Value $calendarstats.FolderAndSubfolderSize

           $report += $mbObj

      }
0
 
meenkeyAuthor Commented:
I did not get an error, however the output looks like this for the tested 46

"Display Name","Inbox Size (Mb)","Deleted Items(MB)","Sent Items(MB)","Calendar(MB)"
"<lastname>, <firstname>",,,,

where as for my user account I get this

"Display Name","Inbox Size (Mb)","Deleted Items(MB)","Sent Items(MB)","Calendar(MB)"
"<lastname>, <firstname>","53.53 MB (56,134,463 bytes)","15.87 MB (16,644,152 bytes)","18.74 MB (19,652,660 bytes)","788.4 KB (807,296 bytes)"
0
 
Narender GakkaAWS / DevOps / Cloud ConsultantCommented:
seems those blank values were the cause of the errors, your script is perfect, since the values are not returning proper values hence the error.

Can you check why is the script failing to get the values for these 46 users, are they valid etc., etc.,
0
 
meenkeyAuthor Commented:
That is what my question is. why is my script failing for some users. I can run this command for example on failing users
Get-MailboxFolderStatistics <user> -FolderScope Inbox | Select FolderSize
and get results. Here is the results of the user that returned ",,,," with my script.
FolderSize
----------
4.262 GB (4,576,711,674 bytes)
637.1 MB (668,038,840 bytes)
36.81 MB (38,598,161 bytes)
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Dump
Get-MailboxFolderStatistics <user> -FolderScope Inbox | Select FolderPath, FolderSize

Open in new window

for a failing user because the folder path is the culprit for sure.
0
 
meenkeyAuthor Commented:
PS C:\Users\server> Get-MailboxFolderStatistics <user> -FolderScope Inbox | Select FolderPath, FolderSize

FolderPath                                                  FolderSize
----------                                                  ----------
/foldername                                         4.27 GB (4,585,222,849 bytes)
/foldername                                                      637.1 MB (668,038,840 bytes)
/foldername                                               36.81 MB (38,598,161 bytes)
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Is that really the result? Three folders with the same name?
0
 
meenkeyAuthor Commented:
I guess it might help to have the folder name listed

PS C:\Users\server> Get-MailboxFolderStatistics <user> -FolderScope Inbox | Select FolderPath, FolderSize

FolderPath                                                  FolderSize
----------                                                  ----------
/Bandeja de entrada                                         4.27 GB (4,585,222,849 bytes)
/DELIA                                                      637.1 MB (668,038,840 bytes)
/ZZZ - Varios                                               36.81 MB (38,598,161 bytes)
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
There is no Inbox folder, so $inboxstats will be $null. I assume the same applies to the other default folders. Is that a resource mailbox, or maybe a archive mailbox? What's special about it?
0
 
meenkeyAuthor Commented:
So interesting find. I have been through about 12 of the 46 and they are in Spanish so they have "Bandeja de entrada" which means Inbox in Spanish and "Elementos enviados" for sent "Elementos eliminados" for deleted and "Calendario" for calendar.
I just tried this modified line and it works.
Get-MailboxFolderStatistics <user> -FolderScope Inbox | Where {$_.FolderPath -eq "/Bandeja de entrada"}

Can you help me with best practice coding that would try the Spanish version if it sees the null error? or if you have a better way I'm all ears!
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Would you like to get a clever&smart approach, or something more easy to understand?
0
 
meenkeyAuthor Commented:
Well honestly I would like both because I am trying to learn, but I don't want to waste your time so I if I had to chose one I will go with the easy to understand...
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
"Easy" one, with some optimizations.
$mailboxes = @(Get-Mailbox <username>)
$report = @()

foreach ($mailbox in $mailboxes)
{
  $mbstat        = Get-MailboxFolderStatistics $mailbox
  $inboxstats    = $mbstat | Where {$_.FolderPath -in "/Inbox"        , "/Bandeja de entrada"   }
  $deletedstats  = $mbstat | Where {$_.FolderPath -in "/Deleted Items", "/Elementos eliminados" }
  $sentitemstats = $mbstat | Where {$_.FolderPath -in "/Sent Items"   , "/Elementos enviados"   }
  $calendarstats = $mbstat | Where {$_.FolderPath -in "/Calendar"     , "/Calendario"           }

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

  $report += New-Object PSObject -Property @{
    "Display Name"      = $mailbox.DisplayName
    "Inbox Size (Mb)"   = $inboxstats   .FolderandSubFolderSize  / 1mb
    "Deleted Items(MB)" = $deletedstats .FolderAndSubfolderSize  / 1mb
    "Sent Items(MB)"    = $sentitemstats.FolderAndSubfolderSize  / 1mb
    "Calendar(MB)"      = $calendarstats.FolderAndSubfolderSize  / 1mb
  }
}

Open in new window

More clever:
$report = @()

foreach ($mailbox in Get-Mailbox <username>)
{
  $mbstat        = Get-MailboxFolderStatistics $mailbox
  $stats  = @()
  foreach ($mbs in (("/Inbox"        , "/Bandeja de entrada"  ),
                    ("/Deleted Items", "/Elementos eliminados"),
                    ("/Sent Items"   , "/Elementos enviados"  ),
                    ("/Calendar"     , "/Calendario"          ))
  {
    $stats += $mbstat | ? { $_.FolderPath -in $mbs } | Select -Expand FolderAndSubFolderSize
  }

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

  $report += New-Object PSObject -Property @{
    "Display Name"       = $mailbox.DisplayName
    "Inbox Size (Mb)"    = $stats[0] / 1mb
    "Deleted Items (MB)" = $stats[1] / 1mb
    "Sent Items (MB)"    = $stats[2] / 1mb
    "Calendar (MB)"      = $stats[3] / 1mb
  }
}

Open in new window

0
 
meenkeyAuthor Commented:
Hmm, so I get errors with both of them :-/ let me take your easier one first. Im going to add "-ResultSize Unlimited" and "$report | Export-Csv c:\Temp\Reportvdimas.csv -NoTypeInformation"


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

foreach ($mailbox in $mailboxes)
{
  $mbstat        = Get-MailboxFolderStatistics $mailbox
  $inboxstats    = $mbstat | Where {$_.FolderPath -in "/Inbox"        , "/Bandeja de entrada"   }
  $deletedstats  = $mbstat | Where {$_.FolderPath -in "/Deleted Items", "/Elementos eliminados" }
  $sentitemstats = $mbstat | Where {$_.FolderPath -in "/Sent Items"   , "/Elementos enviados"   }
  $calendarstats = $mbstat | Where {$_.FolderPath -in "/Calendar"     , "/Calendario"           }

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

  $report += New-Object PSObject -Property @{
    "Display Name"      = $mailbox.DisplayName
    "Inbox Size (Mb)"   = $inboxstats   .FolderandSubFolderSize  / 1mb
    "Deleted Items(MB)" = $deletedstats .FolderAndSubfolderSize  / 1mb
    "Sent Items(MB)"    = $sentitemstats.FolderAndSubfolderSize  / 1mb
    "Calendar(MB)"      = $calendarstats.FolderAndSubfolderSize  / 1mb
  }
}
$report | Export-Csv c:\Temp\Reportvdimas.csv -NoTypeInformation


I get this error

You must provide a value expression on the right-hand side of the '-' operator.
At line:4 char:52
+   $inboxstats    = $mbstat | Where {$_.FolderPath - <<<< in "/Inbox"        , "/Bandeja de entrada"   }
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedValueExpression
0
 
meenkeyAuthor Commented:
Yeah, The change management process here is pretty crazy. an upgrade is in the works but it could be months.....  So I tried your new version and get this error

Unexpected token '.FolderandSubFolderSize' in expression or statement.
At line:11 char:64
+     "Inbox Size (Mb)"   = $inboxstats   .FolderandSubFolderSize <<<<   / 1mb
    + CategoryInfo          : ParserError: (.FolderandSubFolderSize:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

Ah, but using my noob common sense I removed the extra spaces and it is working :D wahoo!!  I slowly walked through the code and it looks pretty straight forward as far as what its doing. This easier code is a lot better then what I was getting into! Thank you so much for the help, I really appreciate it!
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Ooops, sorry about the spaces, I never can remember in which language they are allowed or not :/.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.