I have a simple Powershell script to gather disk information from an array of servers:
$servers="ALPHA", "BRAVO", "CHARLIE", "DELTA", "ECHO", "FOXTROT", "GOLF", "HOTEL"
ForEach ($s in $servers)
{
get-wmiobject win32_logicaldisk -ComputerName $s -Filter "Drivetype=3"| select @{n='Server '; e={$s}},
DeviceID,
@{n="Size [GB]";e={[math]::truncate($_.size / 1GB)}},
@{n="Free [GB]";e={[math]::truncate($_.freespace / 1GB)}}
}
which yields the desidered output:
Server DeviceID Size [GB] Free [GB]
------- -------- --------- ---------
ALPHA C: 299 161
ALPHA E: 499 201
BRAVO C: 476 148
CHARLIE C: 233 46
DELTA C: 475 94
ECHO C: 464 256
ECHO S: 465 222
FOXTROT C: 476 224
GOLF C: 475 100
HOTEL C: 476 50
I'd like to add to the output table a calculated column showing the free percentage of each disk, so I add , @{n="Free [%]";e={($_.freespace / $_.Size).ToString("P0")}}
to my select list, and the resulting script is hence:
ForEach ($s in $servers)
{
get-wmiobject win32_logicaldisk -ComputerName $s -Filter "Drivetype=3"| select @{n='Server '; e={$s}},
DeviceID,
@{n="Size [GB]";e={[math]::truncate($_.size / 1GB)}},
@{n="Free [GB]";e={[math]::truncate($_.freespace / 1GB)}} ,
@{n="Free [%]";e={($_.freespace / $_.Size).ToString("P0")}}
}
which, to my surprise, doesn't just add a column to previous output table, but instead yields the output:
Server : ALPHA
DeviceID : C:
Size [GB] : 299
Free [GB] : 161
Free [%] : 54%
Server : ALPHA
DeviceID : E:
Size [GB] : 499
Free [GB] : 198
Free [%] : 40%
Server : BRAVO
DeviceID : C:
Size [GB] : 476
Free [GB] : 142
Free [%] : 30%
Server : CHARLIE
DeviceID : C:
Size [GB] : 233
Free [GB] : 44
Free [%] : 19%
Server : DELTA
DeviceID : C:
Size [GB] : 475
Free [GB] : 88
Free [%] : 19%
Server : ECHO
DeviceID : C:
Size [GB] : 464
Free [GB] : 256
Free [%] : 55%
Server : ECHO
DeviceID : S:
Size [GB] : 465
Free [GB] : 222
Free [%] : 48%
Server : FOXTROT
DeviceID : C:
Size [GB] : 476
Free [GB] : 224
Free [%] : 47%
Server : GOLF
DeviceID : C:
Size [GB] : 475
Free [GB] : 100
Free [%] : 21%
Server : HOTEL
DeviceID : C:
Size [GB] : 476
Free [GB] : 46
Free [%] : 10%
where the "bidimensional-table" format is completely lost, at the expense of concise readability.
Addinng | Format-Table
just causes the output to be:
Server DeviceID Size [GB] Free [GB] Free [%]
------------ -------- --------- --------- --------
ALPHA C: 299 161 54%
ALPHA E: 499 192 39%
Server DeviceID Size [GB] Free [GB] Free [%]
------------ -------- --------- --------- --------
BRAVO C: 476 137 29%
Server DeviceID Size [GB] Free [GB] Free [%]
------------ -------- --------- --------- --------
CHARLIE C: 233 45 19%
Server DeviceID Size [GB] Free [GB] Free [%]
------------ -------- --------- --------- --------
DELTA C: 475 78 16%
Server DeviceID Size [GB] Free [GB] Free [%]
------------ -------- --------- --------- --------
ECHO C: 464 256 55%
ECHO S: 465 222 48%
Server DeviceID Size [GB] Free [GB] Free [%]
------------ -------- --------- --------- --------
FOXTROT C: 476 224 47%
Server DeviceID Size [GB] Free [GB] Free [%]
------------ -------- --------- --------- --------
GOLF C: 475 100 21%
Server DeviceID Size [GB] Free [GB] Free [%]
------------ -------- --------- --------- --------
HOTEL C: 476 42 9%
while the desired output would be:
Server DeviceID Size [GB] Free [GB] Free [%]
------------ -------- --------- --------- --------
ALPHA C: 299 161 54%
ALPHA E: 499 192 39%
BRAVO C: 476 137 29%
CHARLIE C: 233 45 19%
DELTA C: 475 78 16%
ECHO C: 464 256 55%
ECHO S: 465 222 48%
FOXTROT C: 476 224 47%
GOLF C: 475 100 21%
HOTEL C: 476 42 9%
or, even better:
Server DeviceID Size [GB] Free [GB] Free [%]
------------ -------- --------- --------- --------
ALPHA C: 299 161 54%
ALPHA E: 499 192 39%
------------ -------- --------- --------- --------
BRAVO C: 476 137 29%
------------ -------- --------- --------- --------
CHARLIE C: 233 45 19%
------------ -------- --------- --------- --------
DELTA C: 475 78 16%
------------ -------- --------- --------- --------
ECHO C: 464 256 55%
ECHO S: 465 222 48%
------------ -------- --------- --------- --------
FOXTROT C: 476 224 47%
------------ -------- --------- --------- --------
GOLF C: 475 100 21%
------------ -------- --------- --------- --------
HOTEL C: 476 42 9%
How could I obtain one of these two last output formats?