Powershell - 根据dropdown.text值获取广告用户信息

时间:2013-02-08 15:02:16

标签: powershell drop-down-menu active-directory ldap

我大家,

我已经尽力在点击按钮上处理LDAP查询,但我可以使用dropdown2.text来搜索名称而不是将complet名称设置为“smith,peter”。

我想要的是,如果我在“管理员”字段中输入任何名字或姓氏,点击按钮将会搜索并返回Active Directory事件中的所有相应值(如果它是名称或姓氏)。我不远,但我找不到我的错误

function GenerateForm {

set-Location c:\
add-PSSnapin  quest.activeroles.admanagement

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

$form1 = New-Object System.Windows.Forms.Form
$searchldap = New-Object System.Windows.Forms.Button
$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

#Handler 
$handler_searchldap_click= 
{
    $manager = ""
    $dropdown2.items.clear()
    $manager = get-aduser  -f {name -like $dropdown2.text}
    foreach ($thing in $manager){
        $useraccountname = $thing.name
        $DropDown2.Items.Add($useraccountname) | Out-Null
    }
}




$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"
$DropDown2.add_TextUpdate({ Write-Host "TextUpdate. Updated text is: $($Dropdown2.Text)" })
$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
}

###Button section

$searchldap.Location = New-Object System.Drawing.Size(275,80)
$searchldap.Size = New-Object System.Drawing.Size(100,20)
$searchldap.Text = "Search"
$searchldap.Add_Click($handler_searchldap_click)

$Form1.Controls.Add($searchldap)

#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)

试试这个:

$filter = [scriptblock]::Create("Name -like '*$($dropdown2.text)*'")
$manager = get-aduser  -f $filter

Powershell在将过滤器脚本块传递给DC之前(通过AD网关服务)不会扩展过滤器脚本块中使用的局部变量。要解决此问题,请从可扩展字符串

创建过滤器脚本块
相关问题