这是我的新手,我正在尝试使用文本框字段构建Powershell GUI,并使用该文本查询AD。我的代码出了点问题。文本存储在变量$textbox_HelloWorld
。Text中,并在事件处理程序$button_ClickMe.Add_Click.
中被调用。这不会返回任何结果。如果我将变量替换为字符串,则它可以正常工作。听起来像是一个范围问题,但是我尝试使用$script:
和$global
,但都没有用。
任何帮助表示赞赏。
# import AD module
import-module activedirectory
# Load required assemblies
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Drawing form and controls
$Form_HelloWorld = New-Object System.Windows.Forms.Form
$Form_HelloWorld.Text = "Hello World"
$Form_HelloWorld.Size = New-Object System.Drawing.Size(480,240)
$Form_HelloWorld.FormBorderStyle = "FixedDialog"
$Form_HelloWorld.TopMost = $true
$Form_HelloWorld.MaximizeBox = $false
$Form_HelloWorld.MinimizeBox = $false
$Form_HelloWorld.ControlBox = $true
$Form_HelloWorld.StartPosition = "CenterScreen"
$Form_HelloWorld.Font = "Segoe UI"
# adding firstName TextBox to my form
$textbox_HelloWorld = New-Object System.Windows.Forms.TextBox
$textbox_HelloWorld.Location = New-Object System.Drawing.Size(8,4)
$textbox_HelloWorld.Size = New-Object System.Drawing.Size(120,32)
$textbox_HelloWorld.TextAlign = "MiddleCenter"
$global:textbox_HelloWorld.Text = "Enter first name!"
$Form_HelloWorld.Controls.Add($textbox_HelloWorld)
$firstnme=$textbox_HelloWorld.Text
# adding firstName label to my form
$label_HelloWorld = New-Object System.Windows.Forms.Label
$label_HelloWorld.Location = New-Object System.Drawing.Size(128,4)
$label_HelloWorld.Size = New-Object System.Drawing.Size(80,16)
$label_HelloWorld.TextAlign = "MiddleCenter"
$label_HelloWorld.Text = "First Name"
$Form_HelloWorld.Controls.Add($label_HelloWorld)
# adding lastName TextBox to my form
$Secondtextbox_HelloWorld = New-Object System.Windows.Forms.TextBox
$Secondtextbox_HelloWorld.Location = New-Object System.Drawing.Size(8,38)
$Secondtextbox_HelloWorld.Size = New-Object System.Drawing.Size(120,32)
$Secondtextbox_HelloWorld.TextAlign = "MiddleCenter"
$global:Secondtextbox_HelloWorld.Text = "Enter last name!"
$Form_HelloWorld.Controls.Add($Secondtextbox_HelloWorld)
$lastnme=$Secondtextbox_HelloWorld.Text
# adding lastName label to my form
$Secondlabel_HelloWorld = New-Object System.Windows.Forms.Label
$Secondlabel_HelloWorld.Location = New-Object System.Drawing.Size(128,38)
$Secondlabel_HelloWorld.Size = New-Object System.Drawing.Size(80,16)
$Secondlabel_HelloWorld.TextAlign = "MiddleCenter"
$Secondlabel_HelloWorld.Text = "Last Name"
$Form_HelloWorld.Controls.Add($Secondlabel_HelloWorld)
# add a button
$button_ClickMe = New-Object System.Windows.Forms.Button
$button_ClickMe.Location = New-Object System.Drawing.Size(8,80)
$button_ClickMe.Size = New-Object System.Drawing.Size(240,32)
$button_ClickMe.TextAlign = "MiddleCenter"
$button_ClickMe.Text = "Click Me!"
$button_ClickMe.Add_Click({
$button_ClickMe.Text = "Submitted"
$nme = Get-ADUser -Filter "GivenName -like '$global:textbox_HelloWorld.Text' -and Surname -like '$global:Secondtextbox_HelloWorld.Text*'" | Select-Object -ExpandProperty SamAccountName | out-string
[System.Windows.Forms.MessageBox]::Show($nme , "My Dialog Box")
})
$Form_HelloWorld.Controls.Add($button_ClickMe)
# show form
$Form_HelloWorld.Add_Shown({$Form_HelloWorld.Activate()})
[void] $Form_HelloWorld.ShowDialog()
答案 0 :(得分:0)
由于您要求用引号引起来的变量,因此将“ $ textbox_helloworld”视为变量,将“ .text”视为字符串。您需要通过$()
例如"$city.state"
将返回“ Los Angeles.state”
"$($city.state)"
将返回“加利福尼亚”的地方
也不需要全局。这是因为“全局”和“脚本”用于查找当前环境之外的变量。由于所有代码都在同一环境中,而不是在子环境(例如Function或Scriptblock)中,因此您无需调用父环境变量。
这是您要使用的。
$nme = Get-ADUser -Filter "GivenName -like '$($textbox_HelloWorld.Text)' -and Surname -like '$($Secondtextbox_HelloWorld.Text)*'" | Select-Object -ExpandProperty SamAccountName | out-string
最后一点:“ middleCenter”在Textbox上不是可行的属性,至少在我的计算机上不是。我会删除您的文本框textaligns以获得更多兼容性。
答案 1 :(得分:0)
如 Robert Cotterman 所述,由于需要为z-index
使用subexpression
作为$TextBox.Text
值,因此filter参数是一个问题。
我还建议您使用-f
格式运算符来构建这样的字符串。像这样:
$button_ClickMe.Add_Click({
$button_ClickMe.Text = "Submitted"
$query_filter = "GivenName -like '*{0}*' -and Surname -like '*{1}*'" -f $global:textbox_HelloWorld.Text, $global:Secondtextbox_HelloWorld.Text
$nme = Get-ADUser -Filter $query_filter | Select-Object -ExpandProperty SamAccountName | out-string
[System.Windows.Forms.MessageBox]::Show($nme , "My Dialog Box")
})
使用-f
运算符从格式字符串开始,然后是一个或多个对象或表达式,这些对象或表达式将转换为字符串并插入格式字符串中的指定位置。
格式字符串中的{0}
是格式项。 0是对象的索引,该对象的字符串值将在该位置插入。 (索引从0开始。)如果要插入的对象不是字符串,则在将其插入结果字符串之前,将调用其ToString方法将其转换为一个。
示例:
'{0} is an {1}' -f 'This','example'
This is an example