Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers 2

It says you are trying to call the ToMB method on a string called $stats.TotalItemSize.Value. This is a [string] and it does not have that method.

share|improve this answer
add comment

Do you run the script on a computer with the Exchange Management Shell installed? This also includes some datatypes Exchange uses to represent sizes, and without those such errors are quite common. A valid test would be to check if the error is present when running it on an Exchange server.

Also your code for connecting to the Exchange remoting instance would be interesting to see.

EDIT: Your code is missing some underscores here and there, so the stats object will in some cases never be filled. Here's a short explanation:

$a = "" | select Name, Value
$a.Name = "Trond"
$a.value = "Yes"

$array = @()
$array += $a

#error
$array | where {$.Name -eq "Trond"}

#success
$array | where {$_.Name -eq "Trond"}

So, in short: This line:

$stats = get-mailboxstatistics -id $ | where {$.ObjectClass –eq “Mailbox”}}

Will never produce a "$stats" object, which I'm suspecting is why your code fails. If you're using the PowerShell 3.0 ISE you shold be able to see that the ISE doesn't "understand" your filtering and shows that by not coloring the dollar sign red.

share|improve this answer
    
Yes this script is run on the server with the Exchange Management Shell, and on this server there is Exchange. –  spy86 Sep 24 '13 at 14:21
    
Could you post more of the script? We need to see whats inside the $stats variable. –  Trondh Sep 25 '13 at 11:14
    
Yes sure , please take look on code –  spy86 Sep 27 '13 at 7:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.