Winforms 从数组中筛选数组

Winforms 从数组中筛选数组,winforms,powershell,Winforms,Powershell,我正在尝试构建一个GUI,它最终将允许我们的二线团队轻松地应用查找广告帐户。到目前为止,我已经完成了,但我无法让PowerShell查找输入到文本框中的值,然后评估用户是否存在于AD中 以下是脚本: Add-Type -AssemblyName System.Windows.Forms [System.Windows.Forms.Application]::EnableVisualStyles() $Form = New-Object System.Windows

我正在尝试构建一个GUI,它最终将允许我们的二线团队轻松地应用查找广告帐户。到目前为止,我已经完成了,但我无法让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检索已输入的文本?
我快速环顾四周,希望有一种方法可以做到这一点,而无需打开和关闭另一个窗口。

您在这里指的是一个错误的对象:

$username = $Label1.text
这就是,嗯,标签。当然,您应该从
文本框中获取值:

$username = $TextBox1.text

在定义了
$Button1\u Click
操作后,您需要将其分配给
$null
事件:

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

$Form.controls.AddRange(@($Label1,$TextBox1,$Button1))
$Button1\u单击={
$username=$Label1.text
$Checkuser=Get ADUser-Identity$username
If($Checkuser-eq$null){
$Button1.Text=“找不到用户”
$button1.ForeColor=“红色”
}
Elseif($Checkuser-ne$null){
$Button1.Text=“找到用户”
}
}
$Button1.添加单击($Button1\u单击)
[void]$Form.ShowDialog()

如果要从文本框中获取用户名,请将
$username=$Label1.text
更改为
$username=$TextBox1.text

啊,我现在看到我的错误了。谢谢你的帮助。