-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote-taskkillerV2.ps1
84 lines (62 loc) · 4.11 KB
/
remote-taskkillerV2.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<#
.SYNOPSIS
Kill Process with Powershell remotly or on localhost
.DESCRIPTION
Kill Process with Powershell remotly or on localhost
.PARAMETER computername
Remote-Computername to scan for processes
.EXAMPLE
Show-Taskkiller -computername remotecomputer
.EXAMPLE
Show-Taskkiller -computername localhost
.NOTES
Test, test and test before you use it in production!
Thorsten Enderlein, 2023
Twitter: @endi24
github: https://github.com/endoleg/
#>
function Show-Taskkiller {
[CmdletBinding()]
param (
[parameter(Mandatory=$true,ValueFromPipeline=$false)]
[string]$computername
)
begin{
$DateScanned = Get-Date -Format u
Write-Information -InformationAction Continue -MessageData ("Started script at {0}" -f $DateScanned)
$stopwatch = New-Object System.Diagnostics.Stopwatch
$stopwatch.Start()
}
process{
#$ProcessArray = Get-Process -computername $computername #-IncludeUserName
$ProcessArray = Invoke-Command -ComputerName $computername -ScriptBlock {Get-Process -IncludeUserName }
$CIMProcesses = Get-CimInstance -class win32_Process -computername $computername
$CIMServices = Get-CIMinstance -class Win32_Service -computername $computername
$PerfProcArray = Get-CIMinstance -class Win32_PerfFormattedData_PerfProc_Process -computername $computername
#$colPerfs = Get-wmiobject win32_perfformatteddata_perfproc_process -computername $computername
foreach ($Process in $ProcessArray){
$Services = $CIMServices | Where-Object ProcessID -eq $Process.ID
$Services = $Services.PathName -Join "; "
$CommandLine = $CIMProcesses | Where-Object ProcessID -eq $Process.ID | Select-Object -ExpandProperty CommandLine
$PercentProcessorTime = $PerfProcArray | Where-Object IDProcess -eq $Process.ID | Select-Object -ExpandProperty PercentProcessorTime
$MemoryMB = $PerfProcArray | Where-Object IDProcess -eq $Process.ID | Select-Object -ExpandProperty workingSetPrivate
$MemoryMB = try {[Math]::Round(($MemoryMB / 1mb),2)} Catch{}
$Process | Add-Member -MemberType NoteProperty -Name "Host" -Value $computername
$Process | Add-Member -MemberType NoteProperty -Name "DateScanned" -Value $DateScanned
$Process | Add-Member -MemberType NoteProperty -Name "CommandLine" -Value $CommandLine
$Process | Add-Member -MemberType NoteProperty -Name "Services" -Value $Services
$Process | Add-Member -MemberType NoteProperty -Name "PercentProcessorTime" -Value $PercentProcessorTime
$Process | Add-Member -MemberType NoteProperty -Name "MemoryMB" -Value $MemoryMB
$Process | Add-Member -MemberType NoteProperty -Name "ModuleCount" -Value @($Process.Modules).Count
$Process | Add-Member -MemberType NoteProperty -Name "ThreadCount" -Value @($Process.Threads).Count
}
$elapsed = $stopwatch.Elapsed
Write-Verbose ("Total time elapsed: {0}" -f $elapsed) -verbose
Write-Verbose ("Ended at {0}" -f (Get-Date -Format u)) -verbose
Write-Verbose "---------------------------------------------------------------------------------------------------------------------------------------------------------" -verbose
$Processes = $ProcessArray | Select-Object Host, DateScanned, Product, Description, ProcessName, CommandLine, Services, UserName, StartTime, CPU, PercentProcessorTime, MemoryMB, BasePriority, PriorityClass, PrivateMemorySize, PrivilegedProcessorTime, Responding, SessionId, Id, PriorityBoostEnabled, Company, Path, FileVersion, ProductVersion, TotalProcessorTime, UserProcessorTime, ModuleCount, ThreadCount, MainWindowHandle, HandleCount | Out-GridView -PassThru -Title "$($perf.PSComputerName) - Select process to kill"
Invoke-Command -ComputerName $computername -ScriptBlock {param($Processes) $Processes | ForEach-Object {Stop-Process -Id $Processes.Id -force }} -ArgumentList $Processes
}
}
#Show-Taskkiller -computername localhost
Show-Taskkiller -computername remotecomputer