[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1426
  • Last Modified:

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

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)

        runspace.Open()
        Dim pipeline = runspace.CreatePipeline()
        pipeline.Commands.Add(command)
        Dim results = pipeline.Invoke()
        runspace.Dispose()

        For Each item In results
            MsgBox(item.ToString)
        Next

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.
0
abissa
Asked:
abissa
  • 3
1 Solution
 
CodeCruiserCommented:
If you put a breakpoint on foreach loop and hover over the item, what properties/methods do you see?
0
 
abissaAuthor Commented:
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!
methods.png
properties.png
0
 
abissaAuthor Commented:
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.
0
 
abissaAuthor Commented:
Understood the flaw in my reasoning myself. My last comment should enlighten those that wonders.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now