VMware Cloud Community
Stullma
Contributor
Contributor
Jump to solution

PowerCLI VM Disk Usage

I have this script that I borrowed from another post.   I would like to add what Cluster the VMs reside in as well....Having a hard time since I'm fairly new to this

$report = @() 

foreach($vm in Get-VM){ 

    Get-HardDisk -VM $vm | ForEach-Object { 

        $HardDisk = $_ 

        $row = "" | Select Hostname, VM, GuestName, Datastore, VMXpath, HardDisk, DiskType, CapacityGB, DiskFreespace, TotalVMFSConsumed, ProvisionType 

                    $row.Hostname = $vm.VMHost.Name 

                    $row.VM = $VM.Name 

                    $row.GuestName = $vm.Guest.HostName 

                    $row.Datastore = $HardDisk.Filename.Split("]")[0].TrimStart("[") 

                    $row.VMXpath = $HardDisk.FileName 

                    $row.HardDisk = $HardDisk.Name 

                    $row.CapacityGB = ("{0:f1}" -f ($HardDisk.CapacityKB/1MB)) 

            $row.DiskFreespace = $vm.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select -ExpandProperty Sum 

$row.DiskType = $HardDisk.get_DiskType() 

$row.TotalVMFSConsumed = $vm.get_UsedSpaceGB() 

$row.ProvisionType = $HardDisk.StorageFormat 

                    $report += $row 

  } 

$report | Export-Csv -Path C:\VMDisk-CapacityReport.csv -NoTypeInformation -UseCulture 

Thank you all very much for your assistance

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

$report = @()

foreach($vm in Get-VM){

   Get-HardDisk -VM $vm | ForEach-Object {

   $HardDisk = $_

   $row = "" | Select Hostname, Cluster, VM, GuestName, Datastore, VMXpath, HardDisk, DiskType, CapacityGB, DiskFreespace, TotalVMFSConsumed, ProvisionType

   $row.Hostname = $vm.VMHost.Name

   $row.Cluster = (Get-Cluster -VM $vm ).Name

   $row.VM = $VM.Name

   $row.GuestName = $vm.Guest.HostName

   $row.Datastore = $HardDisk.Filename.Split("]")[0].TrimStart("[")

   $row.VMXpath = $HardDisk.FileName

   $row.HardDisk = $HardDisk.Name

   $row.CapacityGB = ("{0:f1}" -f ($HardDisk.CapacityKB/1MB))

   $row.DiskFreespace = $vm.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select -ExpandProperty Sum

   $row.DiskType = $HardDisk.get_DiskType()

   $row.TotalVMFSConsumed = $vm.get_UsedSpaceGB()

   $row.ProvisionType = $HardDisk.StorageFormat

   $report += $row

   }

}


$report | Export-Csv -Path C:\VMDisk-CapacityReport.csv -NoTypeInformation -UseCulture


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

View solution in original post

5 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

$report = @()

foreach($vm in Get-VM){

   Get-HardDisk -VM $vm | ForEach-Object {

   $HardDisk = $_

   $row = "" | Select Hostname, Cluster, VM, GuestName, Datastore, VMXpath, HardDisk, DiskType, CapacityGB, DiskFreespace, TotalVMFSConsumed, ProvisionType

   $row.Hostname = $vm.VMHost.Name

   $row.Cluster = (Get-Cluster -VM $vm ).Name

   $row.VM = $VM.Name

   $row.GuestName = $vm.Guest.HostName

   $row.Datastore = $HardDisk.Filename.Split("]")[0].TrimStart("[")

   $row.VMXpath = $HardDisk.FileName

   $row.HardDisk = $HardDisk.Name

   $row.CapacityGB = ("{0:f1}" -f ($HardDisk.CapacityKB/1MB))

   $row.DiskFreespace = $vm.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select -ExpandProperty Sum

   $row.DiskType = $HardDisk.get_DiskType()

   $row.TotalVMFSConsumed = $vm.get_UsedSpaceGB()

   $row.ProvisionType = $HardDisk.StorageFormat

   $report += $row

   }

}


$report | Export-Csv -Path C:\VMDisk-CapacityReport.csv -NoTypeInformation -UseCulture


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

Stullma
Contributor
Contributor
Jump to solution

THANK YOU SO MUCH LucD!!!!

I was close after I posted this, but I now see where my mistakes were.  Thanks for the education

Reply
0 Kudos
Krishnamurti
Contributor
Contributor
Jump to solution

Hi All,

I have tried to pull the report however I cannot find the disk free space or disk free space percent correctly pulling the information.

Can someone help on this.

foreach($vm in Get-VM){

   Get-HardDisk -VM $vm | ForEach-Object {

   $HardDisk = $_

   $row = "" | Select Hostname, Cluster, VM, GuestName, Datastore, VMXpath, HardDisk, DiskType, CapacityGB, DiskFreespace, TotalVMFSConsumed, ProvisionType

   $row.Hostname = $vm.VMHost.Name

   $row.Cluster = (Get-Cluster -VM $vm ).Name

   $row.VM = $VM.Name

   $row.GuestName = $vm.Guest.HostName

   $row.Datastore = $HardDisk.Filename.Split("]")[0].TrimStart("[")

   $row.VMXpath = $HardDisk.FileName

   $row.HardDisk = $HardDisk.Name

   $row.CapacityGB = ("{0:f1}" -f ($HardDisk.CapacityKB/1MB))

   $row.DiskFreespace = $vm.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select -ExpandProperty Sum

   $row.DiskType = $HardDisk.get_DiskType()

   $row.TotalVMFSConsumed = $vm.get_UsedSpaceGB()

   $row.ProvisionType = $HardDisk.StorageFormat

   $report += $row

   }

}

Thanks

Krishna

Reply
0 Kudos
licensing3
Contributor
Contributor
Jump to solution

# This VMware PowerCLI Script will list all VM's in your vCenter and dump to a file the latest disk I/O Usage status

# This script has been tested on VMware Center 8.X and works for me at least; your mileage may vary...
# The file will be located in the same directory as the script results.csv
# Before using the script you will need to provide your credentials below under vCenter Connection Details
#
# Import VMware PowerCLI module
Import-Module VMware.PowerCLI

# Ignore SSL certificate validation
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

# vCenter Connection Details
#$vcenterServer = "vcenter name or IP"
#$username = "username@domain.local"
#$password = "Password"

# Connect to vCenter
Connect-VIServer -Server $vcenterServer -User $username -Password $password

# List of specific metrics to retrieve
$metrics = @(
"virtualdisk.mediumseeks.latest",
"virtualdisk.smallseeks.latest",
"virtualdisk.writeiosize.latest",
"virtualdisk.writelatencyus.latest",
"virtualdisk.readoio.latest",
"virtualdisk.totalwritelatency.average",
"virtualdisk.totalreadlatency.average",
"virtualdisk.numberwriteaveraged.average",
"virtualdisk.numberreadaveraged.average",
"virtualdisk.write.average",
"virtualdisk.read.average",
"virtualdisk.readloadmetric.latest",
"virtualdisk.readlatencyus.latest",
"virtualdisk.writeloadmetric.latest",
"virtualdisk.writeoio.latest",
"virtualdisk.readiosize.latest",
"virtualdisk.largeseeks.latest"
)

# Function to get a specific virtual disk statistic for a VM
function Get-SpecificVirtualDiskStat {
param (
[Parameter(Mandatory=$true)]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine]
$VM,
[Parameter(Mandatory=$true)]
[string]
$MetricName
)

$stat = Get-Stat -Realtime -MaxSamples 1 -Entity $VM -Stat $MetricName -ErrorAction SilentlyContinue
return $stat
}

