-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathParseWmiEvents
39 lines (35 loc) · 1.69 KB
/
ParseWmiEvents
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
# this script reads out the WMI Trace Eventlog
# it will parse the event text to extract the WMI queries using a RegEx expression (tested on English OS)
# then it will run those queries and measure how long it takes to execute them
# this should help in troubleshooting high wmi cpu usage
#
# line below is an easy way to view the results:
# $events | Out-GridView
$wmiLogName = 'Microsoft-Windows-WMI-Activity/Trace'
$rawEvents = Get-WinEvent -FilterHashTable @{logname=$wmiLogName; id=11} -Oldest
$events = @()
ForEach ($rawEvent in $rawEvents)
{
if ($rawEvent.Message.Contains("ExecQuery "))
{
$matches = [RegEx]::Match($rawEvent.Message, '-\s(?<Namespace>([^\s]+))\s:\s(?<Query>[^\;]+).*ClientProcessId\s=\s(?<PID>[^\;]+)');
$event = New-Object PSObject
$items = $rawEvent.Message.Split(";")
Add-Member -InputObject $event -MemberType NoteProperty -Name PID -Value $matches.Groups["PID"].Value.Trim()
$process = Get-Process -Id $event.PID -ErrorAction:SilentlyContinue
if ($process -eq $null)
{
Add-Member -InputObject $event -MemberType NoteProperty -Name "Process" -Value "<unknown>"
}
else
{
Add-Member -InputObject $event -MemberType NoteProperty -Name "Process" -Value $process.Name
}
Add-Member -InputObject $event -MemberType NoteProperty -Name 'Namespace' -Value $matches.Groups["Namespace"].Value.Trim()
Add-Member -InputObject $event -MemberType NoteProperty -Name 'Query' -Value $matches.Groups["Query"].Value.Trim()
$event.Query
$result = Measure-Command { Get-WmiObject -Namespace $event.Namespace -Query $event.Query }
Add-Member -InputObject $event -MemberType NoteProperty -Name 'TotalMilliSeconds' -Value $result.TotalMilliseconds
$events += $event
}
}