本文介绍了如何获得在批处理脚本中使用WMIC内存利用率的百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是只使用 WMIC 获得的结果对内存利用率%的批处理脚本。我只得到以字节为单位的总内存和可用内存。

因此​​,如何让一个那些MB以及如何计算内存利用率%?

code数量1: -

 关闭@echo
SETLOCAL enabledelayedexpansion设定时间= 0
FOR / F跳过= 1%% P('WMIC CPU得到的LoadPercentage')做(
    设置Cpusage!倍!= %% p
    设置/ A时报+ = 1
)回声CPU百分比=%Cpusage0 %%%设定时间= 0
FOR / F跳过= 1%% P('WMIC COMPUTERSYSTEM GET TotalPhysicalMemory')做(
    设置totalMem!倍!= %% p
    设置/ A时报+ = 1
)设定时间= 0
FOR / F跳过= 1('WMIC OS获得FreePhysicalMemory')%% p做(
    设置availableMem!倍!= %% p
    设置/ A时报+ = 1
)设定时间= 0
FOR / F在跳过= 1%% P('WMIC OS获得FreePhysicalMemory ^ | FINDSTR物理')做(
    集/ A UsedMem = totalMem - availableMem
    设置usedMem!倍!= %% p
    设置/ A时报+ = 1

集/ A usedpercent =(usedMem * 100)/ totalMem回声可用内存=%availableMem0%字节
回声总内存=%totalMem0%字节
回声使用的内存=%UsedMem0%字节
回声内存利用率=%usedpercent0 %%%暂停

code数2: -

 关闭@echo
SETLOCAL enabledelayedexpansion
设定时间= 0
FOR / F跳过= 1%% P('WMIC CPU得到的LoadPercentage')做(
  设置Cpusage = %% p
  转到:完成

:完成
回声CPU百分比:%Cpusage %%%
SETLOCAL enabledelayedexpansion
设定时间= 0
FOR / F跳过= 1('WMIC OS获得freephysicalmemory')%% p做(  设置availableMem = %% p  集/ A availableMem = %% P / 1024  转到:完成

:完成
回声可用内存:%availableMem%MBSETLOCAL enabledelayedexpansion
设定时间= 0
FOR / F跳过= 1%% P('WMIC COMPUTERSYSTEM GET TotalPhysicalMemory')做(
集/ A totalMem0 / = 1024
  设置totalMem = %% p
 转到:完成

:完成
回声总内存:%totalMem%SETLOCAL enabledelayedexpansion
设定时间= 0  设置usedMem = %% p
 集/ A usedMem = totalMem,availableMem
集/ A usedpercent =(usedMem * 100)/ totalMem
  转到:完成:完成
回声使用的内存:%usedMem%
回声内存使用:%usedpercent %%%暂停


解决方案

SETLOCAL enabledelayedexpansion 应该只使用一次在一个批处理文件,如果没有真正的需要使用 SETLOCAL 多次。此命令不只是启用延迟扩张模式。它的总是拷贝也是整个当前环境表(可高达64 MB),命令扩展和延迟扩展,并在栈(内存)当前目录路径的当前状态。推压上堆叠这样的环境的数量不是无限的。在至少之间使用 ENDLOCAL 来避免因为堆栈溢出的批处理早早出局。有关详细信息请参见答案:




即使是64位的Windows命令间preTER(的cmd.exe )使用32位有符号整数。因此数值范围被限制为-2.147.483.648到+2.147.483.647。换句话说大于2 GB的算术运算离不开整数溢出做产生错误的结果。

下面是一个注释批处理文件,它不适合所有可能已安装的RAM的配置工作,但适用于那些在一年典型的2016年:2 GB,4 G​​B,8 GB,16 GB和32 GB

 关闭@echoREM注:KB =昆明植物研究所,MB = MIB和GB = 2GB的这个批处理文件,请参阅
REM https://en.wikipedia.org/wiki/Gibibyte对吉布的详细信息。REM创建当前环境变量的副本。另外启用
不需要此任务REM延迟的环境变量扩展。
SETLOCALREM命令WMIC与参数CPU获取的LoadPercentage输出
REM每个处理器一行。 WMIC的输出是UTF-16 LE与BOM。
REM的输出重定向到其通过印刷临时文件
REM命令类型为标准输出,这使得对UNI code到ASCII更好的工作
REM转换为FOR命令。注:1处理器可具有1或多个核。设置CpuUsage = 0
设置处理器= 0
%SYSTEMROOT%\\ SYSTEM32 \\ WBEM \\ CPU了Wmic.exe获取的LoadPercentage>中%TEMP%\\ cpu_usage.tmp
FOR / F在跳过= 1%% P(类型%temp%\\ cpu_usage.tmp')做(
    集/ A CpuUsage + = %% P
    集/ A处理器+ = 1

德尔%temp%\\ cpu_usage.tmpREM计算CPU使用率,因为所有的处理器百分比值。
设置/ A CpuUsage / =处理器
转到GetTotalMemoryWMIC的REM输出为UTF-16 LE与BOM。这间pretation
在ASCII REM输出/ OEM可能导致加工,而不是三线
REM只有两个与第三行仅仅是一个回车。因此,退出
第二行的值赋给变量后,REM每个循环。:GetTotalMemory
FOR / F跳过= 1%% M在(%SYSTEMROOT%\\ SYSTEM32 \\ WBEM \\了Wmic.exe COMPUTERSYSTEM GET TotalPhysicalMemory)并设置TotalMemory = %% M&放大器;转到GetAvailableMemory
:GetAvailableMemory
FOR / F跳过= 1%% M在(%SYSTEMROOT%\\ SYSTEM32 \\ WBEM \\了Wmic.exe OS获得FreePhysicalMemory)并设置AvailableMemory = %% M&放大器;转到ProcessValuesREM的总物理存储器是其中可以大于2 ^ 31(= 2 GB)字节
REM Windows命令处理器始终执行算术运算
REM 32位有符号整数。因此,超过2 GB的物理安装
REM内存超过32位有符号整数和算术的位宽
REM计算是错误的超过2 GB安装的内存。避免
REM的整数溢出,最后6个字符从字节删除
REM价值和剩余的字符被分为1073,以获得
GB的REM数量。此解决方法仅适用于物理RAM存在
REM 1 GB,即的整数倍为1 GB,2 GB,4 G​​B,8 GB,...REM 1 GB = 1.073.741.824个字节= 2 ^ 30
REM 2 GB = 2.147.483.648个字节= 2 ^ 31
REM 4 GB = 4.294.967.296个字节= 2 ^ 32
REM 8 GB = 8.589.934.592个字节= 2 ^ 33
REM 16 GB = 17.179.869.184字节= 2 ^ 34
REM 32 GB = 34.359.738.368字节= 2 ^ 35REM但是还有一个问题,至少在Windows XP上的x86。约50 MB
13759 Windows本身的内存物权被扣除。这可以在可见
虽然时已安装2 GB显示REM系统设置1.95 GB。
REM由1073分之前所以加50 MB。:ProcessValues
设置TotalMemory =%TotalMemory:〜0,-6%。
集/ A TotalMemory + = 50
集/ A TotalMemory / = 1073REM在GB的内存总量必须由1024相乘得到
在MB物权总物理内存始终是小到足以
REM有32位有符号整数来计算。集/ A TotalMemory * = 1024REM的可用内存以KB为单位,因此有
REM 32位有符号整数的数值范围没有问题。集/ A AvailableMemory / = 1024REM因此,在MB的内存使用可以很容易地确定。集/ A UsedMemory = TotalMemory - AvailableMemoryREM是必要的计算以MB为百分比值,而不是
REM KB避免对32 GB的RAM和近32位有符号整数溢出
REM整个RAM可因为所使用只是RAM量小。集/ A UsedPercent =(UsedMemory * 100)/ TotalMemory如果%处理器%==1(
    设置ProcessorInfo =
)其他(
    设置%处理器%处理器ProcessorInfo =

回声CPU百分比:%CpuUsage%%%% ProcessorInfo%
无回音内存:%AvailableMemory%MB
回声总内存:%TotalMemory%MB
回声二手内存:%UsedMemory%MB
回声内存使用:%UsedPercent%%%REM放弃当前的环境变量表,恢复previous
REM环境变量。命令处理器扩展的状态
REM(默认:ON)和延迟扩展(默认:OFF),以及作为
REM原有当前目录是通过这个命令虽然恢复
REM不符合上述指令进行修改的。
ENDLOCAL

有关理解使用的命令以及它们如何工作,打开命令提示符窗口中,执行有下面的命令,并阅读完全针对每个命令显示的所有帮助页面非常谨慎。


  • DEL /?

  • 回声/?

  • ENDLOCAL /?

  • 为/?

  • 转到/?

  • REM /?

  • SETLOCAL /?

  • 设置/?

  • 键入/?

  • WMIC /?

  • WMIC CPU获得/?

  • WMIC OS GET /?

  • WMIC COMPUTERSYSTEM GET /?

I am not getting the result for % of memory utilization in batch script using only WMIC. I am getting only total memory and free memory in bytes.

So how to a get those in MB and how to calculate % of memory utilization?

Code Number 1:-

@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

Code Number 2:-

@echo off


setlocal enabledelayedexpansion
set Times=0
for /f "skip=1" %%p in ('wmic cpu get loadpercentage ') do ( 
  set Cpusage=%%p
  goto :done
)
:done
echo CPU Percentage: %Cpusage%%%


setlocal enabledelayedexpansion
set Times=0
for /f "skip=1" %%p in ('wmic os get freephysicalmemory') do ( 

  set availableMem=%%p

  set /a availableMem=%%p/1024

  goto :done
)
:done
echo Free MEMORY: %availableMem% MB



setlocal enabledelayedexpansion
set Times=0
for /f "skip=1" %%p in ('wmic ComputerSystem get TotalPhysicalMemory') do ( 
set /a totalMem0/=1024
  set  totalMem=%%p
 goto :done
)
:done
echo Total MEMORY: %totalMem%

setlocal enabledelayedexpansion
set Times=0

  set usedMem=%%p
 set /a usedMem=totalMem-availableMem
set /a usedpercent=(usedMem*100) / totalMem
  goto :done

:done


echo Used MEMORY: %usedMem%
echo MEMORY usage: %usedpercent%%%

pause
解决方案

setlocal enabledelayedexpansion should be used only once in a batch file if there is no real necessity to use setlocal multiple times. This command does not just enable delayed expansion mode. It always copies also the entire current environment table (which can be up to 64 MB), the current states of command extension and delayed expansion, and the current directory path on stack (memory). The number of such environment pushes on stack is not unlimited. At least use endlocal between to avoid an early exit of batch processing because of a stack overflow. For more details see the answers on:

Even 64-bit Windows command interpreter (cmd.exe) uses 32-bit signed integers. Therefore the value range is limited to -2.147.483.648 to +2.147.483.647. In other words arithmetic operations with more than 2 GB can't be done without integer overflows producing wrong results.

Here is a commented batch file which does not work for all possible installed RAM configurations, but works for those which are typical in year 2016: 2 GB, 4 GB, 8 GB, 16 GB and 32 GB.

@echo off

rem Note: KB = KiB, MB = MiB and GB = GiB in this batch file, see
rem       https://en.wikipedia.org/wiki/Gibibyte 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

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 more than 2 GB installed physical
rem memory exceeds the bit width of a 32-bit signed integer and arithmetic
rem calculations are wrong on more than 2 GB 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

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • del /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • rem /?
  • setlocal /?
  • set /?
  • type /?
  • wmic /?
  • wmic CPU get /?
  • wmic OS get /?
  • wmic ComputerSystem get /?

这篇关于如何获得在批处理脚本中使用WMIC内存利用率的百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 04:46