locked
Export a WMIObject to a csv RRS feed

  • Question

  • In our environment we have a bunch of computer I am trying to figure out some information on the physical RAM using the Get-WmiObject win32_physicalmemory. Everything works great when there is one stick of RAM when I use the command 

    Get-WmiObject win32_physicalmemory | Select-Object pscomputername, capacity, serialnumber, speed, manufacturer | Export-Csv "\\kisd-sccm01\g\Reporting\reporting.csv"

    but when there is more then 1 stick of RAM it will export on two lines. I've tried to figure out how I can take the sum of the sticks capacity and only output 1 row with PSComputerName, Capacity, serialnumber, speed, manufacturer. 

    I've tried doing the following but it only exports the sum

    Get-WmiObject win32_physicalmemory | Select-Object pscomputername, capacity, serialnumber, speed, manufacturer | Measure-Object -Property capacity -Sum | % {[MATH]::Round(($_.sum / 1GB),2)}

    I've tried the next code block and tried to create a variable $sum and pass that to the export but that doesn't seem to work either. 

    For now I would just like a way to be able to get the sum of the two capacity's and export it all on just 1 line no matter how many sticks of RAM are present. In the future we may add more WmiObject's so I am also curious how to output, for example, the above command with win32_physicalmemory and say some win32_processor information as well. 

    Thanks!!


    Thursday, September 15, 2016 1:12 PM

Answers

  • It works perfectly when there is only one stick of RAM but if I add -computername "name" and target a computer with 2 sticks of RAM it does not add the sum property and adds 2 lines in the csv file.

    Since you are going to add different WMI classes in the future you should take a look at this article.

    They explain the exact problem you are having right now. (making multiple objects / lines into 1 object)


    If you find an answer has helped you please use the answer button and/or use that thumbs up! Julio Sanchez-Tirado http://scriptomato.com co-founder | CEO | Pitcher

    • Proposed as answer by Hello_2018 Friday, September 16, 2016 2:42 AM
    • Marked as answer by Serijag Friday, September 16, 2016 2:07 PM
    Thursday, September 15, 2016 2:51 PM

All replies

  • You have to add the values in a script.  You can just loop through the values and add the memory and output to a custom object:

    Here are some instructions on using PowerShell to write a script: https://technet.microsoft.com/en-us/dd742419


    \_(ツ)_/

    Thursday, September 15, 2016 1:24 PM
  • Changed your one-liner in a 5 liner. You can edit where needed but this works just like you want it:

    $allRam = Get-wmiobject win32_physicalmemory | Select-Object pscomputername, capacity, serialnumber, speed, manufacturer 
    Add-Member -InputObject $allRam -MemberType NoteProperty -TypeName String  -Name Sum -Value "" 
    foreach($ram in $allRam){ $allRam.Sum += Measure-Object -InputObject $Ram -Property capacity -Sum | % {[MATH]::Round(($_.sum / 1GB),2)}}
    $allRam.Sum += "GB"
    $allRam | Export-Csv -Path C:\Users\SomeUser\Downloads/test.csv

    Here is the output it generates:

    Output of command, test.csv

    Don't forget to mark this answer as an answer if this has helped you and use that thumbs up!


    Julio Sanchez-Tirado http://scriptomato.com co-founder | CEO | Pitcher



    Thursday, September 15, 2016 1:48 PM
  • It works perfectly when there is only one stick of RAM but if I add -computername "name" and target a computer with 2 sticks of RAM it does not add the sum property and adds 2 lines in the csv file.
    Thursday, September 15, 2016 2:47 PM
  • It works perfectly when there is only one stick of RAM but if I add -computername "name" and target a computer with 2 sticks of RAM it does not add the sum property and adds 2 lines in the csv file.

    Since you are going to add different WMI classes in the future you should take a look at this article.

    They explain the exact problem you are having right now. (making multiple objects / lines into 1 object)


    If you find an answer has helped you please use the answer button and/or use that thumbs up! Julio Sanchez-Tirado http://scriptomato.com co-founder | CEO | Pitcher

    • Proposed as answer by Hello_2018 Friday, September 16, 2016 2:42 AM
    • Marked as answer by Serijag Friday, September 16, 2016 2:07 PM
    Thursday, September 15, 2016 2:51 PM
  • Awesome, that gives me a place to start. I'll try it when I get a chance. Thanks a bunch!
    Thursday, September 15, 2016 3:14 PM
  • No problem. If you have any questions just post a comment. If in the future one of the comments in this thread have helped you please use the answer button to mark the answer ;)

    If you find an answer has helped you please use the answer button and/or use that thumbs up! Julio Sanchez-Tirado http://scriptomato.com co-founder | CEO | Pitcher

    Thursday, September 15, 2016 3:17 PM