How to get CPU usage & Memory consumed by particular process in powershell script

前端 未结 2 1389
北恋
北恋 2021-01-12 12:33

I want to find performance of single process, as example \"SqlServer\"

Which commands I should write to find out 2 things:

  1. RAM utilized by SqlServer
2条回答
  •  囚心锁ツ
    2021-01-12 12:52

    About the CPU I got this working the following way:

    # To get the PID of the process (this will give you the first occurrance if multiple matches)
    $proc_pid = (get-process "slack").Id[0]
    
    # To match the CPU usage to for example Process Explorer you need to divide by the number of cores
    $cpu_cores = (Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors
    
    # This is to find the exact counter path, as you might have multiple processes with the same name
    $proc_path = ((Get-Counter "\Process(*)\ID Process").CounterSamples | ? {$_.RawValue -eq $proc_pid}).Path
    
    # We now get the CPU percentage
    $prod_percentage_cpu = [Math]::Round(((Get-Counter ($proc_path -replace "\\id process$","\% Processor Time")).CounterSamples.CookedValue) / $cpu_cores)
    

提交回复
热议问题