Powershell 以无限或Nan形式获取输出
我尝试了下面的命令来获取计算机的内存使用情况,但得到的错误为无限或NaNPowershell 以无限或Nan形式获取输出,powershell,memory-management,get-wmiobject,Powershell,Memory Management,Get Wmiobject,我尝试了下面的命令来获取计算机的内存使用情况,但得到的错误为无限或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 WorkingSet
$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 WMIOObject Win32_PhysicalMemory |测量对象-属性容量-总和|选择-展开总和
获取WmiObject Win32|u进程|排序WorkingSetSize-降序|选择名称,@{Name='MemoryUsage(%)';表达式={$\uu.WorkingSetSize*100/$Totalsizemem}-前10
$Totalsizemem=(gwmi Win32_PhysicalMemory |测量对象-属性容量-总和)。总和/1GB
;您可以执行以下操作::Round((gwmi Win32_PhysicalMemory | Measure Object-Property capacity-Sum)。Sum/1GB,2)如果您愿意……我尝试了您的建议,但将内存使用量列设置为空。名称MemoryUsage(%)---------powershell_ise.exe explorer.exe explorer.exe explorer.exe svchost。exe@Srishti真奇怪。你有什么改变吗?如果是这样,请尝试执行isi尝试的代码-$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}前10