VMware Cloud Community
VirtualNoitall
Virtuoso
Virtuoso

adding harddrive space to get-vm query

get-vm | select Name, Description, PowerState, Memory*, @{Name="Host"; Expression={$_.Host.Name}} | export-csv c:\output.csv

The above does a good job at getting most of the info I need. I would like to add total hardrive space given to each vm as well whether there is 1, 2 or more harddrives. I tried a couple of things but could get it to work. Any quick solutions? Thanks!

0 Kudos
5 Replies
halr9000
Commander
Commander

Just so happens I was working on something like this the other day. A VM has a HardDisks property which is an array of virtual HD objects. You can get the virtual disk size from that. However, if you want partition size, you have to access the Disks property of the Guest property. This only works if the VMware Tools are running. The Disks property holds an array of DiskInfo objects, one for each drive. It has properties for Capacity and FreeSpace. In this example $dvm is a collection of 2 VMs. Note you get the "hard drive manufacturer answer" on size when accessing the Virtual HD object, but the OS formatted capacity on the DiskInfo object.

51# $dvm | ft harddisks

HardDisks
---------
{Hard Disk 1}
{Hard Disk 1}

52# $dvm[0].HardDisks

CapacityKB Persistence                                           Filename
---------- -----------                                           --------
10485760   Persistent  ...e] dragon (LAN Survey)/dragon (LAN Survey).vmdk

53# $dvm[0].Guest.Disks

Path                                                           Capacity                           FreeSpace
----                                                           --------                           ---------
C:\                                                         10725732352                          2494869504


54# $dvm[0].Guest.Disks[0].Capacity / 1KB
10474348
55# $dvm[0].Guest.Disks[0].Capacity / 1MB
10228.85546875
56# $dvm[0].Guest.Disks[0].Capacity / 1GB
9.98911666870117

Hal Rottenberg

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
VirtualNoitall
Virtuoso
Virtuoso

Thanks, now I just need to figure out what that does to my script. I think I am a little pipe happy so I am gonna start from scratch and try it a different way. If anyone has this or can quickly show how this will fit into my script it would be great appreciated.

I will start on my own though and should have it figured out in a day or so Smiley Happy

0 Kudos
halr9000
Commander
Commander

Nothing wrong with pipe-happy. In context, of course.

get-vm | select name, { $_.HardDisks | % { $_.CapacityKB } }

I have no idea if this works, I don't have any VMs with multiple disks. It worked for 1 disk though. Let me know.

Hal Rottenberg

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
0 Kudos
admin
Immortal
Immortal

get-vm | select Name, Description, PowerState, Memory*, @{Name="Host"; Expression={$_.Host.Name}} | export-csv c:\output.csv

The above does a good job at getting most of the info I need. I would like to add total hardrive space given to each vm as well whether there is 1, 2 or more harddrives. I tried a couple of things but could get it to work. Any quick solutions? Thanks!

It's a bit of a mouthfull, but you could add this to your select:

@{ Name="TotalDisk"; Expression={ ($_ | get-harddisk | measure-object -property CapacityKB -sum).Sum }}

This takes into account that a VM may have more than one hard disk. FWIW, this is taken from example 19 in the PowerShell Lab Manual

admin
Immortal
Immortal

Also, if you want to modify that to use the tools information as Hal suggested, you could do this:

@{ Name="TotalDisk"; Expression={ ($_.guest.disks | measure-object -property Capacity -sum).Sum }}

This will give you a more accurate picture, assuming you have tools running.

0 Kudos