I have around 200 servers and I need to get the disk space & logical drive space details (free space, used space & total space).
Here is my PowerShell query.
I would like to point that there is several places where error could occur. The purpose of this answer is to address the unknown number of headers. I would recommend that you run this in place to see what it is trying to show you before you attempt to integrate this.
# Gather all wmi drives query at once
$alldisksInfo = Get-WmiObject –query "SELECT * FROM Win32_DiskDrive" -ComputerName $servers -ErrorAction SilentlyContinue | Group-Object __Server
# Figure out the maximum number of disks
$MaximumDrives = $alldisksInfo | Measure-Object -Property Count -Maximum | Select-Object -ExpandProperty Maximum
# Build the objects, making empty properties for the drives that dont exist for each server where need be.
$servers | ForEach-Object{
# Clean the hashtable
$infoObject = [ordered]@{}
# Populate Server
$infoObject.Server = $_
# Add other simple properties here
$infoObject.PhysicalMemory = (Get-WmiObject Win32_PhysicalMemory -ComputerName $infoObject.Server | Measure-Object Capacity -Sum).Sum/1gb
# Add the disks information from the $diskInfo Array
$serverDisksWMI = $alldisksInfo | Where-Object{$_.Name -eq $infoObject.Server} | Select-Object -ExpandProperty Group
for($diskIndex =0; $diskIndex -lt $MaximumDrives;$diskIndex++){
$infoObject."PhysicalDisk$diskIndex" = [Math]::Round(($serverDisksWMI | Where-Object{($_.DeviceID -replace "^\D*") -eq $diskIndex} | Select -Expand Size)/1GB)
}
# Create the custom object now.
New-Object -TypeName psobject -Property $infoObject
} # | Export-Csv ....
Since this uses the pipeline you can easily add export-CSV on the end of this.