I'm new to WinForm and I need help concerning filtering.
I created a GUI that list the TS active session of my servers and allow me to take control of a session using a "connect" button.
The list is working, the connect button too, but I'm trying to create an interactive filter textbox, I want that when I enter "r" in my textbox, all ServerName or Username or any column containing "r" are directly filtered.
How can I implement that ?
Thanks for reading me
My code :
Code: Select all
$ErrorActionPreference = 'SilentlyContinue'
Import-Module PSTerminalServices
function GetTsSession{
$servers = (Get-ADComputer -filter * | where {$_.name -like '*PUT_SERVERNAME_HERE*'}).DNSHostName | sort-object
#Active Session List
$Result = foreach ($serv in $servers){
if(Test-Connection $serv -Count 1 )
{
Get-TSSession -ComputerName $serv -State Active
}
Else {
#Write-Host "No Ping :" $serv
}
}
$Result | select @{n='Server';e={$_.server.ServerName}},SessionID,ClientName,Username
}
function RemoteControl {
$srv=$dataGridView1.CurrentRow.Cells['server'].Value
$id=$dataGridView1.CurrentRow.Cells['sessionid'].Value
mstsc /v:$srv /shadow:$id /control
}
#This filter function reload the entire datagridview1...
function funfilter {
$TBfilter=$textBox.text
GetTsSession | where-object {$_ -like "*$TBfilter*"}
}
function Call-MainForm_pff {
#----------------------------------------------
#region Import the Assemblies
#----------------------------------------------
[void][reflection.assembly]::Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
[void][reflection.assembly]::Load("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
#endregion Import Assemblies
#----------------------------------------------
#region Generated Form Objects
#----------------------------------------------
[System.Windows.Forms.Application]::EnableVisualStyles()
$MainForm = New-Object 'System.Windows.Forms.Form'
$dataGridView1 = New-Object 'System.Windows.Forms.DataGridView'
$refreshButton = New-Object 'System.Windows.Forms.Button'
$connectionButton = New-Object 'System.Windows.Forms.Button'
$textBox = New-Object 'System.Windows.Forms.TextBox'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
#endregion Generated Form Objects
#----------------------------------------------
# User Generated Script
#----------------------------------------------
$OnLoadFormEvent={
#TODO: Initialize Form Controls here
}
$refreshButton_Click={
#the filter function is disabled here
#$list=[system.Collections.ArrayList](funfilter)
$list=[system.Collections.ArrayList](GetTsSession)
$dataGridView1.DataSource=$list
}
$remoteButton_Click={
$list=[system.Collections.ArrayList](RemoteControl)
}
$textBox_KeyDown = [System.Windows.Forms.KeyEventHandler]{
if ($_.KeyCode -eq 'Enter')
{
$refreshButton_Click.Invoke()
#Suppress sound from unexpected use of enter on keyPress/keyUp
$_.SuppressKeyPress = $true
}
}
# --End User Generated Script--
#----------------------------------------------
#region Generated Events
#----------------------------------------------
$Form_StateCorrection_Load=
{
#Correct the initial state of the form to prevent the .Net maximized form issue
$MainForm.WindowState = $InitialFormWindowState
}
$Form_Cleanup_FormClosed=
{
#Remove all event handlers from the controls
try
{
$refreshButton.remove_Click($refreshButton_Click)
$remoteButton.remove_Click($remoteButton_Click)
$textBox.remove_Keydown($textBox_KeyDown)
$MainForm.remove_Load($OnLoadFormEvent)
$MainForm.remove_Load($Form_StateCorrection_Load)
$MainForm.remove_FormClosed($Form_Cleanup_FormClosed)
}
catch [Exception]
{ }
}
#endregion Generated Events
#----------------------------------------------
#region Generated Form Code
#----------------------------------------------
#
# MainForm
#
$MainForm.Controls.Add($dataGridView1)
$MainForm.Controls.Add($refreshButton)
$MainForm.Controls.Add($connectionButton)
$MainForm.Controls.Add($textBox)
$MainForm.ClientSize = '520, 900'
$MainForm.Name = "MainForm"
$MainForm.StartPosition = 'CenterScreen'
$MainForm.Text = "Remote Control GUI"
$MainForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$MainForm.MaximizeBox = $False
$MainForm.add_Load($OnLoadFormEvent)
#
# datagridview1
#
$dataGridView1.ColumnHeadersHeightSizeMode = 'AutoSize'
$dataGridView1.Location = '10, 50'
$dataGridView1.Name = "datagridview1"
$dataGridView1.Size = '500, 840'
$dataGridView1.AutoSizeColumnsMode = 'AllCells'
$dataGridView1.AllowUserToResizeRows = $false
$dataGridView1.MultiSelect = $false
$dataGridView1.AllowUserToAddRows = $false
$dataGridView1.ReadOnly = $true
$dataGridView1.SelectionMode = 'FullRowSelect'
#
# refreshButton
#
$refreshButton.Location = '10,24'
$refreshButton.Name = "refreshButton"
$refreshButton.Text = "Search"
$refreshButton.UseVisualStyleBackColor = $True
$refreshButton.add_Click($refreshButton_Click)
#
# connectionButton
#
$connectionButton.Location = '90,24'
$connectionButton.Name = "connectionButton"
$connectionButton.Text = "Connection"
$connectionButton.UseVisualStyleBackColor = $True
$connectionButton.add_Click($remoteButton_Click)
#
# textBox
#
$textBox.Location = '10, 1'
$textBox.Name = "textBox"
$textBox.Size = '500,22'
$textBox.UseVisualStyleBackColor = $True
$textBox.add_Keydown($textBox_KeyDown)
#endregion Generated Form Code
#----------------------------------------------
#Save the initial state of the form
$InitialFormWindowState = $MainForm.WindowState
#Init the OnLoad event to correct the initial state of the form
$MainForm.add_Load($Form_StateCorrection_Load)
#Clean up the control events
$MainForm.add_FormClosed($Form_Cleanup_FormClosed)
#Show the Form
return $MainForm.ShowDialog()
} #End Function
#Call the form
Call-MainForm_pff | Out-Null