我正在尝试构建一个GUI,该GUI最终将使我们的第二线团队能够轻松地应用查找AD帐户。到目前为止,我仍然无法使用PowerShell来查找在文本框中输入的值,然后评估用户是否存在于AD中。
这是脚本:
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object System.Windows.Forms.Form
$Form.ClientSize = '400,400'
$Form.Text = "Add DXE Mailbox Permissions"
$Form.TopMost = $false
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Text = "Username"
$Label1.AutoSize = $true
$Label1.Width = 25
$Label1.Height = 10
$Label1.Location = New-Object System.Drawing.Point(15, 145)
$Label1.Font = 'Microsoft Sans Serif,10'
$TextBox1 = New-Object System.Windows.Forms.TextBox
$TextBox1.Multiline = $false
$TextBox1.Width = 168
$TextBox1.Height = 20
$TextBox1.Location = New-Object System.Drawing.Point(15, 165)
$TextBox1.Font = 'Microsoft Sans Serif,10'
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Text = "Check Username"
$Button1.Width = 120
$Button1.Height = 30
$Button1.Location = New-Object System.Drawing.Point(199, 162)
$Button1.Font = 'Microsoft Sans Serif,10'
$Button1.Add_Click($Button1_Click)
$Form.Controls.AddRange(@($Label1, $TextBox1, $Button1))
$Button1_Click = {
$username = $Label1.Text
$Checkuser = Get-ADUser -Identity $username
if ($Checkuser -eq $null) {
$Button1.Text = "Can't Find User"
$button1.ForeColor = "Red"
} elseif ($Checkuser -ne $null) {
$Button1.Text = "Found User"
}
}
[void]$Form.ShowDialog()
我认为我遇到的问题与$username = $Label1.Text
行有关。我不确定是否应该将$Label1.Text
分配给一个变量,如果可以,我将如何使PowerShell检索已输入的文本?
我快速浏览了一下,希望有一种方法可以在不打开和关闭另一个窗口的情况下进行。
答案 0 :(得分:1)
您在这里指的是错误的对象
$username = $Label1.text
那是标签。当然,您应该从TextBox
中获取值:
$username = $TextBox1.text
答案 1 :(得分:1)
您需要在定义之后 分配$Button1_Click
动作-否则,您只是将$null
分配给Click
事件:>
<# define controls here ... #>
$Form.controls.AddRange(@($Label1,$TextBox1,$Button1))
$Button1_Click = {
$username = $Label1.text
$Checkuser = Get-ADUser -Identity $username
If($Checkuser -eq $null){
$Button1.Text = "Can't Find User"
$button1.ForeColor = "Red"
}
Elseif($Checkuser -ne $null){
$Button1.Text = "Found User"
}
}
$Button1.Add_click($Button1_Click)
[void]$Form.ShowDialog()
如果要从文本框中获取用户名,请将$username = $Label1.text
更改为$username = $TextBox1.Text