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

Posted

技术标签:

【中文标题】如何使用 Windows 窗体在列表视图中显示 PowerShell 输出【英文标题】:How to display a PowerShell output in List view using Windows Forms 【发布时间】:2021-11-17 16:39:30 【问题描述】:

如何将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()

为基本问题道歉。只是尝试创建表单。

【问题讨论】:

【参考方案1】:

将列表视图的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)
)

【讨论】:

太棒了。这正是我所追求的。谢谢你。一个快速的问题,是否可以从列表视图中“复制”字段。目前看来我无法在单元格中复制和粘贴任何数据。难道不能使用列表视图吗? @Atheek 好问题。要读回列表视图中的项目,您需要从字面上反转您最初用于添加它们的方法。对于演示,我添加了代码以通过单击其中一项来获取用户选择的任何内容。附言完成表格后不要忘记$Form.Dispose(),否则它会留在记忆中..【参考方案2】:

您确定普通的网格视图还不够吗?

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

【讨论】:

谢谢丹尼斯,这是我以前做的。但我想要实现的是从此输出中复制某些单元格。但是使用 out-GridView 是不可能的。看起来甚至 ListView 的行为都是一样的。 是的。只需使用-PassThru,所有标记的单元格将在关闭时返回到管道中。

以上是关于如何使用 Windows 窗体在列表视图中显示 PowerShell 输出的主要内容,如果未能解决你的问题,请参考以下文章

显示扭曲图像的列表视图控件

如何使用Visual Studio 2017在Windows窗体应用程序中查看和编辑Visual Basic Power Pack形状?

Windows窗体 - 如何获取列表框的垂直滚动条?

Windows 窗体嵌入式进度条后台工作者

ListView窗体的增删改查

如何获取 Vista/Windows7 等列表视图