![](/img/trans.png)
[英]How to get CPU usage & Memory consumed by particular service in powershell script
[英]How to get CPU usage & Memory consumed by particular process in powershell script
我想找到单个进程的性能,例如“SqlServer”
我应该写哪些命令来找出两件事:
我发现很多解决方案列出了所有进程,但我想只得到1即SqlServer。
获取SQL Server进程信息的命令:
Get-Process SQLSERVR
获取以S开头的任何进程的信息的命令:
Get-Process S*
要获取SQLServer进程正在使用的虚拟内存量:
Get-Process SQLSERVR | Select-Object VM
要获取进程的工作集大小,以千字节为单位:
Get-Process SQLSERVR | Select-Object WS
要获取进程正在使用的可分页内存量,请以千字节为单位:
Get-Process SQLSERVR - Select-Object PM
要获取进程正在使用的不可分页内存量,请以千字节为单位:
Get-Process SQLSERVR - Select-Object NPM
获取CPU(进程在所有处理器上使用的处理器时间量,以秒为单位):
Get-process SQLSERVR | Select-Object CPU
关于CPU我通过以下方式工作:
# 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.