miked
miked Working the technologies in local government.

VHD Size Report Per CSV


VHD Size Report Per CSV

One of the issues with CSVs is that there isn’t a great way to view what’s on them in a GUI. One would think that VMM would be able to show you all VMs on a specific CSV sorted by size. That information is available but only through PowerShell. Below is a script that will take in a CSV name from user input (read-host) and give you the VMs on that CSV, their total disk size and sort them largest first.

This works really well if you host your VHDs for a machine together on the same CSV. Not so much if you split VHDs between CSVs. I’ve found this very useful when you have a CSV that is complaining about free space (more about what I use to alert me of that in a later post) and you need to vacate some VMs to avoid starving the CSV. Would love feedback on the ps1 below, and happy scripting!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
ipmo virtualmachinemanager -erroraction Stop
get-scvmmserver #put VMM Server Here

$VMInfo = @()
$CSVName = Read-Host "Please enter CSV Name"

#Gets VMs on a CSV using a wildcard search on location field so if you have similar names, 
#you may want to change this where-object query
$VMs = Get-SCVirtualMachine|? {$_.location -like "*$CSVName*"} 


foreach ($VM in $VMs)
{
    $disks = $null
    $size = 0
    $disks = Get-SCVirtualHardDisk -VM $VM
    foreach ($disk in $disks)
    {
        $size = $size + $disk.Size
    }
    $1VMInfo = new-object psobject -Property @{
                                        VM = $VM.Name
                                        VHD = $size/1GB
                                        Volume = $disk.HostVolume
                                        Location = $VM.Location
                                        }
    $VMInfo += $1VMInfo


}
$VMinfo |sort vhd -Descending|ft -auto vm,vhd,location

comments powered by Disqus