1

So here is my issue and I sure that you have seen this a thousand times when it comes to formatting your output with PowerShell and hashtables. So I wrote a script that will gather all the essentials from a remote server like HD, MEM, CPU etc. First off I want to say the script works fine, with that said, I m having an issue with the formatting of output to csv. What I m looking for is some suggestion how to help me with my output to capture each HD into there own column or delimiter for systems that have multiple drives along with everything else.

So here is my script that is fully functional:

         $OutFile = "$psscriptroot\sys_info.csv"
         $Status = @()
          $HDCAP = @()
        $ArrComputers =  "."  
        #Specify the list of PC names in the line above. "." means local system

        #Clear-Host
          foreach ($Computer in $ArrComputers) 
        {
            $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer 
            $computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer  
            $computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer 
            $computerCPU = get-wmiobject Win32_Processor -Computer $Computer 
            $computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3 


              $exp =       @{Expression={$_.Server };Label="Computer_Name"},
                           @{Expression={$_.Manufacturer };Label="Manufacturer/Model"},
                           @{Expression={$_.Serial };Label="Serial #"},                   
                           @{Expression={$_.Processor };Label="Processor"},
                           @{Expression={$_.Harddrive};Label="Harddrive"},
                           @{Expression={$_.TotalMemorySize};Label="TotalMemorySize"}, 
                           @{Expression={$_.LastRebootTime};Label="LastRebootTime"} 

        foreach($sys in $computerSystem)
          {
          $mf = $sys | select Manufacturer,Model
          }
         foreach($cs in $computerSystem)
          {

          $mem = $cs | select -Property @{Name = "MemorySize GB" ;   Expression = { $_.TotalPhysicalMemory/1GB –as [int] } }
          }

         foreach($CB in  $computerBIOS)
        {
          $ser = $cb | select SerialNumber
        }
         foreach($CPU in  $computerCPU)
        {
          $PRCR = $CPU | select name
        }
        foreach($HD in $computerHDD)
        {
          $HDCAP += $HD| select -Property DeviceID,
                        @{Name = "SizeGB" ;   Expression = { $_.Size/1GB –as [int] } },
                        @{Name=  "FreeGB";    Expression={[math]::Round($_.Freespace/1GB,2)}}
        }
        foreach($OS in  $computerOS)
        {
          $CPOS = $os | select -Property  @{Name = "LastBootUpTime " ; Expression = {$OS.ConvertToDateTime($OS.LastBootUpTime)}}

        }
        $Status += (@{

         Server          =  $Computer
         Manufacturer    =  $Mf
         Serial          =  $ser      
         Processor       =  $PRCR
         Harddrive       =  $HDCAP 
         TotalMemorySize =  $mem     
         LastRebootTime  =  $CPOS     


                 })

        }




         $Status | select-object $exp 

Here is the copy of the output that I'm receiving:

Computer_Name      : .
Manufacturer/Model : @{Manufacturer=Micro-Star International Co., Ltd; Model=MS-0000}
Serial #           : @{SerialNumber=To be filled by O.E.M.}
Processor          : @{name=AMD Ryzen 5 1600 Six-Core Processor            }
Harddrive          : {@{DeviceID=C:; SizeGB=447; FreeGB=158.99}, @{DeviceID=D:; SizeGB=931; FreeGB=473.66}, @{DeviceID=F:; SizeGB=0; FreeGB=0.11}}
TotalMemorySize    : @{MemorySize GB=32}
LastRebootTime     : @{LastBootUpTime =4/10/2018 9:54:16 AM}

The desired out would look like this:

Computer_Name,Manufacturer/Model,Serial # ,Processor,Harddrive1, Harddrive 2, Harddrive 3 ,TotalMemorySize ,LastRebootTime

plus without all the @ symbols.

Please let me know your thoughts

JosefZ
  • 28,460
  • 5
  • 44
  • 83
user2176024
  • 41
  • 1
  • 6
  • 1
    Your hashtable technique seems very cumbersome. You would be much better generating some `PsCustomObjects`. They are a better choice for combining data like this and are easier to create, manage and (particularly relevant to your situation) export to csv. Also, consider using `Get-CimInstance` instead of the legacy WMI cmdlets - these typically format the output better, removing the need for date time conversions. – boxdog Apr 14 '18 at 17:22
  • Hi Jose, thanks for the edit on my post, I agree that my output results were not idea for this post. With that, I did take your suggestion under advisement, and I must say, one shoe does not fit all. I do like the suggestion about using PScustomObject for building the hash table, but as for using Ciminstance, that does not always give me the results that I'm looking for.If you could offer some ideas that would help my code, I will say that I'm all ears. – user2176024 Apr 14 '18 at 23:26

2 Answers2

0

Try Flatten-Object, it will not give you the exact required header names but very close and you can always consider to rename them:

PS C:\> $Status | Flatten | ft

Manufacturer.Manufacturer Manufacturer.Model       Server LastRebootTime.LastBootUpTime  Harddrive.1.DeviceID Harddrive.1.SizeGB Harddrive.1.FreeGB Serial.SerialNumber Processor.name
------------------------- ------------------       ------ ------------------------------ -------------------- ------------------ ------------------ ------------------- --------------
Hewlett-Packard           HP EliteBook Folio 9470m .      2018-04-11 6:11:28 PM          C:                                  221              59.42 PC1234567          Intel(R) Core...
iRon
  • 20,463
  • 10
  • 53
  • 79
0

Thank you all for the support you provided me with my question. With that, here is the format I decided to go with and I m liking the output that I m receiving: Here is a snippet of using PScustomObjects:

    {   $DiskResults += [pscustomobject][ordered] @{
            Computer = $Computer
            Manufacturer =$mf.Manufacturer
            Model = $mf.Model
            Serial =$ser.SerialNumber
            Memory ="{0:N2}" -f ($mem.TotalPhysicalMemory / 1gb)
            Processor =$PRCR.name
            LastRebootInDays = (($now - 
           ($CPOS.ConverttoDateTime($CPOS.lastbootuptime))).days  )
            Drive = $Disk.DeviceID
            CapacityGB = "{0:N2}" -f ($Disk.Size / 1gb)
            UsedGB = "{0:N2}" -f (($Disk.Size - $Disk.FreeSpace) / 1gb)
            UsedPercent = "{0:P0}" -f (($Disk.Size - $Disk.FreeSpace) / 
            $Disk.Size)
            FreeGB = "{0:N2}" -f ($Disk.FreeSpace / 1gb)
            FreePercent = "{0:P0}" -f ($Disk.FreeSpace / $Disk.Size)
            VolumeName = $Disk.VolumeName
        }

And here is an image of my results:

enter image description here

user2176024
  • 41
  • 1
  • 6