gpt4 book ai didi

windows - 如何在批处理脚本中使用 WMIC 获取内存利用率百分比?

转载 作者:可可西里 更新时间:2023-11-01 09:47:28 30 4
gpt4 key购买 nike

我没有在仅使用 W MIC 的批处理脚本中获得内存利用率百分比的结果。我只获得总内存和空闲内存(以字节为单位)。

那么如何获得以兆字节为单位的数据以及如何计算内存利用率百分比?

代码:-

@echo off
setlocal enabledelayedexpansion

set Times=0
for /f "skip=1" %%p in ('wmic cpu get loadpercentage') do (
set Cpusage!Times!=%%p
set /A Times+=1
)

echo CPU Percentage = %Cpusage0%%%

set Times=0
for /f "skip=1" %%p in ('wmic ComputerSystem get TotalPhysicalMemory') do (
set totalMem!Times!=%%p
set /A Times+=1
)

set Times=0
for /f "skip=1" %%p in ('wmic OS get FreePhysicalMemory') do (
set availableMem!Times!=%%p
set /A Times+=1
)

set Times=0
for /f "skip=1" %%p in ('wmic OS get FreePhysicalMemory ^|findstr physical') do (
set /a UsedMem= totalMem - availableMem
set usedMem!Times!=%%p
set /A Times+=1
)
set /a usedpercent=(usedMem*100)/totalMem

echo Free MEMORY = %availableMem0% Bytes
echo Total MEMORY = %totalMem0% Bytes
echo Used MEMORY = %UsedMem0% Bytes
echo Memory Utilization = %usedpercent0%%%

pause

最佳答案

如果没有必要多次使用 setlocal

setlocal enabledelayedexpansion 通常只在一个批处理文件中使用一次。此命令不仅启用延迟扩展模式。它总是还复制整个当前环境表(最大可达 64 MB)、命令扩展和延迟扩展的当前状态以及堆栈(内存)上的当前目录路径。这种环境压栈的次数不是无限的。至少在两者之间使用 endlocal 以避免由于堆栈溢出而提前退出批处理。有关详细信息,请参阅以下答案:

即使是 64 位 Windows 命令处理器 (cmd.exe) 也使用 32 位有符号整数。因此,取值范围限制在 -2.147.483.648 到 +2.147.483.647 之间。换句话说,如果没有整数溢出产生错误结果,则无法完成 2 GB 或更多的算术运算。

这是一个注释批处理文件,它不适用于所有可能安装的 RAM 配置,但适用于 2016 年的典型配置:2 GB、4 GB、8 GB、16 GB 和 32 GB。

@echo off

rem Note: KB = KiB, MB = MiB and GB = GiB in this batch file, see
rem https://en.wikipedia.org/wiki/Byte for details on GiB.

rem Create a copy of current environment variables. Enabling additionally
rem delayed environment variable expansion is not required for this task.
setlocal EnableExtensions DisableDelayedExpansion

rem The command WMIC with the parameters CPU GET LoadPercentage outputs
rem one line per processor. The output of WMIC is in UTF-16 LE with BOM.
rem The output is redirected to a temporary file which is printed by
rem command TYPE to STDOUT which makes a better job on UNICODE to ASCII
rem conversion as command FOR. Note: 1 processor can have 1 or more cores.

set "CpuUsage=0"
set "Processors=0"
%SystemRoot%\System32\wbem\wmic.exe CPU GET LoadPercentage >"%TEMP%\cpu_usage.tmp"
for /F "skip=1" %%P in ('type "%TEMP%\cpu_usage.tmp"') do (
set /A CpuUsage+=%%P
set /A Processors+=1
)
del "%TEMP%\cpu_usage.tmp"

rem Calculate the CPU usage as percentage value of all processors.
set /A CpuUsage/=Processors
goto GetTotalMemory

rem Output of WMIC is in UTF-16 LE with BOM. The interpretation of this
rem output in ASCII/OEM can result in processing three lines instead of
rem just two with third line being just a carriage return. Therefore exit
rem each loop after assigning the value of second line to the variable.

:GetTotalMemory
for /F "skip=1" %%M in ('%SystemRoot%\System32\wbem\wmic.exe ComputerSystem GET TotalPhysicalMemory') do set "TotalMemory=%%M" & goto GetAvailableMemory
:GetAvailableMemory
for /F "skip=1" %%M in ('%SystemRoot%\System32\wbem\wmic.exe OS GET FreePhysicalMemory') do set "AvailableMemory=%%M" & goto ProcessValues

rem Total physical memory is in bytes which can be greater 2^31 (= 2 GB)
rem Windows command processor performs arithmetic operations always with
rem 32-bit signed integer. Therefore 2 GB or more installed physical
rem memory exceeds the bit width of a 32-bit signed integer and arithmetic
rem calculations are wrong on 2 GB or more installed memory. To avoid
rem the integer overflow, the last 6 characters are removed from bytes
rem value and the remaining characters are divided by 1073 to get the
rem number of GB. This workaround works only for physical RAM being
rem an exact multiple of 1 GB, i.e. for 1 GB, 2 GB, 4 GB, 8 GB, ...

rem 1 GB = 1.073.741.824 bytes = 2^30
rem 2 GB = 2.147.483.648 bytes = 2^31
rem 4 GB = 4.294.967.296 bytes = 2^32
rem 8 GB = 8.589.934.592 bytes = 2^33
rem 16 GB = 17.179.869.184 bytes = 2^34
rem 32 GB = 34.359.738.368 bytes = 2^35

rem But there is one more problem at least on Windows XP x86. About 50 MB
rem of RAM is subtracted as used by Windows itself. This can be seen in
rem system settings when 1.95 GB is displayed although 2 GB is installed.
rem Therefore add 50 MB before dividing by 1073.

:ProcessValues
set "TotalMemory=%TotalMemory:~0,-6%"
set /A TotalMemory+=50
set /A TotalMemory/=1073

rem The total memory in GB must be multiplied by 1024 to get the
rem total physical memory in MB which is always small enough to
rem be calculated with a 32-bit signed integer.

set /A TotalMemory*=1024

rem The available memory is in KB and therefore there is
rem no problem with value range of 32-bit signed integer.

set /A AvailableMemory/=1024

rem So the used memory in MB can be determined easily.

set /A UsedMemory=TotalMemory - AvailableMemory

rem It is necessary to calculate the percentage value in MB instead of
rem KB to avoid a 32-bit signed integer overflow on 32 GB RAM and nearly
rem entire RAM is available because used is just a small amount of RAM.

set /A UsedPercent=(UsedMemory * 100) / TotalMemory

if "%Processors%" == "1" (
set "ProcessorInfo="
) else (
set "ProcessorInfo= of %Processors% processors"
)
echo CPU percentage: %CpuUsage% %%%ProcessorInfo%
echo Free memory: %AvailableMemory% MB
echo Total memory: %TotalMemory% MB
echo Used memory: %UsedMemory% MB
echo Memory usage: %UsedPercent% %%

rem Discard the current environment variable table and restore previous
rem environment variables. The states of command processor extension
rem (default: ON) and delayed expansion (default: OFF) as well as the
rem original current directory are restored by this command although
rem not modified at all by the commands above.
endlocal

要了解使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。

  • del/?
  • echo/?
  • endlocal/?
  • for/?
  • goto/?
  • rem/?
  • setlocal/?
  • 设置/?
  • 输入/?
  • wmic/?
  • wmic CPU get/?
  • wmic 操作系统获取/?
  • wmic ComputerSystem get/?

关于windows - 如何在批处理脚本中使用 WMIC 获取内存利用率百分比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37338476/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com