I have some script write on PowerShell and I have some problem. When I run this script I have some error in output:
You cannot call a method on a null-valued expression.
At C:\scripts\exchange_rep_work_cp.ps1:133 char:82
+ $mbx | add-member -membertype noteproperty -value $stats.TotalItemSize.Value.ToMB <<<< () -name TotalSize
+ CategoryInfo : InvalidOperation: (ToMB:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
This error repeat few times. This is the line of code (133):
130 $tmp = ""
131 $Mailboxes | foreach {
132 #Name_of_company has a diffrent domain and domain controller
133 if ($Company -eq "Name_of_company"){
134 $stats = get-mailboxstatistics -DomainController zzzz.xxx.yyyyyy.local -id $_ | where >135 {$.ObjectClass –eq “Mailbox”} }
136 else {
137 $stats = get-mailboxstatistics -id $ | where {$.ObjectClass –eq “Mailbox”}}
138 $MBx = new-object system.object
139 $tmp = $.EmailAddresses | foreach { if ($_ -notmatch "yyyyyy.local") { $.AddressString } }
140: $Country = $.Customattribute1
141: $Types = $_.Customattribute2
142: $mbx | add-member -membertype noteproperty -value $stats.Displayname -name DisplayName
143: $mbx | add-member -membertype noteproperty -value $stats.TotalItemSize.Value.ToMB() -name TotalSize
Please give me some advise what's wrong
Regards