We help IT Professionals succeed at work.

VB.NET and exchange 2010 powershell: cannot get all results in collection

Medium Priority
Last Modified: 2012-08-21
Dear experts,

I'm programming some maintenance tools in VB.NET interacting with our Exchange 2010 server via PowerShell. Everything works fine but this, here's the simple code snippet:

        Dim command As New Command("Get-Mailbox")
        command.Parameters.Add("Identity", GInitials)

        Dim pipeline = runspace.CreatePipeline()
        Dim results = pipeline.Invoke()

        For Each item In results

Open in new window

Basically, I'm calling the Get-Mailbox cmdlet with the identity switch: "Get-Mailbox -Identity <initials>"
If I run this cmdlet on the exchange server in the Powershell window directly, I get 4 results in columns:

Name / Alias / ServerName / ProhibitSendQuota

Now in my program above, my variable "results" which stores these results, is a
"System.Collections.ObjectModel.Collection(Of System.Management.Automation.PSObject)

So I was expecting to get a collection of the same 4 results..in fact I am not.
When I loop through the collection, I only get 1 result: the 1st one, 'Name'.

How do I get/access the other results??? What I am missing or doing wrong?
I'm actually trying to get the "ServerName"...

Thank you so much for your help.
Watch Question

Most Valuable Expert 2012
Top Expert 2014

If you put a breakpoint on foreach loop and hover over the item, what properties/methods do you see?


First of all please accept my apologies if I'm not accurately answering your question, I'm a self-taught programmer and don't know my way around the IDE perfectly!

I also want to note I made a small change since yesterday. I strictly declared the type of my 'results' variable as the collection type that the Invoke is supposed to return as follows:

        Dim results As System.Collections.ObjectModel.Collection(Of System.Management.Automation.PSObject)
        results = pipeline.Invoke()

Open in new window

...but it didn't change anything unfortunately.

If I set a Breakpoint on the for each loop and hover 'results', I get "Count = 1", that is the Name as expected.

Here are the properties and methods attached as a screenshot, I hope it helps you somewhat because to me it looks like Chinese :)

Cheers for the help!
I understood the flaw in my reasoning.

I was expecting all parameters to be stored in the collection, when in fact the collection would only store all results if there were several!

as I specify the 'identity' parameter, only one result comes, but if I only used "Get-Mailbox" all mailboxes would be stored in the collection.

The fact that in the Powershell console I see 4 columns when using "Get-Mailbox -identity initials" is only for convenience I suppose.

In my programs, to get the "ServerName" member I need to run the command that way:

Get-Mailbox -Identity initials | Select-Object ServerName

Open in new window

and then it returns the ServerName only for the specified Identity, that I can store and use in my program.


Understood the flaw in my reasoning myself. My last comment should enlighten those that wonders.