0

Am not able to convert PS output to CSV format using echo function. I need to collect hardware information about multiple servers and got this script from internet. I modified it to collect only the necessary information such as Computername,HDD space, CPU details and RAM.

Below is my code:

$ArrComputers = "PC17"
Clear-Host
foreach ($Computer in $ArrComputers) {
    $computerSystemRam = Get-WmiObject Win32_ComputerSystem -Computer $Computer |
                         select @{n="Ram";e={[math]::Round($_.TotalPhysicalMemory/1GB,2)}} |
                         FT -HideTableHeaders -AutoSize
    $computerCPU = Get-WmiObject Win32_Processor -Computer $Computer |
                   select Name |
                   FT -HideTableHeaders
    $computerCPUCores = Get-WmiObject Win32_Processor -Computer $Computer |
                        select NumberOfLogicalProcessors |
                        FT -HideTableHeaders -AutoSize 
    $computerC = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID= 'C:'" -ComputerName $Computer |
                 select @{n="Size";e={[math]::Round($_.Size/1GB,2)}} |
                 FT -HideTableHeaders -AutoSize 
    $computerD = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID= 'D:'" -ComputerName $Computer |
                 select @{n="Size";e={[math]::Round($_.Size/1GB,2)}} |
                 FT -HideTableHeaders -AutoSize 
    $computerE = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID= 'E:'" -ComputerName $Computer |
                 select @{n="Size";e={[math]::Round($_.Size/1GB,2)}} |
                 FT -HideTableHeaders -AutoSize 
    echo $computer,$computerC,$computerD,$computerE,$computerSystemRam,$computerCPU,$computerCPUCores
}

and my output is coming as

PC17
99.9
12
537.11
15.98
Intel(R) Xeon(R) CPU E5-2630 0 @ 2.30GHz
12

What I need is to get this outputs as a comma separated value like below

PC17,99.9,12,537.11,15.98,Intel(R) Xeon(R) CPU E5-2630 0 @ 2.30GHz,12

so that I can open it in Excel. Please let me know what the problem here is? Or any other alternative solution to so as to get the output as .csv.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Sid133
  • 354
  • 2
  • 17
  • 1
    No echo is needed. Simply use "$computer,$computerC,$computerD,$computerE,$computerSystemRam,$computerCPU,$computerCPUCores" – f6a4 May 15 '19 at 15:34
  • 2
    Do not use FT ([`Format-Table`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/format-table?view=powershell-6)) either if you want to convert it to a `.csv` file. – iRon May 15 '19 at 15:46
  • At the end (after the `ForEach`) use [`ConvertTo-Csv`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/convertto-csv?view=powershell-6) – iRon May 15 '19 at 15:49

1 Answers1

1

Remove the Format-Table, use ExpandProperty and choose the right property from the array,

Also, I used -f to format the csv, see the differences:

foreach ($Computer in $ArrComputers) 
{
    $computerSystemRam = get-wmiobject Win32_ComputerSystem -Computer $Computer | select @{n="Ram";e={[math]::Round($_.TotalPhysicalMemory/1GB,2)}}
    $computerCPU = get-wmiobject Win32_Processor -Computer $Computer | select -ExpandProperty Name
    $computerCPUCores = get-wmiobject Win32_Processor -Computer $Computer | select -ExpandProperty NumberOfLogicalProcessors
    $computerC = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID= 'C:'" -ComputerName $Computer | select @{n="Size";e={[math]::Round($_.Size/1GB,2)}}
    $computerD = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID= 'D:'" -ComputerName $Computer | select @{n="Size";e={[math]::Round($_.Size/1GB,2)}}
    $computerE = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID= 'E:'" -ComputerName $Computer | select @{n="Size";e={[math]::Round($_.Size/1GB,2)}}
    "{0},{1},{2},{3},{4},{5},{6}" -f $computer,$computerC.Size,$computerD.Size,$computerE.Size,$computerSystemRam.Ram,$computerCPU,$computerCPUCores
}
Avshalom
  • 8,657
  • 1
  • 25
  • 43