# encoding: ascii
# api: powershell
# title: Get-VMDiskUsagePerDS
# description: Fetches the per-datastore disk utilization of a virtual machine. Is thin-provisioning aware and reports only the thin provisioned used space for UsedGB.
# version: 0.1
# author: Justin Grote
# license: CC0
# x-poshcode-id: 6034
# x-archived: 2016-05-17T09:11:42
# x-published: 2016-09-30T23:02:00
#
#
$views = (get-vm | get-view)
$views += (get-template | get-view)

$VMDatastores = get-datastore

$views | foreach {

    $vm = $PSItem

     foreach ($VMDSUsage in $vm.storage.PerDatastoreUsage) {
        $dsReport = [ordered]@{}
        $VMDatastore = $VMDatastores | where { $_.ID -match "datastore-$($VMDSUsage.datastore.value)"}
        $dsReport.VMName = $vm.name
        $dsReport.Datastore = $VMDatastore.Name
        $dsReport.UsedGB = [math]::round(($VMDSUsage.committed / 1GB),2)
        $dsReport.ProvisionedGB = [math]::round((($VMDSUsage.committed + $VMDSUsage.Uncommitted) / 1GB),2)
        $dsReport.DatastoreUsagePct = [math]::round((($dsReport.UsedGB / $VMDatastore.CapacityGB) * 100),2)
        new-object PSObject -property $dsReport
    }


}
