Powershell - 如何使用下拉菜单进行实时Active Directory搜索

时间:2013-02-06 21:49:45

标签: winforms powershell drop-down-menu powershell-v2.0

对于像您这样经验丰富的人来说,这可能是一个简单的问题,但如何在下拉菜单中对用户名进行实时Active Directory搜索,将选定的结果传递给$var

这是我和我的两个下拉菜单,我希望manager下拉列表根据我输入的字符串在我的Active Directory中进行查找。即如果我输入Macdonald,则下拉项目会在我的AD中显示所有Macdonald的名字或姓氏。

function GenerateForm {

[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null

$form1 = New-Object System.Windows.Forms.Form
$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel2 = new-object System.Windows.Forms.Label
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown2 = new-object System.Windows.Forms.ComboBox


$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
    $form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$form1.Text = "User Creation software"
$form1.Name = "form1"
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 400
$System_Drawing_Size.Height = 200
$form1.ClientSize = $System_Drawing_Size

$DropDownArray = "Site1" , "Site2" , "Site3"

$DropDown2.Location = new-object System.Drawing.Size(125,80)
$DropDown2.Size = new-object System.Drawing.Size(150,27)
$dropdown2.DataBindings.DefaultDataSourceUpdateMode = 0
$dropdown2.TabIndex = 5
$dropdown2.Name = "dropdown2"
$Form1.Controls.Add($DropDown2)

$DropDownLabel2 = new-object System.Windows.Forms.Label
$DropDownLabel2.Location = new-object System.Drawing.Size(50,80)
$DropDownLabel2.size = new-object System.Drawing.Size(50,27)
$DropDownLabel2.Text = "Manager:"
$Form1.Controls.Add($DropDownLabel2)

$DropDown.Location = new-object System.Drawing.Size(125,55)
$DropDown.Size = new-object System.Drawing.Size(150,27)
$dropdown.DataBindings.DefaultDataSourceUpdateMode = 0
$dropdown.TabIndex = 4
$dropdown.Name = "dropdown1"

$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(50,58)
$DropDownLabel.size = new-object System.Drawing.Size(50,27)
$DropDownLabel.Text = "Location:"

$Form1.Controls.Add($DropDown)
$Form1.Controls.Add($DropDownLabel)

ForEach ($Item in $DropDownArray) {
$DropDown.Items.Add($Item) | Out-Null
}

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null
} #end of function
GenerateForm

1 个答案:

答案 0 :(得分:2)

要进行实时更新,您需要在修改Dropbox中的文本时更新列表。当您在其中写入时,至少会触发2个事件:TextUpdateKeyPress。选一个。我建议TextUpdate,因为它会在文本更改后作出反应,而KeyPress之前会做出反应。您可以使用例如:

添加eventlistener
$DropDown2.add_TextUpdate({ Write-Host "TextUpdate. Updated text is: $($Dropdown2.Text)" })

在scriptblock中你需要运行一个查询DC的函数,但这个问题没有用ADSI,AD,LDAP等标记,这是另一个问题。

请注意,每当您更改导致DC上额外负载的字母时,这将运行查询。例如。如果你想写"马克",它会在你写完之前搜索至少4次。

我建议使用带搜索按钮的文本框,以尽量减少不必要的流量。