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