VMware Cloud Community
mtopaloff
Contributor
Contributor

VM storage report per Datastore

This is my first post but I have been using the Communities for serveral months for answers to my questions. I have new problem which I have been unable to find what I am looking for. I need a powercli script where I can input a specfic datastore (i.e. VMFSxxx) and it will list which VMs are using that datastore. It also need it to list how much of the datastore each VM is using in GBs, and I was hoping to have that all exported to a csv file. So if anyone has a script that will do all of this, or can point me in the direction of a post that would be great.

Reply
0 Kudos
4 Replies
mikeddib
Enthusiast
Enthusiast

Not necessarily the scripting guru, but maybe I can get you pointed in the right direction.

I started here...

http://www.virtu-al.net/2009/07/14/powercli-local-stored-vms-one-liner/

and then modified some of the options. You wanted to input the datastore name and then list how much each VM is using in GBs, and export to CSV. This is as far as I got.

$name = Read-Host "Enter the name of the datastore"

Connect-VIServer -Server myvCenterServer

Get-Datastore -Name $name | Get-VM | Get-HardDisk | Export-Csv C:\DS-VMs.csv

You'll get all the vmdk files for each VM. They show up in KB, and there is definitely a way to pipe that piece of output and manipulate it, but someone smarter than me would need to help with that. Also, you will only get the VMDK files by VM, but not any config files or more importantly the swap file which could be a decent size.

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

The next script will do exactly what you want:

$Datastore = "vmfs01"
Get-ChildItem (Get-Datastore $Datastore).DatastoreBrowserPath |
  ForEach-Object {
    If ($_.PSIsContainer) {
      $VM = $_.Name
      $Sum = 0
      Get-ChildItem $_ | `
        ForEach-Object {
          $Sum += $_.Length
        }
      "" | Select-Object @{N="Datastore";E={$Datastore}},@{N="VM";E={$VM}},@{N="CapacityGB";E={$Sum/1GB}}
    }
  } | Export-Csv -Path C:\temp\StoragePerVM.csv -NoTypeInformation

Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
mtopaloff
Contributor
Contributor

Thanks RvdNieuwendijk that worked great. I used that and created some batch files so that I can launch a single report for each my datastores.

Once again Thanks.

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

I'm glad you like it.

Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos