【问题标题】:How can know the usage of CPU of a windows service in an interval of time with PowerShell?PowerShell如何知道某个时间间隔内windows服务的CPU使用率?
【发布时间】:2019-10-12 11:23:00
【问题描述】:

我是 PowerShell 的初学者,如何使用 PowerShell 知道某个时间间隔(例如一小时)内 Windows 服务的 CPU 使用情况? 像这样的:

Get-Service | Get-counter

非常感谢

【问题讨论】:

  • Resmon.exe/perfmon.exe 也许是更好的选择?

标签: powershell windows-services cpu-usage


【解决方案1】:

对于 CPU 使用率,您需要使用 get-process commandlet,然后将进程名称映射到相应的服务。

这是过去讨论 get-process 的讨论线程之一

Listing processes by CPU usage percentage in powershell

此处为示例代码

$Details = @()
$AllRunningServices = Get-CimInstance -class win32_service | Where-Object {$_.State -eq 'Running'}  | Select-Object ProcessId , Name 

foreach($procid in $AllRunningServices)
{
$Details += Get-Process | Where-Object {$_.Id -eq $procid.ProcessId} | Select-Object ProcessName, Id, CPU , @{Name = "serviceName" ; Expression={$procid.Name}}

}

$Details |Sort-Object -Property CPU -Descending | ft

【讨论】:

  • 如何将进程名映射到对应的服务?非常感谢!
  • 每个服务都会产生它的进程。例如,SQL Server 服务将生成 sqlservr.exe 进程。选择进程名称 PID 并与相应的服务匹配。就像这里提到的stackoverflow.com/questions/26331183/…
  • 在我的答案中添加了一个示例代码,您可以使用它来进一步开发。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多