How Do You Monitor The CPU Utilization of a Process Using PowerShell?

Viewed 24934

I'm trying to write a PowerShell script to monitor % CPU utilization of a SQL Server process. I'd like to record snapshots of this number every day so we can monitor it over time and watch for trends.

My research online said this WMI query should give me what I want:

Get-WmiObject -Query "SELECT PercentProcessorTime FROM win32_PerfFormattedData_PerfProc_Process WHERE Name='SqlServr'"

When I run the WMI query I usually get a value somewhere between 30-50% enter image description here

However, when I watch the process in Resource Monitor it usually averages at less than 1% CPU usage SQL Server Average CPU Utilization

I know the WMI query is simply returning snapshot of CPU usage rather than an average over a long period of time so I know the two aren't directly comparable. Even so, I think the snapshot should usually be less than 1% since the Resource Monitor average is less than 1%.

Does anybody have any ideas on why there is such a large discrepancy? And how I can get an accurate measurement of the CPU usage for the process?

3 Answers
Related