1

This time I am stuck with another PowerShell GUI challenge. I have a Form that contains two different comboboxes (combobox1 and combobox2)

What I want to do is: The first combobox to show a list of all the clients that I have and the second combobox to show all the different mailboxes that are available for the client that has been selected on the first combobox.

Once again I don't know how to do it so I am asking you guys for advise.

I managed to show a list of all the clients on the first combobox. But I have never managed to populate the second combobox showing the different mailboxes available for that specific client.

This is the code I've got so far, the issue is that I don't know how to do to "trigger the population of the second combobox"

$MainForm_Load={
#TODO: Initialize Form Controls here
add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010 -ea silentlycontinue
import-module activedirectory

$Clients = Get-ADOrganizationalUnit -SearchBase 'OU=Clients,DC=asp,DC=xaracloud,DC=net' -SearchScope Onelevel -Filter * -Properties Description

foreach ($client in $Clients)
{
    $CurrentClient = "{0} ({1})" -f $client.Name, $client.Description
    Load-ComboBox $combobox1 $CurrentClient -Append
}

if ($combobox1.SelectedIndex -gt -1)
{

    $ClientSelected = ($combobox1.SelectedItem) -replace " \(.*\)"

    $Mailboxes = Get-Mailbox -OrganizationalUnit $ClientSelected
    foreach ($mailbox in $Mailboxes)
    {
        $CurrentMailbox = "{0} ({1})" -f $mailbox.Name, $mailbox.Alias
        Load-ComboBox $combobox2 $CurrentMailbox -Append
    }

}

}

Please note that in order to populate the second combobox basing on the selection of the first one I have to query my exchange server and that takes a few seconds. Is there any way of showing a process bar to say that the request is being processed? I don't want the user to think that the script is doing nothing or that it doesn't work.

miticoluis
  • 511
  • 2
  • 15
  • 32

1 Answers1

1

Remove the | Out-String from the Get-Mailbox line and add use one of this options(of course there's more):

Use a ComboBox.SelectionChangeCommitted Event:

"Occurs when the user changes the selected item and that change is displayed in the ComboBox"

$combobox2_SelectionChangeCommitted={

  $Mailboxes = Get-Mailbox -OrganizationalUnit $ClientSelected
  foreach ($mailbox in $Mailboxes)
  {
      $CurrentMailbox = "{0} ({1})" -f $mailbox.Name, $mailbox.Alias
      Load-ComboBox $combobox2 $CurrentMailbox -Append
  }

}

Use a button:

$button1_Click={

$Mailboxes = Get-Mailbox -OrganizationalUnit $ClientSelected
  foreach ($mailbox in $Mailboxes)
  {
      $CurrentMailbox = "{0} ({1})" -f $mailbox.Name, $mailbox.Alias
      Load-ComboBox $combobox2 $CurrentMailbox -Append
  }
}

Also, there's more options, you can start with one of the above...

Avshalom
  • 8,657
  • 1
  • 25
  • 43
  • I agree, that was a mistake, but it is not the solution. I have just tested it and still, same issue. I select the client, and the second combobox never populates – miticoluis Oct 29 '15 at 19:50
  • how do you submit the client selection, a button? or by event handler? – Avshalom Oct 29 '15 at 20:16
  • I don't submit the client selection using a button nor by event handler. That is the problem, I don't know how to trigger the second combobox to populate. I would like to do it once a client is selected. So it would be by event handler but I don't know how to do it. – miticoluis Oct 30 '15 at 00:13
  • I would like to use an event instead. The reason why I want to use an event is because the user interface would look nicer. I am already using many buttons, avoiding more buttons would be great. – miticoluis Oct 30 '15 at 10:26
  • I figured out how to do it using an event, so I don't need a button: $combobox1_SelectedIndexChanged={ my code } Thanks for your help anyway – miticoluis Nov 04 '15 at 16:34
  • I am sorry for that Avshalom, I didn't notice about your first option. Thanks for your help, that is definitely the right answer. – miticoluis Nov 05 '15 at 14:29