如何使用 Windows 表单在列表视图中显示 PowerShell 输出

How to display a PowerShell output in List view using Windows Forms

如何将 Get-ADUser -Filter "GivenName -eq '$UserList' -or Surname -eq '$UserList'" | select Name, SamAccountName 输出到列表视图。

列表视图变量为 = $lwOutput

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = New-Object System.Drawing.Point(547,386)
$Form.text                       = "Form"
$Form.TopMost                    = $false

$tbName                          = New-Object system.Windows.Forms.TextBox
$tbName.multiline                = $false
$tbName.width                    = 203
$tbName.height                   = 20
$tbName.location                 = New-Object System.Drawing.Point(218,37)
$tbName.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$lblName                         = New-Object system.Windows.Forms.Label
$lblName.text                    = "Enter First Name or Last Name"
$lblName.AutoSize                = $true
$lblName.width                   = 25
$lblName.height                  = 10
$lblName.location                = New-Object System.Drawing.Point(14,40)
$lblName.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$lwOutput                        = New-Object system.Windows.Forms.ListView
$lwOutput.text                   = "listView"
$lwOutput.width                  = 476
$lwOutput.height                 = 119
$lwOutput.location               = New-Object System.Drawing.Point(32,115)

$DGVOutput                       = New-Object system.Windows.Forms.DataGridView
$DGVOutput.width                 = 480
$DGVOutput.height                = 129
$DGVOutput.location              = New-Object System.Drawing.Point(31,248)

$btnSearch                       = New-Object system.Windows.Forms.Button
$btnSearch.text                  = "Search"
$btnSearch.width                 = 60
$btnSearch.height                = 30
$btnSearch.location              = New-Object System.Drawing.Point(361,71)
$btnSearch.Font                  = New-Object System.Drawing.Font('Microsoft Sans Serif',10,[System.Drawing.FontStyle]([System.Drawing.FontStyle]::Bold))

$Form.controls.AddRange(@($tbName,$lblName,$lwOutput,$DGVOutput,$btnSearch))

$btnSearch.Add_Click({ btnGetUsers })

function btnGetUsers { 


$UserList = $tbName.Text
Get-ADUser -Filter "GivenName -eq '$UserList' -or Surname -eq '$UserList'" | select Name, SamAccountName 



}


#Write your logic code here

[void]$Form.ShowDialog()

对于这个基本问题深表歉意。只是尝试创建表单。

您确定纯网格视图不够吗?

Get-ADUser -Filter "GivenName -eq '$UserList' -or Surname -eq '$UserList'" | select Name, SamAccountName | Out-GridView

将列表视图的 View 属性设置为 Details 并添加两列:

# add this to the ListView
$lwOutput.View                   = 'Details'
$null = $lwOutput.Columns.Add('Name',326)
$null = $lwOutput.Columns.Add('SamAccountName',150)

然后像这样创建 btnGetUsers 函数:

function btnGetUsers { 
    # remove whatever was in the ListView before
    $lwOutput.Items.Clear()
    # search for the user(s) and in a loop add the properties you need to the ListView
    Get-ADUser -Filter "GivenName -eq '$($tbName.Text)' -or Surname -eq '$($tbName.Text)'" | 
    ForEach-Object {
        $lvi = [System.Windows.Forms.ListViewItem]::new()
        $lvi.Text = $_.Name
        $lvi.SubItems.Add($_.SamAccountName)
        $lwOutput.Items.Add($lvi)
    }
}

要读回 ListView 项目,例如当用户选择一个项目时,您可以像下面的演示那样做:

向 ListView 添加另一个事件处理程序以对 SelectedIndexChanged 事件作出反应:

$lwOutput.Add_SelectedIndexChanged({
    # get the item(s) that were selected in a variable as objects
    # inside the eventhandler, you can refer to the current object using automatic variable '$this'

    $selected = $this.SelectedItems | 
        Select-Object @{Name = 'Name'; Expression = {$_.Text}},
                      @{Name = 'SamAccountName'; Expression = {$_.SubItems[1].Text}}
    # for demo, just output to the console window
    Write-Host ($selected | Format-Table -AutoSize | Out-String)
})