Illuminati
Illuminati

Reputation: 565

Error in foreach function in powershell

Below is the code snippet i tried but having an error because of it can anyone helpme in editing this one

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

Upvotes: 0

Views: 107

Answers (3)

Kevin_
Kevin_

Reputation: 3106

You already had it. You just added too much.

Gwmi win32_PhysicalMemory | Measure-Object -Property Capacity -Sum

And if you wanted to show only the sum then:

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

Upvotes: 3

eltaco431
eltaco431

Reputation: 850

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

Your code works fine. You just have an extra pipe at the end of your gwmi cmdlet.

Upvotes: 1

Illuminati
Illuminati

Reputation: 565

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

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

Will solve the problem :D

Upvotes: 0

Related Questions