2

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.

pk.
  • 6,413
  • 1
  • 41
  • 63
  • `-OutVariable` as a [common parameter](http://technet.microsoft.com/en-us/library/hh847884.aspx) is implemented by the PowerShell runtime – not the cmdlet – therefore this is not about Exchange cmdlets. – Richard May 24 '12 at 17:21
  • What happens if you try `$asdf = get-mailbox jdoe -OutVariable`? – Richard May 24 '12 at 17:22
  • Well, I'm hard-pressed to find any evidence that this is **not** specifically about Exchange cmdlets since it works for everything but them. Furthermore, `$asdf = get-mailbox jdoe -OutVariable` is just incorrect syntax and rejected as such. The manner in which I used them is correct in both examples above. It simply doesn't work properly in the first. Is anyone else able to reproduce this behavior? – pk. May 24 '12 at 17:59

2 Answers2

2

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:

Get-Command Get-Mailbox | select CommandType

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:

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

True

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:

 Add-PSSnappin -Name Microsoft.Exchange.*

... and ignore remoting stuff.

BartekB
  • 666
  • 6
  • 9
  • This is great! I'm beginning to think I need to avoid the EMS altogether. I haven't read through the link you sent (but soon will), but at the moment I don't see any advantage to using the EMS over vanilla PowerShell with a Snapin. I wish I could upvote you more. Thanks. – pk. May 25 '12 at 13:23
  • Manually loading the Exchange 2010 snap-in is not supported; also, you could very well be running EMS on a computer which is not an Exchange server... good luck avoiding remoting then. – Massimo Jul 31 '13 at 17:03
0

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:

Get-Mailbox UserName -OutVariable global:outvar
Massimo
  • 68,714
  • 56
  • 196
  • 319