I am building a report of certain mailbox attributes from Exchange Server 2010 using PowerShell. The following code worked perfectly from my management workstation using a remote session.
$Mailboxes = Get-mailbox -ResultSize Unlimited
foreach ($Mailbox in $Mailboxes)
{
$Mailbox | Add-Member -MemberType "NoteProperty" -Name "MailboxSizeMB" -Value (Get-MailboxStatistics $Mailbox).TotalItemSize
}
However, when I added the .Value.ToMb() method to the TotalItemSize attribute, the script failed with the following error:
$Mailboxes = Get-mailbox -ResultSize Unlimited
foreach ($Mailbox in $Mailboxes)
{
$Mailbox | Add-Member -MemberType "NoteProperty" -Name "MailboxSizeMB" -Value ((Get-MailboxStatistics $Mailbox).TotalItemSize.Value.ToMb())
}
You cannot call a method on a null-valued expression. At line:6 char:6 + $Mailbox | Add-Member -MemberType "NoteProperty" -Name "MailboxSizeMB" -Val ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
Cannot process argument transformation on parameter 'Identity'. Cannot convert the "[Mailbox Name Redacted]" value of type "Deserialized.Microsoft.Exchange.Data.Directory.Management.Mailbox" to type "Microsoft.Exchange.Configuration.Tasks.GeneralMailboxOrMailUserIdParameter". + CategoryInfo : InvalidData: (:) [Get-MailboxStatistics], ParameterBindin...mationException + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-MailboxStatistics
And yet...the second block of code (converting the TotalItemSize value to MB) works perfectly when I run it locally on the Exchange server. Can anyone explain why this only fails remotely?
This happens when you're missing the .Net types Exchange uses. PowerShell remoting perfomrs some serialization that makes some objects end up as "PsObject" instead of the full strongy typed object. The solution is to install the Exchange Management Console on your client computer, this will include the .Net types you need.
Try this:
Then run the command set.