# Create an array to hold the results
$results = @()

# Iterate through all VMs
Get-VM | ForEach-Object {
$vm = $_
$vmName = $vm.Name

# Temporary object to hold all metrics for a VM
$vmMetrics = New-Object PSObject -Property @{ VMName = $vmName }

# Iterate through each metric for the VM
foreach ($metric in $metrics) {
$stat = Get-SpecificVirtualDiskStat -VM $vm -MetricName $metric
if ($stat -and $stat -ne "System.Object[]") {
# Check if the response is an array, and set to null if it is
$value = if ($stat.Value -is [Array]) { $null } else { $stat.Value }
$vmMetrics | Add-Member -MemberType NoteProperty -Name $metric -Value $value
} else {
$vmMetrics | Add-Member -MemberType NoteProperty -Name $metric -Value $null
}
}

$results += $vmMetrics
}

# Export the results to a CSV file
$results | Export-Csv -Path "results.csv" -NoTypeInformation

# Disconnect from vCenter
Disconnect-VIServer -Server $vcenterServer -Confirm:$false

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Nice script, but you could significantly lower the execution time by limiting the number of calls to Get-Stat.

I would use 1 call to Get-Stat for all VMs and all metrics, and then use the Group-Object cmdlet to split out the results per VM and metric.
There are some examples available of that technique in this community, search on Get-Stat and Group-Object.


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

Reply
0 Kudos