VMware Cloud Community
HAL07
Contributor
Contributor

Reporting a per-disk usage (PerDatastoreUsage)

The following code will report commited disk usage, sorted per datastore usage. E.g. it will report twice a server if it has disks on two separate storage volumes.

E.g. if it has

Disk 1 at san_lun53_sata_raid6

Disk 2 at san_lun21_fc_raid5.

If it has 2 disks on the same datastorage, I think they will be added together and reported the total use.

Is there a way to make it always report more than once if it has more than one disk, even if the disks are on same datastorage?

It would also be very nice if it reported the disk name (e.g. "Disk 1" or similar would be fantastic).

Script:

#script taken from: http://communities.vmware.com/thread/230007

$VC = Connect-VIServer "vc01"
$report = @()
$allvms = Get-VM -name * | Sort-Object $_.datacenter
foreach ($vm in $allvms) {
     write-host $vm.name
     $vmview = $vm | Get-View                    # get-view to see details of the FM
     foreach($disk in $vmview.Storage.PerDatastoreUsage){      # Disks for the VM
      $dsview = (Get-View $disk.Datastore)                    # Datastore used by the disks
      $dsview.RefreshDatastoreStorageInfo()                    # refresh to get the latest data
      $vmview.Config.Name                              # Echo to the screen to show progress
      $row = "" | select HOST, VMNAME, POWERSTATE, CPUS, RAM, DATASTORE, VMSIZE_MB, VMUSED_MB, PERCENT     # blank row
      $row.HOST = $vm.Vmhost
      $row.POWERSTATE = $vm.PowerState
      $row.CPUS = $vm.NumCpu
      $row.RAM = $vm.MemoryMB
      $row.VMNAME = $vmview.Config.Name                         # Add the data to the row
      $row.DATASTORE = $dsview.Name
      $row.VMSIZE_MB = (($disk.Committed+$disk.Uncommitted)/1024/1024)
      $row.VMUSED_MB = (($disk.Committed)/1024/1024)
      $row.PERCENT = [int](($row.VMUSED_MB / $row.VMSIZE_MB)*100)
      $report += $row       # Add the row to the structure
   }
}
$report | Export-Csv "virtual computers.csv"

0 Kudos
7 Replies
LucD
Leadership
Leadership

Afaik, vCenter doesn't keep track of committed and uncomitted values on a virtualdisk basis.

Only on a datastore basis, as you discovered.


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

HAL07
Contributor
Contributor

okay thank you for clarifying that. is there a way to get the number of disks connected to a virtual computer?

0 Kudos
RvdNieuwendijk
Leadership
Leadership

You can use the NumVirtualDisks VIProperty made by Alan Renouf from Luc's VIProperties blog post. With that you can do:

New-VIProperty -Name NumVirtualDisks -ObjectType VirtualMachine `
  -Value {
    param($vm)

    $vm.ExtensionData.Summary.Config.numVirtualDisks
} -Force

Get-VM | Select-Object -Property Name,NumVirtualDisks

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

Don't know if this is what you're exactly looking for, but you should give it a try. This reports every vDisk seperatly with capacity and datastore info.

$VC = Connect-VIServer "vc01"

$report = @()

$virtualmachines = Get-VM

foreach ($vm in $virtualmachines)

{

foreach ($harddisk in $vm.harddisks)

{

$row = "" | select VMname, HarddiskName, Capacity, Datastore, Filename

$row.VMname = $vm.name

$row.HarddiskName = $harddisk.name

$row.Capacity = [Math]::round(([int]($harddisk.CapacityKB) /1mb), 2)

$filename = [string]$harddisk.Filename

$i=0

foreach ($char in $filename.ToCharArray())

{

if ($char -eq '['){$StartPos = $i}

if ($char -eq ']'){$EndPos = $i}

$i++

}

$datastorename = $filename.Substring(($StartPos+1), ($EndPos-$StartPos-1))

$row.Datastore = $datastorename

$row.Filename = $harddisk.Filename

$report += $row

}

}

$report | Sort-Object VMname, HarddiskName | Export-Csv "virtual computers.csv"

HAL07
Contributor
Contributor

Fantastic! thank you very much, rengelen!

Now a last question. Is it possible to detect if the virtual machine has snapshots?

0 Kudos
RvdNieuwendijk
Leadership
Leadership

You can use the PowerCLI Get-Snapshot cmdlet to list all the snapshots of a virtual machine. E.g.

Get-VM $VM | Get-Snapshot


To check if a VM has a snapshot you can do:

if (Get-VM $VM | Get-Snapshot) {"VM $VM has one or more snapshots."}
Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
HAL07
Contributor
Contributor

Uh sorry again. I didn't feel like creating another topic.

I have the following code. The code is mostly not mine but copied from various threads. I am trying to give a percent usage of thin disks. However this fails on me as it gives duplicate outputs if you have disks on multiple datastores...

If anybody know how to clean this up I would be most interested!

$VC = Connect-VIServer "vc01"

$report = @()
$allvms = Get-VM -name *| Sort-Object $_.datacenter
foreach ($vm in $allvms) {
     $vmview = $vm | Get-View                                # get-view to see details of the FM
     foreach($disk in $vmview.Storage.PerDatastoreUsage){    # Disks for the VM
            $dsview = (Get-View $disk.Datastore)             # Datastore used by the disks
            $dsview.RefreshDatastoreStorageInfo()            # refresh to get the latest data

           $vmview.Config.Name                              # Echo to the screen to show progress

           foreach ($harddisk in $vm.harddisks)
            {
                $harddisk
                $row = "" | select HOST, VMNAME, POWERSTATE, CPUS, RAM, DATASTORE, FILENAME, SIZE_GB, USED_GB, USED_PERCENT, HarddiskName, Capacity, SnapshotEnabled
                $row.HOST = $vm.Vmhost
                $row.VMNAME = $vmview.Config.Name
                $row.POWERSTATE = $vm.PowerState
                $row.CPUS = $vm.NumCpu
                $row.RAM = $vm.MemoryMB
                $row.SIZE_GB = [math]::round( (($disk.Committed+$disk.Uncommitted)/1024/1024/1024) , 2)
                $row.USED_GB = [math]::round( (($disk.Committed)/1024/1024/1024) , 2)
                $row.USED_PERCENT = [int](($row.USED_GB / $row.SIZE_GB)*100)
               
                #$row.VMname = $vm.name
                  $row.HarddiskName = $harddisk.name
                $row.Capacity = [Math]::round(([int]($harddisk.CapacityKB) /1mb), 2)
               
                $filename = [string]$harddisk.Filename

                $i=0
                foreach ($char in $filename.ToCharArray())
                {
                    if ($char -eq '['){$StartPos = $i}
                    if ($char -eq ']'){$EndPos = $i}
                    $i++
                }
               
                $datastorename = $filename.Substring(($StartPos+1), ($EndPos-$StartPos-1))
                $row.DATASTORE = $datastorename
                $filename = $filename.Replace($datastorename, "")
                $filename = $filename.Replace("[] ", "")
                $row.FILENAME = $filename

                if (Get-VM $vm | Get-Snapshot) {  $row.SnapshotEnabled = "Yes"}
               
                $report += $row           # Add the row to the structure
            }
   }
}
$report | Sort-Object HOST, HarddiskName | Export-Csv "virtual_computers.csv" -NoTypeInformation

0 Kudos