powershell - 将输出设为无限或 Nan
问题描述
我尝试了以下命令来获取计算机的内存使用情况,但得到的错误为无限或 NaN。
$Totalsizemem=gwmi Win32_PhysicalMemory | Measure-Object -Property capacity -Sum | Foreach {"
{0:N2}" -f ([math]::round(($_.Sum / 1GB),2))}
gwmi -computername localhost Win32_Process | Sort WorkingSetSize -Descending | Select
Name,@{n="Memoryusage(%)";Expression ={[math]::round(($_.WorkingSetSize / 1GB),1) /
$Totalsizemem*100}} | Select -First 10 | Format-Table -AutoSize | Out-Default
预期的输出是 -
Name Memoryusage(%)
---- --------------
powershell_ise.exe 0.655737704918033
explorer.exe 0.655737704918033
explorer.exe 0.655737704918033
explorer.exe 0.655737704918033
但我得到的输出 -
Name Memoryusage(%)
---- --------------
powershell_ise.exe ∞
svchost.exe ∞
explorer.exe NaN
svchost.exe NaN
explorer.exe NaN
有谁知道如何纠正这个?
解决方案
您不断舍入和格式化中间结果,牺牲准确性到您的数据毫无意义的地步。
把事情简单化:
$Totalsizemem = Get-WmiObject Win32_PhysicalMemory | Measure-Object -Property Capacity -Sum |Select -Expand Sum
Get-WmiObject Win32_Process |Sort WorkingSetSize -Descending |Select Name,@{Name='MemoryUsage(%)';Expression={$_.WorkingSetSize * 100 / $Totalsizemem}} -First 10
推荐阅读
- c# - c# 按索引对列表进行分组
- django - 获取上传文件的Django模型名称
- c++ - cin.fail() 和 !(cin>>var) 在输入多个整数然后是字符串或字符时失败
- c# - 将十六进制转换为 unicode
- javascript - 在 ReactJs 中制作计时器/倒计时代码时遇到问题
- node.js - 在nodejs中以windows-1251编码格式下载文件
- javascript - 如何在反应中将我的导入缩短为“@dir/subdir/file”中的 import xxx
- docker - Kubernetes集群初始化问题
- python - += 不支持的操作数类型:“方法”和“整数”
- git - git push 成功并且能够看到比较和拉入 git hub 但无法接受或合并它