Functions/Get-HyperVStorageCommit.ps1


function Get-HyperVStorageCommit {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)] [string] $Driveletter
    )

    $Volume = Get-Volume $Driveletter -ea Stop


    $VHDFiles = Get-ChildItem "$($Driveletter):\" -Recurse -Filter "*.VHD*" -ea 0

    if (!($VHDFiles)) {
        Write-Error "No VHD files found on volume $Driveletter"
        break
    }

    $TotalVHDSize = 0
    $VHDResult = @()

    foreach ($VHD in $VHDFiles) {
        $VHDProperties = Get-VHD $VHD.fullname

        $obj = [PSCustomObject]@{
            Path     = $VHDProperties.Path
            FileSize = $VHDProperties.FileSize
            Size     = $VHDProperties.Size
        }
        $VHDResult += $obj

        $TotalVHDSize = $TotalVHDSize + $VHDProperties.Size

    }
    if (!($TotalVHDSize)) {
        Write-Error "No VHD files found on volume $Driveletter"
        break
    }

    $VHDResult | Format-Table

    Write-Host "------------"

    $Result = [PSCustomObject]@{
        "Volume Drive Letter"   = $Volume.DriveLetter
        "Volume Total Size"     = [math]::Round( ($Volume.Size / 1024 / 1024 / 1024) , 2)
        "Volume Size Remaining" = [math]::Round( ($Volume.SizeRemaining / 1024 / 1024 / 1024) , 2)
        "Total VHD Size"        = [math]::Round( ($TotalVHDSize / 1024 / 1024 / 1024) , 2)
        "VHD Commit Size Left"  = [math]::Round( (($Volume.Size - $TotalVHDSize) / 1024 / 1024 / 1024) , 2)
    }

    $Result | Format-List

    if ($Result."VHD Commit Size Left" -lt 1) {
        Write-Host "VHD size overcommitted!" -ForegroundColor Red
    } else {
        Write-Host "VHD size not overcommitted" -ForegroundColor Green
    }

}