VMware PowerCli Script to create a report for CPU and MEM statistics for last 1 month

Following script will give you CPU, MEM statistics in Production Cluster for last 1 month. The script will generate the output in CSV format.
———————————————-

$capacityinfo = @()
$Date = Get-Date
$cluv = Get-Cluster Production | Get-View
    if ((Get-Cluster $cluv.name|Get-VM).count -gt 0){
   
        #Cluster CPU statistics for last 1 month
  
    $DasRealCpuCapacity = $cluv.Summary.EffectiveCpu – (($cluv.Summary.EffectiveCpu*$cluv.Configuration.DasConfig.FailoverLevel)/$cluv.Summary.NumEffectiveHosts)
        $CluCpuUsage = get-stat -entity $cluv.name -stat cpu.usagemhz.average -Start ($Date).adddays(-15) -Finish ($Date)
        $CluCpuUsageAvg = ($CluCpuUsage|Where-object{$_.value -gt ($CluCpuUsage|Measure-Object -average -Property value).average}|Measure-Object -Property value -Average).Average
        $VmCpuAverage = $CluCpuUsageAvg/(Get-Cluster $cluv.name|Get-VM).count
        $CpuVmLeft = [math]::round(($DasRealCpuCapacity-$CluCpuUsageAvg)/$VmCpuAverage,0)

        #Cluster MEM statistics for last 1 month

        $DasRealMemCapacity = $cluv.Summary.EffectiveMemory – (($cluv.Summary.EffectiveMemory*$cluv.Configuration.DasConfig.FailoverLevel)/$cluv.Summary.NumEffectiveHosts)
        $CluMemUsage = get-stat -entity $cluv.name -stat mem.consumed.average -Start ($Date).adddays(-15) -Finish ($Date)
        $CluMemUsageAvg = ($CluMemUsage|Where-object{$_.value -gt ($CluMemUsage|Measure-Object -average -Property value).average}|Measure-Object -Property value -Average).Average/1024
        $VmMemAverage = $CluMemUsageAvg/(Get-Cluster $cluv.name|Get-VM).count
        $MemVmLeft = [math]::round(($DasRealMemCapacity-$CluMemUsageAvg)/$VmMemAverage,0)
       
        #You can change following fields as per your requirements

        $clucapacity = “” |Select ClusterName, NumHosts, CluRealCpuCapacityMhz, CluCpuUsageAvgMhz, CluRealMemCapacityMB, CluMemUsageAvgMB, “Est Avail VM CPU(Mhz)”,”Est Avail VM Mem (MB)”
        $clucapacity.ClusterName = $cluv.name
        $clucapacity.NumHosts = $cluv.Summary.numhosts
        $clucapacity.CluRealCpuCapacityMhz = $DasRealCpuCapacity
        $clucapacity.CluCpuUsageAvgMhz = $CluCpuUsageAvg
        $clucapacity.CluRealMemCapacityMB = $DasRealMemCapacity
        $clucapacity.CluMemUsageAvgMB = $CluMemUsageAvg
        $clucapacity.”Est Avail VM CPU(Mhz)” = ($CpuVmLeft * $VmCpuAverage)
        $clucapacity.”Est Avail VM Mem (MB)” = ($MemVmLeft * $VmMemAverage)
        }
        $capacityinfo += $clucapacity
$capacityinfo |Export-Csv ClusterReport.csv -NoTypeInformation

Leave a Reply

Your email address will not be published. Required fields are marked *