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

前端 未结 2 1392
北恋
北恋 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)
    
    0 讨论(0)
  • 2021-01-12 12:58

    The command to get SQL server process information:

    Get-Process SQLSERVR
    

    The command to get information for any process that starts with S:

    Get-Process S*
    

    To get the amount of virtual memory that the SQLServer process is using:

    Get-Process SQLSERVR | Select-Object VM
    

    To get the size of the working set of the process, in kilobytes:

    Get-Process SQLSERVR | Select-Object WS 
    

    To get the amount of pageable memory that the process is using, in kilobytes:

    Get-Process SQLSERVR - Select-Object PM
    

    To get the amount of non-pageable memory that the process is using, in kilobytes:

    Get-Process SQLSERVR - Select-Object NPM
    

    To get CPU (The amount of processor time that the process has used on all processors, in seconds):

    Get-process SQLSERVR | Select-Object CPU
    

    To better understand the Get-Process cmdlet, check out the documentation on technet here.

    0 讨论(0)
提交回复
热议问题