如何使用批处理文件查找 CPU、RAM 和内存使用百分比

问题描述 投票:0回答:2

我想显示CPU使用率百分比和空闲CPU百分比, RAM 和正在运行的带有日期和时间戳的程序也一样

@echo off 
Powershell -command "&{Get-WmiObject -Query 'Select * from Win32_PerfFormattedData_PerfProc_Process '| Select Name, @{Name='CPU(p)';Expression={$_.PercentProcessorTime}} | where {$_.'CPU(p)' -gt 0 } |Sort 'CPU(p)' -descending | Format-Table -AutoSize;}" 
Powershell -command "&{Get-Process | Select Name, @{Name='CPU(s)';Expression={$_.CPU}} | sort 'CPU(s)' -Descending | Select -First 5| Format-Table -AutoSize;}"
 Powershell -noexit -command "&{Get-WmiObject -Query 'Select * from Win32_OperatingSystem' | Select FreePhysicalMemory | Format-Table -AutoSize;}

我尝试了上面的代码,但我想要输出像 CPU % Utilized % Free、RAM % Applied % Free、Program %CPU % RAM

cmd
2个回答
0
投票

试试这个:

@echo off

set totalMem=
set availableMem=
set usedMem=
for /f "tokens=4" %%a in ('systeminfo ^| findstr Physical') do if defined totalMem (set availableMem=%%a) else (set totalMem=%%a)

set totalMem=%totalMem:,=%

set availableMem=%availableMem:,=%

set /a usedMem=totalMem-availableMem

Echo Total Memory: %totalMem%
Echo Used Memory: %usedMem%



setlocal enabledelayedexpansion

set Times=0

for /f "skip=1" %%p in ('wmic cpu get loadpercentage') do (

set Cpusage!Times!=%%p

set /A Times=!Times! + 1

)

echo Percentage = %Cpusage0%

来源:如何在批处理文件中获取当前CPU使用率和可用内存?


0
投票

我们可以使用~,通过使用~-‘要提取的字符数’运算符来提取字符串。

for /f "tokens=2 delims==" %%a in ('wmic computersystem 
 getTotalPhysicalMemory /value') do (
   for /f "delims=" %%b in ("%%a") do (
    Set "MEM=%%b" 
   )
)
Set /a TotalPhysicalMemory = %MEM:~0,-3%/1024/1024
echo %TotalPhysicalMemory% GB
© www.soinside.com 2019 - 2024. All rights reserved.