You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PowerShell WinForms文本框实现AD用户Tab键搜索功能?

Hey there! Let's tweak your PowerShell WinForms script to implement the Tab-triggered search and auto-fill functionality you need. Here's the optimized version with clear explanations of the key changes:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
Import-Module ActiveDirectory

# GUI Interface
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(500,400)
$Form.Text = "Benutzer Löschen"

#-----Labels-----
# Label Benutzer Löschen
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Size(200,10)
$Label.Size = New-Object System.Drawing.Size(200,20)
$Label.Text = "Benutzer Löschen"
$Label.Name = "Benutzer Löschen"
$Form.Controls.Add($Label)

#--------Buttons-------
# Button Cancel
$BTcancel = New-Object System.Windows.Forms.Button
$BTcancel.Location = New-Object System.Drawing.Size(400,200)
$BTcancel.Size = New-Object System.Drawing.Size(75,23)
$BTcancel.Text = "Cancel"
$BTcancel.Name = "Cancel"
$BTcancel.Add_Click({$Form.Close()})
$Form.Controls.Add($BTcancel)

# Button Löschen
$BTlöschen = New-Object System.Windows.Forms.Button
$BTlöschen.Location = New-Object System.Drawing.Size(320,200)
$BTlöschen.Size = New-Object System.Drawing.Size(75,23)
$BTlöschen.Text = "Löschen"
$BTlöschen.Name = "Löschen"
$Form.Controls.Add($BTlöschen)

#-----ListBox-----
# User Anzeige
$Useranzeige = New-Object System.Windows.Forms.ListBox
$Useranzeige.Location = New-Object System.Drawing.Size(90,50)
$Useranzeige.Size = New-Object System.Drawing.Size(300,100)
$Useranzeige.DisplayMember = "Name" # Set display property to user's full name
$Form.Controls.Add($Useranzeige)

#-----Textbox-----
# Suchfeld
$Suchfeld = New-Object System.Windows.Forms.TextBox
$Suchfeld.Location = New-Object System.Drawing.Size(90,160)
$Suchfeld.Size = New-Object System.Drawing.Size(300,20)
$Form.Controls.Add($Suchfeld)

#---Funktionen---
# Lade AD Benutzer in ListBox
$adUsers = Get-ADUser -Filter * -Properties Name, SamAccountName
foreach ($user in $adUsers) {
    $Useranzeige.Items.Add($user) # Add full ADUser object for reliable later use
}

# BTlöschen Click Handler
$BTlöschen.Add_Click({
    if ($Useranzeige.SelectedItem) {
        # Use SamAccountName for more reliable deletion
        Remove-ADUser -Identity $Useranzeige.SelectedItem.SamAccountName -Confirm:$false
        # Remove deleted user from ListBox
        $Useranzeige.Items.Remove($Useranzeige.SelectedItem)
        $Suchfeld.Clear()
        [System.Windows.Forms.MessageBox]::Show("Benutzer erfolgreich gelöscht!", "Erfolg", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
    } else {
        [System.Windows.Forms.MessageBox]::Show("Bitte einen Benutzer auswählen oder suchen!", "Fehler", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
    }
})

# Search & Auto-Fill on Tab Key
$Suchfeld.Add_KeyDown({
    if ($_.KeyCode -eq [System.Windows.Forms.Keys]::Tab) {
        # Prevent default Tab focus switch to handle search first
        $_.SuppressKeyPress = $true
        
        $searchText = $Suchfeld.Text.Trim()
        if (-not [string]::IsNullOrEmpty($searchText)) {
            # Find first matching user (case-insensitive fuzzy match)
            $matchedUser = $Useranzeige.Items | Where-Object { $_.Name -like "*$searchText*" } | Select-Object -First 1
            
            if ($matchedUser) {
                # Fill textbox with matched user's name
                $Suchfeld.Text = $matchedUser.Name
                # Select the matched user in ListBox for clarity
                $Useranzeige.SelectedItem = $matchedUser
            } else {
                [System.Windows.Forms.MessageBox]::Show("Kein passender Benutzer gefunden!", "Info", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
            }
        }
    }
})

[void] $Form.ShowDialog()

Key Changes Explained

  • Tab-Triggered Matching: Replaced the old Ctrl+F search with a Tab key handler that does exactly what you need:
    • $_.SuppressKeyPress = $true stops the default Tab behavior (focus switching) so we can process the search first
    • Uses fuzzy matching (-like "*$searchText*") to find the first user in the ListBox that contains your input text
    • Automatically fills the textbox with the matched username and selects it in the ListBox for better visibility
  • Robust AD User Handling: We now store full ADUser objects in the ListBox (instead of just display names) and use SamAccountName for deletion—this is more reliable than relying solely on display names which might not be unique
  • Better User Feedback: Added success/error message boxes to keep the user informed about what's happening
  • Cleaner UI: Removed unnecessary tab characters from ListBox entries for a cleaner display

内容的提问来源于stack exchange,提问作者Sandro21

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:56:26