VMware Cloud Community
sdolcourt1
Contributor
Contributor

$_UsedSpaceGB, use case is a VM with multple vmdk's on different datastores. How to break that out?

Hello, Forum,

I'm writing a report to break out datastores, the VMs on them, and how much space each VMDK consumes on the datastore.

Though I've found $_.UsedSpaceGB property via the get-vm cmdlet, it's a total sum of the VM's vmdk's.

Of the script examples I've seen, nothing yet covers the condition of vmdk's spread amongst other datastores; scripts tend towards all vmdk's on a single datastore.

Question is: how best to report the size of each VMDK, under a datastore heading?

$vm = get-vm server1
$datastores = get-datastore | where name -like "datastore*"

$vm | Get-HardDisk |sort filename

CapacityGB      Persistence           Filename
----------            -----------                      --------
80.000          Persistent                 [datastore1] server1/server1.vmdk
120.000         Persistent                [datastore1] server1/server1_1.vmdk
120.000         Persistent                [datastore1] server1/server1_2.vmdk
2,000.000       Persistent              [datastore2] server1/server1.vmdk

foreach ($ds in $datastores){
    Write-host "$ds`n"
    get-vm -Datastore $ds | select name, vmhost, UsedSpaceGB | ft -auto
}

(Output I get, becuase $_.UsedSpaceGB is the total for the VM, combining all datastores.)

Datastore1

Name      VMHost                              UsedSpaceGB
----             ------                                       -----------
server1   esxhost1.company.com       2320.00


Datastore2

Name     VMHost                               UsedSpaceGB
----           ------                                           -----------
server1   esxhost1.company.com       2320.00


Output desired:

Datastore1

Name      VMHost                              UsedSpaceGB
----      ------                                              -----------
server1   esxhost1.company.com       320.00


Datastore2

Name      VMHost                                UsedSpaceGB
----            ------                                        -----------
server1   esxhost1.company.com       2000.00

Cheers,

sdolcourt

$vm = get-vm server1
$datastores = get-datastore | where name -like "datastore*"
 
$vm | Get-HardDisk |sort filename
 
CapacityGB      Persistence                                        Filename
----------      -----------                                        --------
80.000          Persistent                [datastore1] server1/server1.vmdk
120.000         Persistent              [datastore1] server1/server1_1.vmdk
120.000         Persistent              [datastore1] server1/server1_2.vmdk
2,000.000       Persistent                [datastore2] server1/server1.vmdk
 
foreach ($ds in $datastores){
    Write-host "$ds`n"
    get-vm -Datastore $ds | select name, vmhost, UsedSpaceGB | ft -auto
}
 
Output I get, becuase $_.UsedSpaceGB is the total for the VM, combining all datastores.
 
Datastore 1
 
Name      VMHost                     UsedSpaceGB
----      ------                     -----------
server1   esxhost1.company.com       2320.00
 
 
Datastore 2
 
Name      VMHost                     UsedSpaceGB
----      ------                     -----------
server1   esxhost1.company.com       2320.00
 
 
Output I desire; how do I achieve this?
 
Datastore 1
 
Name      VMHost                     UsedSpaceGB
----      ------                     -----------
server1   esxhost1.company.com       320.00
 
 
Datastore 2
 
Name      VMHost                     UsedSpaceGB
----      ------                     -----------
server1   esxhost1.company.com       2000.00
2 Replies
LucD
Leadership
Leadership

Try something like this.

Please note that this will only show the amount of storage used by the harddisks (VMDK), and does not include any of the other files related to a MV.

Get-VM |

  ForEach-Object -Process {

  $obj = [ordered]@{

   Name = $_.Name

  }

  Get-HardDisk -VM $_ |

   Group-Object -Property {$_.Filename.Split(']')[0].TrimStart('[')} |

   ForEach-Object -Process {

   $obj.Add($_.Name, ($_.Group | Measure-Object -Property CapacityGB -Sum | select -ExpandProperty Sum))

  }

  New-Object PSObject -Property $obj | Out-Default

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

sdolcourt1
Contributor
Contributor

Hi, LucD,

Thank you, I'll give it a try.

I've found variations of your scipts online, and have used it for code ideas.  Thank you for all your efforts!

Cheers,

Seth

0 Kudos