今天在群里看到童鞋们在讨论使用Powershell获取内存信息,不才整理了下,发下博客。希望对后来者有所帮助!
#获取本机内存大小,单位为Gb,小数点后保留两位 $mem = "{0:N2}GB" -f (((Get-WmiObject -Class Win32_PhysicalMemory).capacity | Measure-Object -Sum).sum /1gb) #获取本机内存条数 $slot = ((Get-WmiObject -Class Win32_PhysicalMemory).capacity | Measure-Object -Sum).count #获取本机内存生产厂商 $Manufacturer = (Get-WmiObject -Class Win32_PhysicalMemory).Manufacturer #获取本机空闲内存,单位Gb,小数点后保留两位 $freemem = "{0:N2}GB" -f ((Get-WmiObject -Class Win32_OperatingSystem).FreePhysicalMemory /1mb) #打印结果 Write-Host "本机内存总计: $mem" Write-Host "本机共 $slot 条内存" Write-Host "本机空闲内存 $freemem" Write-Host "内存厂商 $Manufacturer"
本文链接: https://www.pstips.net/get-memory-info-from-localhost.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
$slot = (Get-WmiObject -Class Win32_PhysicalMemory).length
这样更直接
感谢回复,但是貌似没有length这个属性啊,用Capacity吧。
$slot原文意思是内存条的数目,Capacity就是容量了不过,你的提示也是对的,有时候确实没有length属性,我测试的时候是2条内存,所以是有length属性的(数组),而在只有1根内存条的机器上我的代码就有问题了,修正如下$slot =@(Get-WmiObject -Class Win32_PhysicalMemory).Length
感谢帮助啊,之前一直没搞懂softy的回复为什么会用length,你这么一说我恍然大悟!