我正在使用 PowerShell 从 Exchange Server 2010 构建某些邮箱属性的报告。以下代码在我的管理工作站上使用远程会话完美运行。
$Mailboxes = Get-mailbox -ResultSize Unlimited
foreach ($Mailbox in $Mailboxes)
{
$Mailbox | Add-Member -MemberType "NoteProperty" -Name "MailboxSizeMB" -Value (Get-MailboxStatistics $Mailbox).TotalItemSize
}
但是,当我将.Value.ToMb()方法添加到 TotalItemSize 属性时,脚本失败并出现以下错误:
$Mailboxes = Get-mailbox -ResultSize Unlimited
foreach ($Mailbox in $Mailboxes)
{
$Mailbox | Add-Member -MemberType "NoteProperty" -Name "MailboxSizeMB" -Value ((Get-MailboxStatistics $Mailbox).TotalItemSize.Value.ToMb())
}
您不能在空值表达式上调用方法。在 line:6 char:6 + $Mailbox | Add-Member -MemberType "NoteProperty" -Name "MailboxSizeMB" -Val ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
无法处理参数“身份”的参数转换。无法将类型“Deserialized.Microsoft.Exchange.Data.Directory.Management.Mailbox”的“[Mailbox Name Redacted]”值转换为类型“Microsoft.Exchange.Configuration.Tasks.GeneralMailboxOrMailUserIdParameter”。+ CategoryInfo : InvalidData: (:) [Get-MailboxStatistics], ParameterBindin...mationException + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-MailboxStatistics
然而...当我在 Exchange 服务器上本地运行第二个代码块(将 TotalItemSize 值转换为 MB)时,它可以完美运行。谁能解释为什么这只会远程失败?