PowerCLI

 View Only
  • 1.  PowerCLI VM Disk Usage

    Posted Oct 03, 2018 02:45 PM

    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



  • 2.  RE: PowerCLI VM Disk Usage
    Best Answer

    Posted Oct 03, 2018 03:23 PM

    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



  • 3.  RE: PowerCLI VM Disk Usage

    Posted Oct 03, 2018 03:55 PM

    THANK YOU SO MUCH LucD!!!!

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



  • 4.  RE: PowerCLI VM Disk Usage

    Posted Feb 14, 2019 10:50 PM

    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



  • 5.  RE: PowerCLI VM Disk Usage

    Posted Jan 09, 2024 09:45 PM

    # 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



  • 6.  RE: PowerCLI VM Disk Usage

    Posted Jan 10, 2024 07:30 AM

    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.