VMware Cloud Community
123veer
Enthusiast
Enthusiast
Jump to solution

VM Guest HDD percentage

$report1 = Get-Datastore "yyyyyy" |Get-VM |

Select Name,

@{N="GuestUsageGB";E={[math]::Round(($_.Guest.Disks | %{$_.CapacityGB - $_.FreeSpaceGB} | Measure-Object -Sum | Select -ExpandProperty Sum),1)}},

@{N=“GuestCapacityGB";E={[math]::Round(($_.Guest.Disks | %{ $_.Capacity / 1GB} | Measure-Object -Sum | Select -ExpandProperty Sum),2)}},

@{N=“FreeSpaceGB";E={[math]::Round(($_.Guest.Disks | %{ $_.FreeSpace / 1GB} | Measure-Object -Sum | Select -ExpandProperty Sum),2)}},

@{N="GuestUsage(%)";E={[math]::Round(($_.Guest.Disks | %{ 100-(100*($_.FreeSpaceGB / $_.CapacityGB))} | Measure-Object -Average | Select -ExpandProperty Average),2)}},

@{N='SnapshotSizeGB';E={[math]::Round((Get-Snapshot -VM $_ | Measure-Object -Property SizeGB -sum).Sum,2)}}

$sum = $report1 | Measure-Object -sum "GuestUsageGB","GuestCapacityGB",“FreeSpaceGB"

$row = "" | Select "GuestUsageGB","GuestCapacityGB",“FreeSpaceGB"

$row."GuestUsageGB" = 'Total'

$row."GuestUsageGB" = $sum | where{$_.Property -eq "GuestUsageGB"} | select -ExpandProperty Sum

$row."GuestCapacityGB" = $sum | where{$_.Property -eq "GuestCapacityGB"} | select -ExpandProperty Sum

$row."FreeSpaceGB" = $sum | where{$_.Property -eq "FreeSpaceGB"} | select -ExpandProperty Sum

$report1 += $row

$report1 | export-csv VMHDD_REPORT_$datefile.csv -NoTypeInformation

I am using the above script to get Guest HDD usage but having issue at (Guestusage %) percentage, its not providing the correct values.

30 Replies
123veer
Enthusiast
Enthusiast
Jump to solution

when i have changed the last line result of CSV file was showing only one system in 400 Rows that is the  last VM which it is looking at

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you please attach the script you are using (as a file)?


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

0 Kudos
123veer
Enthusiast
Enthusiast
Jump to solution

not able to find option to attach file

#Initialize PowerCLI

#add-pssnapin VMware.VimAutomation.Core

Import-Module VMware.VimAutomation.Core

# Farm Login

$vCUser="xxxxx"

$vCPass="yyyyyy"

# LIST OF FARM

$vCenterIP = "17234554"

foreach ($IPAddress in $vCenterIP){

    # Connessione a vCenter

    Connect-VIServer $IPAddress -User $vCUser -Password $vCPass -port 443

}

#Variables

$Date = get-date

$Datefile = ( get-date ).ToString("yyyy-MM-dd-hhmm")

Get-Datastore -PipelineVariable ds | Get-VM |

ForEach-Object -Process {

    Write-Host "Looking at $($_.Name)"

    $obj = [ordered]@{

        VM = $_.Name

        VMState = $_.ExtensionData.summary.runtime.powerState

        Boottime = $_.ExtensionData.Runtime.BootTime

        TotalNics = $_.ExtensionData.summary.config.numEthernetCards

        Portgroup = Get-View -Id $_.ExtensionData.Network -Property Name | select -ExpandProperty Name

        OS = $_.ExtensionData.Config.GuestFullName

        Hostname = $_.ExtensionData.guest.hostname

        IPAddress = $_.ExtensionData.guest.ipAddress

        VMHost = Get-View -Id $_.ExtensionData.Runtime.Host -property Name | select -ExpandProperty Name

        ProvisionedSpaceGB = [math]::Round(($_.ExtensionData.Summary.Storage.Committed + $vm.Summary.Storage.UnCommitted)/1GB,0)

        TotalUsedSpaceGB = [math]::Round($_.ExtensionData.Summary.Storage.Committed/1GB,0)

        MemoryReservation = $_.ExtensionData.resourceconfig.memoryallocation.reservation

        CPUreservation = $_.ExtensionData.resourceconfig.cpuallocation.reservation     

        TotalCPU = $_.ExtensionData.summary.config.numcpu

        TotalMemoryGB = [math]::Round($_.ExtensionData.summary.config.MemorysizeMB/1KB,0)

        Memory = $_.ExtensionData.summary.config.memorysizemb

        MemoryUsage = $_.ExtensionData.summary.quickStats.guestMemoryUsage 

        ToolsStatus = $_.ExtensionData.guest.toolsstatus

        ToolsVersion = $_.ExtensionData.config.tools.toolsversion

        TimeSync = $_.ExtensionData.Config.Tools.SyncTimeWithHost

        HardwareVersion = $_.ExtensionData.config.Version

        Datastore = $_.ExtensionData.Config.DatastoreUrl[0].Name

        CBT = $_.ExtensionData.Config.ChangeTrackingEnabled

        Notes = $_.ExtensionData.Config.Annotation.ToString()

        FaultTolerance = $_.ExtensionData.Runtime.FaultToleranceState

        SnapshotName = &{$script:snaps = Get-Snapshot -VM $_.ExtensionData.Name; $script:snaps.Name -join ","}

        SnapshotDate = $script:snaps.Created -join ","

        SnapshotSizeGB = $script:snaps.SizeGB -join ","

        ToolsState = $_.Guest.State

        GuestUsageGB = [math]::Round(($vm.Guest.Disks | %{$_.CapacityGB - $_.FreeSpaceGB} | Measure-Object -Sum | Select -ExpandProperty Sum),1)

        GuestCapacityGB = [math]::Round(($_.Guest.Disks | %{ $_.Capacity / 1GB} | Measure-Object -Sum | Select -ExpandProperty Sum),2)

        FreeSpaceGB = [math]::Round(($_.Guest.Disks | %{ $_.FreeSpace / 1GB} | Measure-Object -Sum | Select -ExpandProperty Sum),2)

        'GuestUsage(%)' = &{

            $capacityGB = $_.Guest.Disks | Measure-Object -Property CapacityGB -Sum | Select -ExpandProperty Sum

            $freeGB = $_.Guest.Disks | Measure-Object -Property FreeSpaceGB -Sum | Select -ExpandProperty Sum

            if($capacityGB){

                [math]::Round((($capacityGB - $freeGB)/$capacityGB*100),2)

            }

        }

        VMDKSizeGB = (Get-HardDisk -VM $_ | Measure-Object -Property CapacityGB -Sum).Sum

        System_Group = (Get-TagAssignment -Category "System Group" -Entity $_).Tag.Name

        System_Type = (Get-TagAssignment -Category "System Type" -Entity $_).Tag.Name

    }

    $diskNum = 1

    if($_.Guest.Disks){

        $_.Guest.Disks | ForEach-Object -Process {

            $obj.Add("Disk$($DiskNum)_Path",$_.Path)

            $obj.Add("Disk$($DiskNum)_Capacity(GB)",([math]::Round($_.CapacityGB,1)))

            $obj.Add("Disk$($DiskNum)_FreeSpace(GB)",([math]::Round(($_.FreeSpaceGB),1)))

            $obj.Add("Disk$($DiskNum)_USedSpace(GB)",([math]::Round($_.CapacityGB - $_.FreeSpaceGB,1)))

            $obj.Add("Disk$($DiskNum)_USedSpace%",([math]::Round((1 - ($_.FreeSpace)/$_.Capacity)*100,1)))

            $DiskNum++

        }

    }

    New-Object PSObject -Property $obj

} | Sort-Object -Property {($_ | Get-Member -MemberType NoteProperty).Count} -Descending |

Export-Csv -Path d:\report1\report.csv -NoTypeInformation -UseCulture

0 Kudos
123veer
Enthusiast
Enthusiast
Jump to solution

result out putUntitled234.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

attach.png


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

0 Kudos
123veer
Enthusiast
Enthusiast
Jump to solution

dont have the option

Capture12.PNG

0 Kudos
123veer
Enthusiast
Enthusiast
Jump to solution

Please find attached script and result file

0 Kudos
LucD
Leadership
Leadership
Jump to solution

When I run you script in my test environment, it works without a problem and reports all the info.
What I find strange is that you have columns like Disk0_Path in your report.
There shouldn't be a 0 but those properties should start with 1 ($disknum).

Did you stop/start your PowerShell/PowerCLI environment before running the script?
How you run the script? From a .ps1 file at the prompt?

The script you attached is not the one you ran to produce the attached CSV file?


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

123veer
Enthusiast
Enthusiast
Jump to solution

i am running the script Power shell ISE mode and i have not stop the powershell environment. The same script i am running. let me stop the powershell environment and start it gain

0 Kudos
123veer
Enthusiast
Enthusiast
Jump to solution

now report is coming sorry for mistake i was not doing start/stop powershell environment. Thanks for your help.

LucD
Leadership
Leadership
Jump to solution

No problem, that sometimes happens when using the ISE.

I'm a bit amazed that you mark your own answer as the Correct Answer?!?


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