win32_physicalMemory.Capacity在powershell中返回null

时间:2015-06-17 14:21:29

标签: windows powershell

这是一个非常奇怪的问题,只发生在我测试过的一台计算机上。

我有以下代码行:

(get-WmiObject win32_physicalMemory -Impersonation 3 -ComputerName "localhost").Capacity

由于某种原因返回null。但是,在同一台计算机上,如果我运行命令:

get-WmiObject win32_physicalMemory -Impersonation 3 -ComputerName "localhost"

我得到两根柱塞的信息,每根柱子的容量为8589934592(8GB)。计算机在Powershell版本2上,并且在其他计算机上的这个操作(Powershell版本在2到4之间)将从每个ram棒的容量返回而不是null。

为什么我从相同的命令得到不同的结果,即使Powershell版本在计算机上保持不变?

1 个答案:

答案 0 :(得分:3)

这是因为使用powershell 2.0,您无法使用该方法访问数组。

get-WmiObject win32_physicalMemory -Impersonation 3 -ComputerName "localhost" | select -expand capacity

将起作用

你提到的其他powershell 2.0计算机可能只有一根内存,所以它不会返回一个数组