2013-02-06 28 views
1

對於像你這樣的有經驗的人來說,這可能是一個簡單的問題,但是我怎樣才能在下拉菜單中爲活動目錄搜索用戶名?結果並將值傳遞給$varPowershell - 如何使用下拉菜單進行實時活動目錄搜索

這裏是我和我的兩個下拉菜單,我希望manager下拉列表根據我輸入的字符串在我的Active Directory中執行查找。即如果我輸入Macdonald,則下拉菜單項會在我的AD中顯示所有麥克唐納的名或姓。

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 

回答

2

要進行實時更新,您需要在保管箱中的文本被修改時更新列表。當您寫入時,至少會觸發2個事件:TextUpdateKeyPress。選一個。我建議TextUpdate,因爲它在文本被改變之後起作用,而KeyPress之前有反應。您可以添加使用例如爲:

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

在您需要運行查詢DC功能的腳本塊一個事件監聽,但所以這是另外一個問題這個問題並未標ADSI,AD,LDAP等。

請注意這將運行查詢每次您更改一個字母,這會導致額外的DC負載。例如。如果你想寫「馬克」,它會搜索至少4次,然後再寫完。

我建議使用帶有搜索按鈕的文本框來代替,以儘量減少不必要的流量。

+0

這是一個好點,我會採取你的建議搜索botton。我還會爲AD搜索創建一個新問題。感謝@Grainer,你的幫助是價格更低,對於任何其他事情有M @ stercards:P – lotirthos227