Wednesday, February 16, 2011

Get-DriveSpace

So, I'm not going through this right now, but here is the way to discover disk space utilization AND include mount points on remote machines.  It uses are fancy RegEx replaces to compair Win32_Volume.DeviceID and Win32_MountPoint.Volume to find the mount points associated to each disk.
$sessions = "localhost","PHSCONSOLE" | New-PSSession
$block = {
    gwmi -query "select * from Win32_Volume where DriveType='3'" | Select `
        @{Name="Server";Expression={$ENV:COMPUTERNAME}},`
        @{Name="Device";Expression={$_.DriveLetter}},`
        @{Name="MountPoint";Expression={$DID=$_.DeviceID;gwmi Win32_MountPoint | ? { (($_.Volume.Split("=")[1] -replace "[^a-z0-9-]|Volume","") -match ($DID -replace "[^a-z0-9-]|Volume","")) } | % { $_.Directory.Split("=")[1] -replace "`"","" }}},`
        @{Name="Capacity";Expression={[math]::round(($($_.Capacity)/1GB),2)}},`
        @{Name="FreeSpace";Expression={[math]::round(($($_.FreeSpace)/1GB),2)}},`
        @{Name="UsedSpace";Expression={[math]::round((($_.Capacity - $_.FreeSpace)/1GB),2)}},`
        @{Name="PercentFree";Expression={[math]::round(($($_.FreeSpace)/$($_.Capacity)*100),2)}}
}
Invoke-Command
-ScriptBlock $block -Session $sessions | Select Server,Device,MountPoint,Capacity,FreeSpace,UsedSpace,PercentFree | Sort Server,Device
Get-PSSession
| Remove-PSSession

0 comments: