Advertisement
SX86

PowerShell Windows Forms Tutorial 2 : Process Manager

Jun 29th, 2017
4,566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Load required assemblies
  2. [void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
  3. [System.Windows.Forms.Application]::EnableVisualStyles()
  4.  
  5. # Start Creating Functions
  6. Function GetProcesses{
  7.  
  8.     # Reset the columns and content of listview_Processes before adding data to it.
  9.     $listview_Processes.Items.Clear()
  10.     $listview_Processes.Columns.Clear()
  11.    
  12.     # Get a list and create an array of all running processes
  13.     $Processes = Get-Process | Select Id,ProcessName,Handles,NPM,PM,WS,VM,CPU,Path
  14.    
  15.     # Compile a list of the properties stored for the first indexed process "0"
  16.     $ProcessProperties = $Processes[0].psObject.Properties
  17.  
  18.     # Create a column in the listView for each property
  19.     $ProcessProperties | ForEach-Object {
  20.         $listview_Processes.Columns.Add("$($_.Name)") | Out-Null
  21.     }
  22.  
  23.     # Looping through each object in the array, and add a row for each
  24.     ForEach ($Process in $Processes){
  25.  
  26.         # Create a listViewItem, and assign it it's first value
  27.         $ProcessListViewItem = New-Object System.Windows.Forms.ListViewItem($Process.Id)
  28.  
  29.         # For each properties, except for 'Id' that we've already used to create the ListViewItem,
  30.         # find the column name, and extract the data for that property on the current object/process
  31.         $Process.psObject.Properties | Where {$_.Name -ne "Id"} | ForEach-Object {
  32.             $ColumnName = $_.Name
  33.             $ProcessListViewItem.SubItems.Add("$($Process.$ColumnName)") | Out-Null
  34.         }
  35.  
  36.         # Add the created listViewItem to the ListView control
  37.         # (not adding 'Out-Null' at the end of the line will result in numbers outputred to the console)
  38.         $listview_Processes.Items.Add($ProcessListViewItem) | Out-Null
  39.  
  40.     }
  41.  
  42.     # Resize all columns of the listView to fit their contents
  43.     $listview_Processes.AutoResizeColumns("HeaderSize")
  44.  
  45. }
  46.  
  47. Function EndProcesses{
  48.  
  49.     # Since we allowed 'MultiSelect = $true' on the listView control,
  50.     # Compile a list in an array of selected items
  51.     $SelectedProcesses = @($listview_Processes.SelectedIndices)
  52.  
  53.     # Find which column index has an the name 'Id' on it, for the listView control
  54.     # We chose 'Id' because it is required by 'Stop-Process' to properly identify the process to kill.
  55.     $IdColumnIndex = ($listview_Processes.Columns | Where {$_.Text -eq "Id"}).Index
  56.    
  57.     # For each object/item in the array of selected item, find which SubItem/cell of the row...
  58.     $SelectedProcesses | ForEach-Object {
  59.    
  60.         # ...contains the Id of the process that is currently being "foreach'd",
  61.         $ProcessId = ($listview_Processes.Items[$_].SubItems[$IdColumnIndex]).Text
  62.        
  63.         # ...and stop it.
  64.         Stop-Process -Id $ProcessId -Confirm:$false -Force -WhatIf
  65.  
  66.         # The WhatIf switch was used to simulate the action. Remove it to use cmdlet as per normal.
  67.  
  68.     }
  69.  
  70.     # Refresh your process list, once you are done stopping them
  71.     GetProcesses
  72.    
  73. }
  74.  
  75.  
  76. # Drawing form and controls
  77. $Form_HelloWorld = New-Object System.Windows.Forms.Form
  78.     $Form_HelloWorld.Text = “Process Manager”
  79.     $Form_HelloWorld.Size = New-Object System.Drawing.Size(832,528)
  80.     $Form_HelloWorld.FormBorderStyle = "FixedDialog"
  81.     $Form_HelloWorld.TopMost  = $true
  82.     $Form_HelloWorld.MaximizeBox  = $true
  83.     $Form_HelloWorld.MinimizeBox  = $true
  84.     $Form_HelloWorld.ControlBox = $true
  85.     $Form_HelloWorld.StartPosition = “CenterScreen”
  86.     $Form_HelloWorld.Font = "Segoe UI"
  87.  
  88.  
  89. # Adding a label control to Form
  90. $label_HelloWorld = New-Object System.Windows.Forms.Label
  91.     $label_HelloWorld.Location = New-Object System.Drawing.Size(8,8)
  92.     $label_HelloWorld.Size = New-Object System.Drawing.Size(240,32)
  93.     $label_HelloWorld.TextAlign = "MiddleLeft"
  94.     $label_HelloWorld.Text = “Processes:”
  95.         $Form_HelloWorld.Controls.Add($label_HelloWorld)
  96.  
  97.  
  98. # Adding a listView control to Form, which will hold all process information
  99. $Global:listview_Processes = New-Object System.Windows.Forms.ListView
  100.     $listview_Processes.Location = New-Object System.Drawing.Size(8,40)
  101.     $listview_Processes.Size = New-Object System.Drawing.Size(800,402)
  102.     $listview_Processes.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor
  103.     [System.Windows.Forms.AnchorStyles]::Right -bor
  104.     [System.Windows.Forms.AnchorStyles]::Top -bor
  105.     [System.Windows.Forms.AnchorStyles]::Left
  106.     $listview_Processes.View = "Details"
  107.     $listview_Processes.FullRowSelect = $true
  108.     $listview_Processes.MultiSelect = $true
  109.     $listview_Processes.Sorting = "None"
  110.     $listview_Processes.AllowColumnReorder = $true
  111.     $listview_Processes.GridLines = $true
  112.     $listview_Processes.Add_ColumnClick({SortListView $_.Column})
  113.         $Form_HelloWorld.Controls.Add($listview_Processes)
  114.  
  115.  
  116. # Adding a button control to Form
  117. $button_GetProcess = New-Object System.Windows.Forms.Button
  118.     $button_GetProcess.Location = New-Object System.Drawing.Size(8,450)
  119.     $button_GetProcess.Size = New-Object System.Drawing.Size(240,32)
  120.     $button_GetProcess.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor
  121.     [System.Windows.Forms.AnchorStyles]::Left
  122.     $button_GetProcess.TextAlign = "MiddleCenter"
  123.     $button_GetProcess.Text = “Refresh Process List”
  124.     $button_GetProcess.Add_Click({GetProcesses})
  125.         $Form_HelloWorld.Controls.Add($button_GetProcess)
  126.  
  127.  
  128. # Adding another button control to Form
  129. $button_EndProcess = New-Object System.Windows.Forms.Button
  130.     $button_EndProcess.Location = New-Object System.Drawing.Size(568,450)
  131.     $button_EndProcess.Size = New-Object System.Drawing.Size(240,32)
  132.     $button_EndProcess.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor
  133.     [System.Windows.Forms.AnchorStyles]::Right
  134.     $button_EndProcess.TextAlign = "MiddleCenter"
  135.     $button_EndProcess.Text = “End Selected Process(es)
  136.     $button_EndProcess.Add_Click({EndProcesses})
  137.         $Form_HelloWorld.Controls.Add($button_EndProcess)
  138.  
  139.  
  140.  
  141. # Show form with all of its controls
  142. $Form_HelloWorld.Add_Shown({$Form_HelloWorld.Activate();GetProcesses})
  143. [Void] $Form_HelloWorld.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement