[Void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.Application]::EnableVisualStyles()
Function Main {
$Form1 = New-Object System.Windows.Forms.Form
$Form1.Size = New-Object System.Drawing.Size 441,450 #387
$Form1.Text = "Remote Task Manager"
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Anchor = "Bottom, Right"
$Button1.Location = New-Object System.Drawing.Point 258, 345
$Button1.Size = New-Object System.Drawing.Size 75, 23
$Button1.TabIndex = 1
$Button1.Text = "Refresh"
$Button1.Add_Click({Refresh_Form})
$Form1.Controls.Add($Button1)
$Button2 = New-Object System.Windows.Forms.Button
$Button2.Anchor = "Bottom, Right"
$Button2.Location = New-Object System.Drawing.Point 339, 345
$Button2.Size = New-Object System.Drawing.Size 75, 23
$Button2.TabIndex = 2
$Button2.Text = "Kill"
$Button2.Add_Click({Kill_Process})
$Form1.Controls.Add($Button2)
$ComboBox1 = New-Object System.Windows.Forms.ComboBox
$ComboBox1.Anchor = "Bottom, Left, Right"
$ComboBox1.FormattingEnabled = $True
$ComboBox1.Location = New-Object System.Drawing.Point 12, 346
$ComboBox1.Size = New-Object System.Drawing.Size 240, 21
$ComboBox1.TabIndex = 3
$ComboBox1.FlatStyle = "System"
$Form1.Controls.Add($ComboBox1)
$ListView1 = New-Object System.Windows.Forms.ListView
$listView1.Anchor = "Bottom, Top, Left, Right"
$ListView1.FullRowSelect = $True
$ListView1.Location = New-Object System.Drawing.Point 12, 12
$ListView1.Size = New-Object System.Drawing.Size 402, 317 #274
$ListView1.TabIndex = 0
$ListView1.View = "Details"
[void] $ListView1.Columns.Add("Process", 125)
[void] $ListView1.Columns.Add("User Name", 117)
[void] $ListView1.Columns.Add("PID", 36)
[void] $ListView1.Columns.Add("Memory", 103, "Right")
$Form1.Controls.Add($ListView1)
$StatusStrip1 = New-Object System.Windows.Forms.StatusStrip
$StatusStrip1.Location = New-Object System.Drawing.Point 12, 294
$StatusStrip1.Size = New-Object System.Drawing.Size 426, 22
$StatusStrip1.SizingGrip = $False
$StatusStrip1.TabIndex = 4
$StatusStrip1.Text = "StatusStrip1"
$Form1.Controls.Add($StatusStrip1)
$ToolStripStatusLabel1 = New-Object System.Windows.Forms.ToolStripStatusLabel
$ToolStripStatusLabel1.Size = New-Object System.Drawing.Size 0, 17
[void] $StatusStrip1.Items.Add($ToolStripStatusLabel1)
$Form1.Add_Load({Load_Form})
[void] $Form1.ShowDialog()
}
Function Load_Form {
$strComputer = [System.Environment]::GetEnvironmentVariable("ComputerName")
#[System.Windows.Forms.MessageBox]::Show("$strComputerName")
$ComboBox1.Text = $strComputer
Refresh_Form
}
Function Kill_Process {
$intPID = $($ListView1.SelectedItems[0].SubItems[2].text)
$objProcess = Get-WmiObject -class Win32_Process -namespace "root\CIMV2" -computername $ComboBox1.Text -Filter "ProcessID=$intPID"
Try {
$objProcess.Terminate()
}
Catch [System.Management.Automation.RuntimeException]{
$ToolStripStatusLabel1.Text = "Process: $($PID) could not be terminated"
}
$ToolStripStatusLabel1.Text = "Process: $($PID) Terminated!"
Refresh_Form
}
Function Refresh_Form {
$ToolStripStatusLabel1.Text = "Listing Processes of $($ComboBox1.Text) ..."
$ListView1.BeginUpdate()
$ListView1.Items.Clear()
Try {
$colItems = Get-WMIObject -class "Win32_Process" -namespace "root\CIMV2" -computername $ComboBox1.Text
ForEach ($objItem in $colItems) {
$lvwItem = $ListView1.Items.Add($objItem.Name)
If ($objItem.Name -notlike "system*") {
$lvwItem.SubItems.Add($objItem.GetOwner().User)
}
Else {
$lvwItem.SubItems.Add("SYSTEM")
}
$lvwItem.SubItems.Add($objItem.ProcessId)
$lvwItem.SubItems.Add($objItem.WorkingSetSize /1024 /2)
}
$ListView1.Sorting = "Ascending"
$ListView1.Sort()
If (-Not $ComboBox1.Items.Contains($ComboBox1.Text)) {
$ComboBox1.Items.Add($ComboBox1.Text)
}
$ToolStripStatusLabel1.Text = "Processes: $($ListView1.Items.Count)"
}
Catch [System.Exception] {
$ToolStripStatusLabel1.Text = "Connection to $($ComboBox1.Text) could not be made"
}
$ListView1.EndUpdate()
}
Main