Get Disk Space

Function to gather disk space from a list of servers

Function to gather disk space form a list of servers

Function get-diskspace {
Param($servers)
$servers | foreach {
#define a named variable for the computername so that it can be used the Catch
#scriptblock
$computer = $_
Try {
$os = Get-WmiObject win32_OperatingSystem -computername $computer -ErrorAction Stop
#continue if there were no errors
Get-WmiObject -Class win32_Logicaldisk -ComputerName $computer
}
Catch {
#$_ is the error object
Write-Warning “Failed to get OperatingSystem information from $computer. $($_.Exception.Message)”
}
}|
Select PSComputername,DeviceID,VolumeName,
@{Name=”SizeGB”;Expression={$_.Size/1GB -as [int]}},
@{Name=”FreeGB”;Expression={[math]::Round($_.Freespace/1GB,2)}},
@{Name=”Free%”;Expression={[Math]::Round(($_.FreeSpace / $_.Size * 100),0) }} | Format-Table –AutoSize
}