Приклад виводу користувачів по фільтру "usr", з кнопкою Refresh:
![file](/uploads/image-1667755188507.png)
```
Add-Type -assembly System.Windows.Forms
$Header = "USERNAME", "SESSIONNAME", "ID", "STATE", "IDLETIME", "LOGONTIME"
$dlgForm = New-Object System.Windows.Forms.Form
$dlgForm.Text ='Session Connect'
$dlgForm.Width = 400
$dlgForm.AutoSize = $true
$dlgBttn = New-Object System.Windows.Forms.Button
$dlgBttn.Text = 'Control'
$dlgBttn.Location = New-Object System.Drawing.Point(15,10)
$dlgBttnsec = New-Object System.Windows.Forms.Button
$dlgBttnsec.Text = 'Refresh'
$dlgBttnsec.Location = New-Object System.Drawing.Point(145,10)
$dlgForm.Controls.Add($dlgBttn)
$dlgForm.Controls.Add($dlgBttnsec)
$dlgList = New-Object System.Windows.Forms.ListView
$dlgList.Location = New-Object System.Drawing.Point(0,50)
$dlgList.Width = $dlgForm.ClientRectangle.Width
$dlgList.Height = $dlgForm.ClientRectangle.Height
$dlgList.Anchor = "Top, Left, Right, Bottom"
$dlgList.MultiSelect = $False
$dlgList.View = 'Details'
$dlgList.FullRowSelect = 1;
$dlgList.GridLines = 1
$dlgList.Scrollable = 1
$dlgForm.Controls.add($dlgList)
# Add columns to the ListView
foreach ($column in $Header){
$dlgList.Columns.Add($column) | Out-Null
}
$([Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding("utf-8"))
$(quser.exe | findstr "usr*") -replace "^[\s>]" , "" -replace "\s+" , "," | ConvertFrom-Csv -Header $Header | ForEach-Object {
$dlgListItem = New-Object System.Windows.Forms.ListViewItem($_.USERNAME)
$dlgListItem.Subitems.Add($_.SESSIONNAME) | Out-Null
$dlgListItem.Subitems.Add($_.ID) | Out-Null
$dlgListItem.Subitems.Add($_.STATE) | Out-Null
$dlgListItem.Subitems.Add($_.IDLETIME) | Out-Null
$dlgListItem.Subitems.Add($_.LOGONTIME) | Out-Null
$dlgList.Items.Add($dlgListItem) | Out-Null
}
$dlgBttn.Add_Click(
{
$SelectedItem = $dlgList.SelectedItems[0]
if ($SelectedItem -eq $null){
[System.Windows.Forms.MessageBox]::Show("Выберите сессию для подключения")
}else{
$session_id = $SelectedItem.subitems[2].text
$(mstsc /shadow:$session_id /noConsentPrompt)
#[System.Windows.Forms.MessageBox]::Show($session_id)
}
}
)
$dlgBttnsec.Add_Click(
{
$dlgList.Items.Clear()
$([Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding("utf-8"))
$(quser.exe | findstr "usr") -replace "^[\s>]" , "" -replace "\s+" , "," | ConvertFrom-Csv -Header $Header | ForEach-Object {
$dlgListItem = New-Object System.Windows.Forms.ListViewItem($_.USERNAME)
$dlgListItem.Subitems.Add($_.SESSIONNAME) | Out-Null
$dlgListItem.Subitems.Add($_.ID) | Out-Null
$dlgListItem.Subitems.Add($_.STATE) | Out-Null
$dlgListItem.Subitems.Add($_.IDLETIME) | Out-Null
$dlgListItem.Subitems.Add($_.LOGONTIME) | Out-Null
$dlgList.Items.Add($dlgListItem) | Out-Null
}
}
)
$dlgForm.ShowDialog()
```