Pages: [1]
Print
Author Topic: Request: Script to list VMs with large disks  (Read 629 times)
KristianW
Newbie
*
Posts: 13


View Profile
« on: May 13, 2009, 05:05:22 AM »

Hi,

I am trying to make a script that lists VMs with large harddisks in an easy way.
I found what I was looking for in a one liner, which does what I want - but it would be nice to have a script that gives a report.

this lists size of disk and the path to the diskfile.

Code:
get-vm | get-harddisk | ?{$_.CapacityLB*1kb -gt 50gb

Since I am not a experienced scripter, I would appreciate some help.


edit: would also be excellent, to list VMs with multiple harddisks that together gives, lets say 50gb.
ex: Vm1 has three harddisks, one is 30Gb one is 20gb and one is 5 gb. Alltogether this gives a total harddisk capacity of 55GB.

« Last Edit: May 13, 2009, 05:35:59 AM by KristianW » Logged
ewannema
Administrator
Newbie
*****
Posts: 19


View Profile
« Reply #1 on: May 13, 2009, 08:30:50 AM »

Here is a script that should do what you want.  It takes a parameter of the cutoff size, but defaults it to 50GB to be compatible with what you posted.

Also be aware that if you are interested in all disk space rather than configured you will have to factor in the swap file and snapshots.

I am not sure what you mean by report.  That would be a combination of fields and formatting.  For simple reporting you can try <scriptname> | Export-Csv <report> or <scriptname> | ConvertTo-Html > <report>

Code:
param
(
    $limit = 50gb # Show any VMs with more disk storage than this
)

foreach ($vm in Get-VM)
{
    $hdTotal = 0
    $hdCount = 0
    foreach ($hardDisk in ($vm | Get-HardDisk))
    {
        $hdTotal += $hardDisk.CapacityKB * 1kb
        $hdCount += 1
    }
     
    if ($hdTotal -gt $limit)
    {
        $vm | Select-Object Name,
                            @{Name="HardDiskTotalBytes"; Expression={$hdTotal}},
                            @{Name="HardDiskCount"; Expression={$hdCount}}
    }
}
Logged
KristianW
Newbie
*
Posts: 13


View Profile
« Reply #2 on: May 13, 2009, 08:55:49 AM »

Thanks! You are a saint!!
Logged
Pages: [1]
Print
Jump to: