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



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

列表视图变量is = $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()

为这个基本问题道歉。只是试着创建表单。

设置listview的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)
})

你确定一个普通的网格视图不够吗?

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

相关内容

  • 没有找到相关文章

最新更新