2 min. Read

Some time ago, I released a PowerShell script that asks System Center Virtual Machine Manager (SCVMM) to get all the virtual machines, including their virtual hard disks, then calculates the size and percentage used for each VHD (X), and finally sends you a nicely formatted HTML report. You can read all about it here.

In this quick blog post, I’ll share with you how to get the same report in comma-separated value (CSV) format and without SCVMM. This is handy when working with a large Hyper-V cluster with many virtual machines, and you want to know the type of virtual hard disk attached to each virtual machine, including their disk size and remaining disk space, so you know before your VM runs out of disk space.

Here is a function that does the work for you.

Function Get-VMVHDs {

<#
    .Synopsis
        A script used to find all VHD files for all VMs in a Hyper-V Cluster

    .DESCRIPTION
        A script used to find all VHD files for all VMs in a Hyper-V Cluster,
        including the type of each virtual hard disk attached to every VM with their footprint size on disk,
        and the remaining disk space, so you know before the VM runs out of disk space.
        Finally, it will export the report into a csv file.
        
    .Notes
        Created   : 2018-12-03
        Version   : 1.0
        OS        : Windows Server 2016 Hyper-V or later
        Author    : Charbel Nemnom
        Twitter   : @CharbelNemnom
        Blog      : https://charbelnemnom.com
        Disclaimer: This script is provided "AS IS" with no warranties.

    .EXAMPLE
        . .Get-VMVHDs.ps1
        Get-VMVHDs -Cluster <ClusterName>
    #>   

[CmdletBinding(SupportsShouldProcess=$true)]
    Param(
    [string]$ClusterName
    )

Get-ClusterGroup -Cluster $ClusterName | ? {$_.GroupType -eq 'VirtualMachine' } | Get-VM | `
% { $vhd = Get-VHD -ComputerName $_.ComputerName -VmId $_.VmId; $vhd | Add-Member -NotePropertyName “Name” -NotePropertyValue $_.Name; $vhd; } | `
Select-Object Name, @{label=’Host Name’;expression={$_.ComputerName}}, Path, VhdFormat, VhdType, @{label=’Size On Physical Disk (GB)’;expression={$_.FileSize/1gb –as [int]}}, `
@{label=’Max Disk Size (GB)’;expression={$_.Size/1gb –as [int]}}, @{label=’Remaining Space (GB)’;expression={($_.Size/1gb - $_.FileSize/1gb) –as [int]}} | `
Export-Csv -Path “C:$($ClusterName)-VHDReport.csv" -NoTypeInformation

}

And here is the final report in CSV:

This is version 1.0, do you want more features? Feel free to leave a comment below.

Enjoy 🙂

__
Thanks for locking my blog.

If you have any questions or feedback, please leave a comment.

-Charbel Nemnom-

LEAVE A REPLY

Please enter your comment!
Please enter your name here