Link to home
Start Free TrialLog in
Avatar of meenkey
meenkeyFlag for United States of America

asked on

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
Avatar of Narender Gakka
Narender Gakka
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of meenkey

ASKER

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
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.
Avatar of meenkey

ASKER

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
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

      }
Avatar of meenkey

ASKER

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)"
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.,
Avatar of meenkey

ASKER

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)
Avatar of Qlemo
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.
Avatar of meenkey

ASKER

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)
Is that really the result? Three folders with the same name?
Avatar of meenkey

ASKER

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)
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?
Avatar of meenkey

ASKER

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!
Would you like to get a clever&smart approach, or something more easy to understand?
Avatar of meenkey

ASKER

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...
"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

Avatar of meenkey

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of meenkey

ASKER

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!
Ooops, sorry about the spaces, I never can remember in which language they are allowed or not :/.