mercredi 22 novembre 2017

Using Powershell to group by the Hard Disks Used Space from multiple Servers

Sometime, you may want to retrieve the Hard Disks Used Space from one or multiple Servers to build up a report (or something else!). In this case, the Drive ID does not really need to be shown as we want to group it up.

Here's what the Output looks like:

TotalCapacity TotalUsedSpace Server    
------------- -------------- ------    
571,7 Gb      228,3 Gb       SRV-SAPCS001
161,7 Gb      17,9 Gb        SRV-IIS001
109,7 Gb      15,4 Gb        SRV-IIS002

Here's the script:

"srv-sapcs001.katalykt.lan", "srv-iis001.katalykt.lan", "srv-iis002.katalykt.lan" |

    ForEach-Object {Get-WmiObject Win32_LogicalDisk -ComputerName $_ -Filter "Drivetype=3"} |

        Select-Object SystemName, Size, @{Label = "Used Space";Expression = {$_.Size - $_.FreeSpace}} |

            Group-Object SystemName |

                ForEach-Object {

                    New-Object PSObject -Property @{

                        Server = $_.Group.SystemName[0]

                        TotalUsedSpace = ("{0:N1} Gb" -f (($_.Group."Used Space" | Measure-Object -Sum).Sum / 1Gb))

                        TotalCapacity = ("{0:N1} Gb" -f (($_.Group.Size | Measure-Object -Sum).Sum / 1Gb))

                    }

                }