This line
$output = $($datastore.Name), $capacity, $freeSpace, $usedSpace, $utilization
is interpreted as an array (due to the commas), and those are printed row by row, each row on a new line.
You can use a string with all the variables, and there the commas will be interpreted as literal characters.
$output = "$($datastore.Name), $capacity, $freeSpace, $usedSpace, $utilization"
------------------------------
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
------------------------------
Original Message:
Sent: Nov 28, 2024 05:39 PM
From: dbutch1976
Subject: Issue with csv formating
Hi all,
Having a simple issue but can't seem to figure it out. Rather than having my output all on the correct line it's coming out like this:
Datastore,Capacity,FreeSpaceGB,UsedSpaceGB,utilization |
Storage1 |
3023.650391 |
492.1201172 |
2531.530273 |
83.72430494 |
Storage 2 |
31.37207031 |
31.32519531 |
0.046875 |
0.149416342 |
Naturally this makes it impossible to read. Why is the information not aligning with the column headers? Here's the script:
"Datastore,Capacity,FreeSpaceGB,UsedSpaceGB,utilization" > C:\output\Storage.csv
$datastores = Get-Datastore
foreach ($datastore in $datastores) {
# Get the capacity and free space of the datastore
$capacity = $datastore.CapacityGB
$freeSpace = $datastore.FreeSpaceGB
# Calculate the used space in GB
$usedSpace = $capacity - $freeSpace
# Calculate the utilization percentage
$utilization = ($usedSpace / $capacity) * 100
write-host $($datastore.Name) $capacity $freeSpace $usedSpace $utilization
#Write Output to csv
$output = $($datastore.Name),$capacity,$freeSpace,$usedSpace,$utilization
$output >> C:\output\Storage.csv
}