-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_process.ps1
58 lines (36 loc) · 1.68 KB
/
filter_process.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
#----------------------------------------------
#<Script name> was created by <Name> <Surname>
#<Script definition>
#----------------------------------------------
#----------------------------------------------
#--------------------Fields--------------------
#----------------------------------------------
#----------------------------------------------
#--------------------Functions-----------------
#----------------------------------------------
Function FilterProcess {
Write-Host "*Filtering by the number of handles the process has opened*" -ForegroundColor Yellow
$parameter = Read-Host "Enter maximum handles for a process "
try {
New-Item -Path . -Name "logs" -ItemType "directory" -ErrorAction Stop
}
catch {
Write-Host "Logs dir already exists, no action" -ForegroundColor Gray
}
while(1) {
Get-Process | Where-Object { $_.HANDLES -lt $parameter }
$fileNameDate = Get-Date -Format "yyyyMMDD_HHmmss"
$fileCount = ( Get-ChildItem .\logs | Measure-Object ).Count;
#deleting oldest log file
if($fileCount -eq 5) {
Get-ChildItem .\logs | Sort CreationTime | Select -First 1 | Remove-Item
}
Get-Process | Where-Object { $_.HANDLES -gt $parameter } | Format-Table Handles, Name, Id -AutoSize | `
Out-File -FilePath .\logs\FilteredProcessList_$fileNameDate.csv
Start-Sleep -Seconds 30
Write-Host "*Updating..*" -ForegroundColor Yellow
}
}
#----------------------------------------------
#-------------------Execution code-------------
#----------------------------------------------