Exchange 2010 cmdlet的OutVariable已损坏

在使用Exchange 2010 cmdlet时,OutVariable似乎中断了。 这仅仅是我的服务器,还是这是每个人的情况? 我观察到以下 –

get-mailbox jdoe -OutVariable asdf | out-null $asdf.getType() You cannot call a method on a null-valued expression. At line:1 char:14 + $asdf.getType <<<< () + CategoryInfo : InvalidOperation: (getType:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull 

在上面的例子中, $asdf永远不会被创build, get-mailbox jdoe绝对会返回一些东西。

 get-childitem -OutVariable asdf | out-null $asdf.getType() [PS] C:\temp>$asdf.getType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True ArrayList System.Object 

在此示例中, $asdf是按预期方式创build的,因为get-childitem不是Exchange 2010 cmdlet。

说实话,以前的post后,我已经尝试过(我的例子,我testing过的真实代码 – 它在我的testing中工作)。 但是我在EX服务器(VM)上运行它们。 而我没有使用EM Shell,我只是添加EX snapins到我的“常规”powershell.exe

有什么不同? 那么仔细看一下Exchange命令行pipe理程序中的命令:

 Get-Command Get-Mailbox | select CommandType 

EMS正在使用PSRemoting和Implicit remoting。 为什么重要? 那么,让我们来看看-OutVariable如何用于通常会给你结果的impicitly-remoted命令,比如ls:

 $Session = New-PSSession -ComputerName EX Import-PSSession -Prefix Test -Session $Session -CommandName Get-ChildItem Get-TestChildItem -OutVariable Foo | Out-Null $Foo -eq $null True 

你也可以看看这篇文章,了解更多关于EMS魔术的细节: http : //www.mikepfeiffer.net/2010/02/managing-exchange-2010-with-remote-powershell/

ATM我不知道这是一个错误,或只是对象的序列化/反序列化的副作用,或者一般隐式远程处理的工作方式。 但是,这绝对是根本原因,而不是EX cmdlet本身(因为正如你所看到的 – 通常你并不是真的在使用cmdlet)所以 – 正如我所说 – 你更好的与添加成员(我的例子在你的链接的post必须更新,以前我用Get-Mailbox两次而不是Get-MailboxStatistics)。 这也不是子弹的certificate(至less我的例子有点脆弱),但至less它的工作原理…你显然可以运行“常规”的PowerShell,只是:

  Add-PSSnappin -Name Microsoft.Exchange.* 

…而忽略远程的东西。

我刚刚遇到与-ErrorVariable完全相同的问题: 为什么Exchange 2010 cmdlet忽略ErrorVariable? 。

不pipe根本原因是什么(隐式远程处理很可能涉及到),解决方法是使用全局范围的variables:

 Get-Mailbox UserName -OutVariable global:outvar