Powershell "You cannot call a method on a null-valued expression"

47 views Asked by At
$Results = @()

$MbxUsers = Get-EXOMailbox -resultsize unlimited
 
foreach($user in $MbxUsers)
{

$Mbx = Get-ExoMailboxStatistics $user

 
      $Properties =  @{
      DisplayName = $user.DisplayName
      Userprincipalname = $user.UserPrincipalName
      PrimarySmtpAddress = $user.PrimarySmtpAddress
      EmailAddresses = $user.EmailAddresses
      Alias = $user.alias
      RecipientTypeDetails = $user.RecipientTypeDetails
      ItemCount = $Mbx.ItemCount
      TotalItemSize = [math]::Round($Mbx.TotalItemSize.value.ToBytes()/[math]::Pow(1024, 3), 3)
      MailboxGuid = $Mbx.MailboxGuid
       
      }


$Results += New-Object psobject -Property $properties
 
}
 
$Results | Select-Object DisplayName,Alias,Userprincipalname,PrimarySmtpAddress,EmailAddresses,ItemCount,TotalItemSize,RecipientTypeDetails,MailboxGuid | Export-Csv -nti -enc utf8 -Path .\MailboxSize.csv

While running this getting error

You cannot call a method on a null-valued expression.
At C:\Temp\MyMbxReport.ps1:11 char:7
+       $Properties =  @{
+       ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

I was expecting the ps1 to run without throwing the error

0

There are 0 answers