我正在尝试使用power shell中的性能计数器监视本地计算机的物理内存使用率。在资源监视器中,在Memory选项卡下,我们可以在任务管理器中,在Performance选项卡--> memory下,我们可以看到有多少%的内存使用了used.Also。也要检查图像的引用。
为了达到同样的效果,我在power shell中执行了以下步骤
1)使用下面的命令,我可以获得最大的物理内存
$totalPhysicalmemory = gwmi Win32_ComputerSystem | % {$_.TotalPhysicalMemory /1GB}
2)使用下面的计数器命令,我得到了平均可用内存
$avlbleMry = ((GET-COUNTER -Counter "\Memory\Available MBytes"|select -ExpandProperty countersamples | select -ExpandProperty cookedvalue )/1GB
3)计算使用的物理内存的百分比:(将数学四舍五入到小数后的2位)
(($totalPhysicalmemory-$avlbleMry)/$totalPhysicalmemory)*100
我做得对吗?这是获得%内存使用率的正确方法吗?有没有更好的方法来使用WMI命令或性能计数器或其他方法来获取%的物理内存?
发布于 2020-05-22 08:30:43
我认为你的方法是正确的,但存储单元是错误的。
还有,using Get-CimInstance is recommended。
所以代码看起来像这样
# use the same unit `/1MB` and `Available MBytes`
$totalPhysicalmemory = (Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory /1MB
$avlbleMry = (Get-Counter -Counter "\Memory\Available MBytes").CounterSamples.CookedValue
(($totalPhysicalmemory-$avlbleMry)/$totalPhysicalmemory)*100
以及其他一些方式
# Win32_OperatingSystem, KB
$osInfo = Get-CimInstance -ClassName Win32_OperatingSystem
$total = $osInfo.TotalVisibleMemorySize
$free = $osInfo.FreePhysicalMemory
$used = $total - $free
$usedPercent = $used/$total * 100
echo $usedPercent
# Microsoft.VisualBasic, bytes
Add-Type -AssemblyName Microsoft.VisualBasic
$computerInfo = [Microsoft.VisualBasic.Devices.ComputerInfo]::new()
$total = $computerInfo.TotalPhysicalMemory
$free = $computerInfo.AvailablePhysicalMemory
$used = $total - $free
$usedPercent = $used/$total * 100
echo $usedPercent
https://stackoverflow.com/questions/61949108
复制相似问题