-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpoolmon-powershell.ps1
More file actions
205 lines (190 loc) · 6.18 KB
/
poolmon-powershell.ps1
File metadata and controls
205 lines (190 loc) · 6.18 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<#
.SYNOPSIS
.
.DESCRIPTION
View kernel memory pool tag information
.PARAMETER tags
comma separated list of tags to display
.PARAMETER values
comma separated list of values to display
.PARAMETER sortvalue
value to sort by
.PARAMETER sortdir
direction to sort (ascending|descending)
.PARAMETER top
top X records to display
.PARAMETER view
output view (table|csv|grid)
.PARAMETER tagfile
file containing tag information
.PARAMETER loop
loop interval in seconds
.EXAMPLE
.\poolmon-powershell.ps1 -tags FMfn -values DateTime,Tag,PagedUsedBytes,Binary,Description -tagfile pooltag.txt -loop 5 -view csv
"DateTime","Tag","PagedUsedBytes","Binary","Description"
"2019-07-24T12:21:57","FMfn","199922400","fltmgr.sys","NAME_CACHE_NODE structure"
"2019-07-24T12:22:02","FMfn","199941136","fltmgr.sys","NAME_CACHE_NODE structure"
"2019-07-24T12:22:07","FMfn","199878016","fltmgr.sys","NAME_CACHE_NODE structure"
#>
param (
[string]$tags,
[string]$values,
[string]$sortvalue = 'TotalUsed',
[string]$sortdir = 'Descending',
[int]$top = 0,
[string]$view = 'table',
[string]$tagfile = 'pooltag.txt',
[int]$loop = 0
)
Add-Type -TypeDefinition @'
using System;
using System.Runtime.InteropServices;
namespace Win32 {
public enum NT_STATUS
{
STATUS_SUCCESS = 0x00000000,
STATUS_BUFFER_OVERFLOW = unchecked((int)0x80000005),
STATUS_INFO_LENGTH_MISMATCH = unchecked((int)0xC0000004)
}
public enum SYSTEM_INFORMATION_CLASS
{
SystemPoolTagInformation = 22,
}
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEM_POOLTAG
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] public byte[] Tag;
public uint PagedAllocs;
public uint PagedFrees;
public System.IntPtr PagedUsed;
public uint NonPagedAllocs;
public uint NonPagedFrees;
public System.IntPtr NonPagedUsed;
}
public class PInvoke {
[DllImport("ntdll.dll")]
public static extern NT_STATUS NtQuerySystemInformation(
[In] SYSTEM_INFORMATION_CLASS SystemInformationClass,
[In] System.IntPtr SystemInformation,
[In] int SystemInformationLength,
[Out] out int ReturnLength);
}
}
'@
Function Get-Pool() {
if ($tagfile) {
if (Test-Path $tagfile) {
$tagFileHash = $null
$tagFileHash = new-object System.Collections.Hashtable
foreach($line in Get-Content $tagfile) {
if(($line.trim() -ne '') -and ($line.trim() -like '*-*-*') -and ($line.trim().SubString(0,2) -ne '//') -and ($line.trim().SubString(0,3) -ne 'rem')){
$t,$b,$d = $line.split('-')
$t = $t.trim()
$b = $b.trim()
$d = $d.trim()
if (!($tagFileHash.containsKey($t))) {
$tagFileHash.Add($t,"$b|$d")
}
}
}
}
}
$ptrSize = 0
while ($true) {
[IntPtr]$ptr = [System.Runtime.InteropServices.Marshal]::AllocHGlobal($ptrSize)
$ptrLength = New-Object Int
$tagInfo = [Win32.PInvoke]::NtQuerySystemInformation([Win32.SYSTEM_INFORMATION_CLASS]::SystemPoolTagInformation, $ptr, $ptrSize, [ref]$ptrLength)
if ($tagInfo -eq [Win32.NT_STATUS]::STATUS_INFO_LENGTH_MISMATCH) {
[System.Runtime.InteropServices.Marshal]::FreeHGlobal($ptr)
$ptrSize = [System.Math]::Max($ptrSize,$ptrLength)
}
elseif ($tagInfo -eq [Win32.NT_STATUS]::STATUS_SUCCESS) {
break
}
else {
[System.Runtime.InteropServices.Marshal]::FreeHGlobal($ptr)
"An error occurred getting SystemPoolTagInformation"
return
}
}
$tags = $tags -Split ','
$datetime = Get-Date
$systemPoolTag = New-Object Win32.SYSTEM_POOLTAG
$systemPoolTag = $systemPoolTag.GetType()
$size = [System.Runtime.InteropServices.Marshal]::SizeOf([type]([Win32.SYSTEM_POOLTAG]))
$offset = $ptr.ToInt64()
$count = [System.Runtime.InteropServices.Marshal]::ReadInt32($offset)
$offset = $offset + [System.IntPtr]::Size
for ($i=0; $i -lt $count; $i++){
$entryPtr = New-Object System.Intptr -ArgumentList $offset
$entry = [system.runtime.interopservices.marshal]::PtrToStructure($entryPtr,[type]$systemPoolTag)
$tag = [System.Text.Encoding]::Default.GetString($entry.Tag)
if (!$tags -or ($tags -and $tags -contains $tag)) {
$tagResult = $null
$tagResult = [PSCustomObject]@{
DateTime = Get-Date -Format s $datetime
DateTimeUTC = Get-Date -Format s $datetime.ToUniversalTime()
Tag = $tag
PagedAllocs = [int64]$entry.PagedAllocs
PagedFrees = [int64]$entry.PagedFrees
PagedDiff = [int64]$entry.PagedAllocs - [int64]$entry.PagedFrees
PagedUsedBytes = [int64]$entry.PagedUsed
NonPagedAllocs = [int64]$entry.NonPagedAllocs
NonPagedFrees = [int64]$entry.NonPagedFrees
NonPagedDiff = [int64]$entry.NonPagedAllocs - [int64]$entry.NonPagedFrees
NonPagedUsedBytes = [int64]$entry.NonPagedUsed
TotalUsedBytes = [int64]$entry.PagedUsed + [int64]$entry.NonPagedUsed
}
if ($tagFileHash) {
if ($tagFileHash.containsKey($tag)) {
$Bin,$BinDesc = $tagFileHash.$tag.split('|')
$tagResult | Add-Member NoteProperty 'Binary' $Bin
$tagResult | Add-Member NoteProperty 'Description' $BinDesc
} else {
$tagResult | Add-Member NoteProperty 'Binary' ''
$tagResult | Add-Member NoteProperty 'Description' ''
}
}
$tagResult
}
$offset = $offset + $size
}
[System.Runtime.InteropServices.Marshal]::FreeHGlobal($ptr)
}
$expression = 'Get-Pool'
if ($sortvalue) {
$expression += "|Sort-Object -Property $sortvalue"
if ($sortdir -eq 'Descending') {
$expression += ' -Descending'
}
}
if ($top -gt 0) {
$expression += "|Select-Object -First $top"
}
if ($values) {
$expression += "|Select-Object $values"
}
if ($view -eq 'csv') {
$expression += '|ConvertTo-Csv -NoTypeInformation'
} elseif ($view -eq 'grid') {
$expression += '|Out-GridView -Title "Kernel Memory Pool (captured $(Get-Date -Format "dd/MM/yyyy HH:mm:ss"))" -Wait'
} elseif ($view -eq 'table') {
$expression += '|Format-Table *'
}
if ($loop -gt 0 -and $view -ne 'grid') {
$loopcount = 0
while ($true) {
$loopcount++
if ($loopcount -eq 1) {
Invoke-Expression $expression
if ($view -eq 'csv') {
$expression += '|Select-Object -skip 1'
}
} else {
Invoke-Expression $expression
}
Start-Sleep -Seconds $loop
}
} else {
Invoke-Expression $expression
}