Hi Luc,
can you help me out in this scipt. I have a Problem with the stat parameter. The script is not able to calculate the stat´s. Takes way to long. PS, it is a hugh environment. I reduced -Maxsamples already to 100.
$Cluster = "CL01"
$group = "DBhosts"
$drsgroup = Get-DrsClusterGroup -name $group -Cluster $cluster
$days = 15
$Hosts = $drsgroup.Member
$allhosts = @()
foreach($vmHost in $hosts){
$hoststat = "" | Select HostName, MemoryInstalled, MemoryAllocated, MemoryConsumed, MemoryUsage,MemoryFree, CPUMax, CPUAvg, CPUMin
$hoststat.HostName = $vmHost.name
$statcpu = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-$days) -Finish (Get-Date)-MaxSamples 100 -stat cpu.usage.average # problem
$statmemconsumed = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-$days) -Finish (Get-Date)-MaxSamples 10000 -stat mem.consumed.average
$statmemusage = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-$days) -Finish (Get-Date)-MaxSamples 10000 -stat mem.usage.average
$statmemallocated = Get-VMhost $vmHost.name | Select @{N="allocated";E={$_ | Get-VM | %{$_.MemoryGB} | Measure-Object -Sum | Select -ExpandProperty Sum}}
$statmeminstalled = Get-VMHost $vmHost.name | select MemoryTotalGB
$statmemoryfree = Get-VMhost $vmHost.name | select @{N='Mem';E={[math]::Round(($_.MemoryTotalGB - $_.MemoryUsageGB),0)}} | select -ExpandProperty Mem
$statmeminstalled = $statmeminstalled.MemoryTotalGB
$cpu = $statcpu | Measure-Object -Property value -Average -Maximum -Minimum
$memconsumed = $statmemconsumed | Measure-Object -Property value -Average
$memusage = $statmemusage | Measure-Object -Property value -Average
$CPUMax = "{0:N0}" -f ($cpu.Maximum)
$CPUAvg = "{0:N0}" -f ($cpu.Average)
$CPUMin = "{0:N0}" -f ($cpu.Minimum)
$allocated = "{0:N0}" -f ($statmemallocated.allocated)
$consumed = "{0:N0}" -f ($memconsumed.Average/1024/1024)
$usage = "{0:P0}" -f ($memusage.Average/100)
$installed = "{0:N0}" -f ($statmeminstalled)
$free = "{0:N0}" -f ($statmemoryfree)
$CPUMax = $CPUMax.ToString() + " %"
$CPUAvg = $CPUAvg.ToString() + " %"
$CPUMin = $CPUMin.ToString() + " %"
$MemoryInstalled = $installed.ToString() + " GB"
$MemoryAllocated = $allocated.ToString() + " GB"
$MemoryConsumed = $consumed.ToString() + " GB"
$MemoryUsage = $usage.ToString()
$MemoryFree = $free.ToString() + " GB"
$hoststat.CPUMax = $CPUMax
$hoststat.CPUAvg = $CPUAvg
$hoststat.CPUMin = $CPUMin
$hoststat.MemoryInstalled = $MemoryInstalled
$hoststat.MemoryAllocated = $MemoryAllocated
$hoststat.MemoryConsumed = $MemoryConsumed
$hoststat.MemoryUsage = $MemoryUsage
$hoststat.Memoryfree = $MemoryFree
$allhosts += $hoststat
}
$allhosts | Select HostName, MemoryInstalled, MemoryAllocated, MemoryConsumed, MemoryUsage,MemoryFree, CPUMax, CPUAvg, CPUMin
If you are only interested in the aggregated values I would add the Instance parameter, especially when you have multi-core ESXi nodes.
I would also consolidate all the Get-Stat calls into just one Get-Stat call, then split out the individual metrics via something like Group-Object.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
even If I only run this against one host, it is failing
$statcpu = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-$days) -Finish (Get-Date)-MaxSamples 100 -stat cpu.usage.average # problem
Get-Stat : 22.05.2023 09:46:00 Get-Stat The request channel timed out while waiting for a reply after 00:05:00. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding.
The time allotted to this operation may have been a portion of a longer timeout.
At line:9 char:16
You can try increasing the WebOperationTimeoutSeconds value (default 300 seconds) with the Set-PowerCLICOnfiguration cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference