powershell 根据另一个组合框上的所选项目填充组合框

powershell populate combobox basing on the selected item on another combobox

这次我遇到了另一个 PowerShell GUI 挑战。 我有一个包含两个不同组合框(combobox1 和 combobox2)的表单

我想做的是: 第一个组合框显示我拥有的所有客户端的列表,第二个组合框显示可用于在第一个组合框上选择的客户端的所有不同邮箱。

我又一次不知道该怎么做,所以我请大家指教。

我设法在第一个组合框中显示了所有客户端的列表。但我从未设法填充第二个组合框,显示可用于该特定客户端的不同邮箱。

这是我目前得到的代码,问题是我不知道该怎么做 "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
    }

}

}

请注意,为了根据第一个组合框的选择填充第二个组合框,我必须查询我的交换服务器,这需要几秒钟。有什么方法可以显示一个进程条来表明正在处理请求?我不希望用户认为脚本什么都不做或不起作用。

从 Get-Mailbox 行中删除 | Out-String 并添加使用此选项之一(当然还有更多):

使用 ComboBox.SelectionChangeCommitted 事件:

"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
  }

}

使用按钮:

$button1_Click={

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

此外,还有更多的选择,你可以从上面的一个开始...