PowerShell中的foreach函数出错

时间:2013-07-16 19:33:31

标签: powershell powershell-v2.0 powershell-ise

下面是我尝试的代码片段,但由于有任何人可以帮助编辑此代码而出现错误

  $colItems4 = Get-WMIObject -class Win32_PhysicalMemory | Measure-Object -Property capacity -Sum | 
       foreach ($objItem4 in $colItems4 )
       {
             write-host "Total Physical Ram : " $objItem4.Sum 
       }

3 个答案:

答案 0 :(得分:3)

你已经拥有它了。你添加了太多。

Gwmi win32_PhysicalMemory | Measure-Object -Property Capacity -Sum

如果你想只显示总和:

Gwmi win32_physcialmemory | measure-object -property Capacity -sum | select sum

答案 1 :(得分:1)

$colItems4 = Get-WMIObject -class Win32_PhysicalMemory | Measure-Object -Property capacity -Sum
foreach ($objItem4 in $colItems4 )
{
     write-host "Total Physical Ram : " $objItem4.Sum 
}

您的代码运行正常。您只需在gwmi cmdlet末尾添加一个额外的管道。

答案 2 :(得分:0)

   $colItems4 = Get-WMIObject -class Win32_PhysicalMemory  -computername $strComputer
      $colItems5=$colItems4 | Measure-Object -Property capacity -Sum 
      foreach ($objItem4 in $colItems5)
   {

      write-host "Memory :  " $colItems5.Sum
   }

将解决问题:D