It seems that OutVariable is broken when using the Exchange 2010 cmdlets. Is it just my server or is this the case for everyone? I observed the following --
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
In the example above, $asdf
is never created and get-mailbox jdoe
absolutely returns something.
get-childitem -OutVariable asdf | out-null
$asdf.getType()
[PS] C:\temp>$asdf.getType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True ArrayList System.Object
In this example, $asdf
is created as expected since get-childitem
is not an Exchange 2010 cmdlet.
To be honest after your previous post I've tried that (my examples where real code I've tested - and it worked in my tests). But I was running them on my EX server (VM). And I haven't used EM Shell, I just added EX snapins to my "regular" powershell.exe
What's the difference? Well, take a closer look at commands in Exchange Management Shell:
EMS is using PSRemoting and Implicit remoting under the hood. Why it matters? Well, let's see how -OutVariable will work for impicitly-remoted command that would normally give you results, like ls:
You can also take a look at this article for more details on EMS magic: http://www.mikepfeiffer.net/2010/02/managing-exchange-2010-with-remote-powershell/
ATM I'm not sure if that's a bug, or just side effect of serialization/ deserialization of objects, or just how implicit remoting works in general. But that's definitely root cause, not EX cmdlets themselves (because as you can see - you are not really using cmdlets usually....) So - as I said - you are better of with Add-Member (my example in your linked post had to be updated, previously I've used Get-Mailbox twice instead of Get-MailboxStatistics). It's not bullet proof either (at least my examples where bit fragile) but at least it works... And you can obviously just run "regular" powershell, and just do:
... and ignore remoting stuff.
I just encountered the exact same problem with
-ErrorVariable
: Why are Exchange 2010 cmdlets ignoring ErrorVariable?.Whatever the root cause is (implicit remoting is very likely involved), the solution is to use globally-scoped variables: