我想显示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
试试这个:
@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%
我们可以使用~,通过使用~-‘要提取的字符数’运算符来提取字符串。
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