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

Posted on 2012-08-15
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 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.
Question by:abissa
    LVL 83

    Expert Comment

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

    Author Comment

    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!
    LVL 2

    Accepted Solution

    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.
    LVL 2

    Author Closing Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Integrate social media with email signatures

    Is your company active on social media? Do you also use email signatures? Including social media icons in your email signature is a great way to get fans for free. Let all your email users know you’re on social media quickly and easily, in a single click.

    Email statistics and Mailbox database quotas You might have an interest in attaining information such as mailbox details, mailbox statistics and mailbox database details from Exchange server. At that point, knowing how to retrieve this information …
    Granting full access permission allows users to access mailboxes present in their database. By giving full access permission one can open and read the content of any mailbox but cannot send emails from that mailbox.
    In this video we show how to create a mailbox database in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Servers >> Data…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now