none
$Object = New-Object PSObject -Property @{ How to add and access PSObject Rows

    Question

  • I'm writing a script to check cpu, ram, disk-space on several computers in our network.  My script connects fine to the remote computers and dumps the correct data to the screen. 

    Goal:  To store the query results from all machines into an array or hash so the data can then be exported, either to excel or to Out-GridView or other. 

    Issue:  Only last record is getting expored to Out-GridView

    How do I store All my results into an array so it can be viewed?

     

    $Computers = Get-Content -Path C:\scripts\Computers.txt

    foreach($machine in $Computers)
    {

      $objCompSys = Get-WmIObject -class "Win32_ComputerSystem" -cn $machine -EA silentlyContinue   -namespace "root\CIMV2"
      foreach($item in $objCompSys)
      {
        $compname = $item.Name
        $physicalRAM = [Math]::Ceiling($item.TotalPhysicalMemory / 1MB)
        $numProcs = $item.NumberOfProcessors
      }
     
      $objProc = Get-WMIObject -class "Win32_Processor" -cn $machine -EA silentlyContinue   -namespace "root\CIMV2"
      foreach($item in $objProc)
      {
        $procType = $item.Name
        $procSpeed = $item.MaxClockSpeed
      }
     
      Write-Host ("CPU : " + $procType)
      Write-Host ("Processor Speed : " + $procSpeed)
      Write-Host ("MachineName is: " + $machine)  
      Write-Host ("Name          : " + $compname)
      Write-Host ("No. CPUs      : " + $numProcs)
      Write-Host ("Physical RAM  : " + $physicalRAM + " MB")  
     
     
      $Object = New-Object PSObject -Property @{
     
        MachineName         = $compname
        CPUSpeed            = $procSpeed
        "Num CPUs"          = $numProcs
        "Physical RAM"      = $physicalRAM
      }
     
    }

    $Object | Out-GridView


    -Tom
    Friday, February 18, 2011 10:45 PM

Answers

  • Tom;

    Create a blank array before the first foreach, and add each $object to it:

    $Computers = Get-Content -Path C:\scripts\Computers.txt
    $AllComputers = @()
    foreach($machine in $Computers)
    {
    
     $objCompSys = Get-WmIObject -class "Win32_ComputerSystem" -cn $machine -EA silentlyContinue  -namespace "root\CIMV2"
     foreach($item in $objCompSys)
     {
      $compname = $item.Name
      $physicalRAM = [Math]::Ceiling($item.TotalPhysicalMemory / 1MB)
      $numProcs = $item.NumberOfProcessors
     }
     
     $objProc = Get-WMIObject -class "Win32_Processor" -cn $machine -EA silentlyContinue  -namespace "root\CIMV2"
     foreach($item in $objProc)
     {
      $procType = $item.Name
      $procSpeed = $item.MaxClockSpeed
     }
     
     Write-Host ("CPU : " + $procType)
     Write-Host ("Processor Speed : " + $procSpeed)
     Write-Host ("MachineName is: " + $machine)  
     Write-Host ("Name     : " + $compname)
     Write-Host ("No. CPUs   : " + $numProcs)
     Write-Host ("Physical RAM : " + $physicalRAM + " MB")  
     
     
     $Object = New-Object PSObject -Property @{
     
      MachineName     = $compname
      CPUSpeed      = $procSpeed
      "Num CPUs"     = $numProcs
      "Physical RAM"   = $physicalRAM
     } 
     $AllComputers += $Object
    }
    
    $AllComputers | Out-GridView
    
    Karl
    http://unlockpowershell.wordpress.com
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    • Marked as answer by TomCelica Saturday, February 19, 2011 6:47 PM
    Friday, February 18, 2011 11:21 PM

All replies

  • Tom;

    Create a blank array before the first foreach, and add each $object to it:

    $Computers = Get-Content -Path C:\scripts\Computers.txt
    $AllComputers = @()
    foreach($machine in $Computers)
    {
    
     $objCompSys = Get-WmIObject -class "Win32_ComputerSystem" -cn $machine -EA silentlyContinue  -namespace "root\CIMV2"
     foreach($item in $objCompSys)
     {
      $compname = $item.Name
      $physicalRAM = [Math]::Ceiling($item.TotalPhysicalMemory / 1MB)
      $numProcs = $item.NumberOfProcessors
     }
     
     $objProc = Get-WMIObject -class "Win32_Processor" -cn $machine -EA silentlyContinue  -namespace "root\CIMV2"
     foreach($item in $objProc)
     {
      $procType = $item.Name
      $procSpeed = $item.MaxClockSpeed
     }
     
     Write-Host ("CPU : " + $procType)
     Write-Host ("Processor Speed : " + $procSpeed)
     Write-Host ("MachineName is: " + $machine)  
     Write-Host ("Name     : " + $compname)
     Write-Host ("No. CPUs   : " + $numProcs)
     Write-Host ("Physical RAM : " + $physicalRAM + " MB")  
     
     
     $Object = New-Object PSObject -Property @{
     
      MachineName     = $compname
      CPUSpeed      = $procSpeed
      "Num CPUs"     = $numProcs
      "Physical RAM"   = $physicalRAM
     } 
     $AllComputers += $Object
    }
    
    $AllComputers | Out-GridView
    
    Karl
    http://unlockpowershell.wordpress.com
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    • Marked as answer by TomCelica Saturday, February 19, 2011 6:47 PM
    Friday, February 18, 2011 11:21 PM
  • Yep that works, Thanks a bunch Karl

    -tom


    -Tom
    Saturday, February 19, 2011 6:48 PM
  • Glad I could help, Tom :)

    Karl


    http://unlockpowershell.wordpress.com
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Tuesday, February 22, 2011 4:49 PM