2013-02-08 24 views
0

我的乡亲,PowerShell的 - 获取基于一个dropdown.text值的广告用户信息

我已经尽我所能,处理上的一个按钮点击一个LDAP查询,但我可以得到dropdown2.text来搜索名字,而我把完整的名字,如“史密斯,彼得”。

我想要的是,如果我将任何名字或姓氏放在“经理”字段中,单击按钮将搜索并返回所有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 
+0

我不熟悉quest-cmdlet。你有没有试过用:Get-ADUser -DisplayName * $($ dropdown2.text)*“'?这个问题取决于你在AD中使用的字段,但是'displayname'通常是全名,比如“Lastname FirstName” – 2013-02-08 15:15:22

+0

@Graimer'get-aduser'不是Quest cmdlet,而是'get-qaduser'是..;) – 2013-02-08 15:30:03

+0

那么这就是他的第一个错误,因为他加载了任务管理单元,而不是微软的AD模块:P – 2013-02-08 15:38:18

回答

2

试试这个:它把它们传递给前

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

PowerShell不会扩大在过滤器脚本块使用的局部变量DC(通过AD网关服务)。为了解决这个问题,从可扩展字符串创建过滤器脚本块

+0

谢谢,这是我需要阅读的东西。但它像一个魅力。谢谢 – lotirthos227 2013-02-08 16:23:34

相关问题