Hi Friends
Can someone help me to get the powercli scripts to get VM folder wise memory usage with metricks like Consumed,Active,Granted for two week with particular time period for example buisness hours with output to csv file like below
Vmname | Esxhost | Activememory | Grantedmemory | Consumed memory | Folder |
---|---|---|---|---|---|
It looks like the Statistics Level for Historical Interval 3 (past month) is not set to at least 2.
The active and granted counters both require level 1.
The consumed requires level 1
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
What do you already have ?
Where do you get stuck ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Lucd
below is the scrip which i hve tried and i didnt get the output values for active,consumed,granted and please let me know how to get the esxhost name to the corresponding Vm in output csv files
Add-PSSnapin -Name "VMware.VimAutomation.Core"
Connect-VIServer "VCserver"
Write-host "Gathering MEM utilization VMs for last 2 weeks"
$metrics = "mem.active.average","mem.consumed.average","mem.granted.average"
#$start = (Get-Date).AddDays(-7)
#$start=$todayMidnight.AddDays(-14) -Finish $todayMidnight.AddDays(-7)
$todayMidnight = (Get-Date -Hour 0 -Minute 0 -Second 0).AddMinutes(-1)
$workingDays = "Monday","Tuesday","Wednesday","Thursday","Friday"
$dayStart = New-Object DateTime(2014,07,01,00,00,0) # 00:00 AM
$dayEnd = New-Object DateTime(2014,07,20,23,59,0) # 23:59 PM
$folders = Get-Folder -Location folder1
&{foreach($folder in $folders){
$vms = Get-VM -Location $folder
if($vms){
$stats = Get-Stat -Entity $vms -Stat $metrics -Start $todayMidnight.AddDays(-15) -Finish $todayMidnight.AddDays(-1) -ErrorAction SilentlyContinue
#$stats = Get-Stat -Entity $vms -Stat $metrics -Start $todayMidnight.AddDays(-7) -ErrorAction SilentlyContinue
$report = $stats | Where-Object {
$workingDays -contains $_.Timestamp.DayOfWeek -and
$_.Timestamp.TimeOfDay -gt $dayStart.TimeOfDay -and
$_.Timestamp.TimeOfDay -lt $dayEnd.TimeOfDay
}
if($report){
$report | Group-Object -Property {$_.Entity.Name} | %{
$Memactive = $_.Group | where {$_.MetricId -eq "Mem.active.average"} | Measure-Object -Property Value -Average
$Memconsumed = $_.Group | where {$_.MetricId -eq "Mem.consumed.average"} | Measure-Object -Property Value -Average
$Memgranted = $_.Group | where {$_.MetricId -eq "Mem.granted.average"} | Measure-Object -Property Value -Average
New-Object PSObject -Property @{
Folder = $folder.Name
VM = $_.Values[0]
Memactive = $Memactive
Memconsumed = $Memconsumed
Memgranted = $Memgranted
}
}
}
}}} | Export-Csv C:\users\administrator\desktop\vm.csv -NoTypeInformation -UseCulture
You seem to have forgotten the selection of the Average
Try like this
Add-PSSnapin -Name "VMware.VimAutomation.Core"
Connect-VIServer "VCserver"
Write-host "Gathering MEM utilization VMs for last 2 weeks"
$metrics = "mem.active.average","mem.consumed.average","mem.granted.average"
$todayMidnight = (Get-Date -Hour 0 -Minute 0 -Second 0).AddMinutes(-1)
$workingDays = "Monday","Tuesday","Wednesday","Thursday","Friday"
$dayStart = New-Object DateTime(2014,07,01,00,00,0) # 00:00 AM
$dayEnd = New-Object DateTime(2014,07,20,23,59,0) # 23:59 PM
$folders = Get-Folder -Location Folder1
&{foreach($folder in $folders){
$vms = Get-VM -Location $folder
if($vms){
$stats = Get-Stat -Entity $vms -Stat $metrics -Start $todayMidnight.AddDays(-15) -Finish $todayMidnight.AddDays(-1) -ErrorAction SilentlyContinue
$report = $stats | Where-Object {
$workingDays -contains $_.Timestamp.DayOfWeek -and
$_.Timestamp.TimeOfDay -gt $dayStart.TimeOfDay -and
$_.Timestamp.TimeOfDay -lt $dayEnd.TimeOfDay
}
if($report){
$report | Group-Object -Property {$_.Entity.Name} | %{
$Memactive = $_.Group | where {$_.MetricId -eq "Mem.active.average"} | Measure-Object -Property Value -Average | Select -ExpandProperty Average
$Memconsumed = $_.Group | where {$_.MetricId -eq "Mem.consumed.average"} | Measure-Object -Property Value -Average | Select -ExpandProperty Average
$Memgranted = $_.Group | where {$_.MetricId -eq "Mem.granted.average"} | Measure-Object -Property Value -Average | Select -ExpandProperty Average
New-Object PSObject -Property @{
Folder = $folder.Name
VM = $_.Values[0]
Memactive = $Memactive
Memconsumed = $Memconsumed
Memgranted = $Memgranted
}
}
}
}}} | Export-Csv C:\users\administrator\desktop\vm.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD
Thanks for the quick response ,i have tried with the modified script but getting the below output with no values on active and granted column and also i need to get ESX host name on output CSV file with Respective VMs where it is residing
Memactive | Memgranted | Folder | Memconsumed | VM |
Test | 5191640.667 | VM-Test1 | ||
Test | 7669387 | VM-Test2 | ||
Test | 2688585.458 | VM-Test3 | ||
Test | 4193169.392 | VM-Test4 | ||
Test | 4193140.567 | VM-Test5 | ||
Test | 4146308.945 | VM-Test6 | ||
Test | 4190891.483 | VM-Test7 |
please help me on this
It looks like the Statistics Level for Historical Interval 3 (past month) is not set to at least 2.
The active and granted counters both require level 1.
The consumed requires level 1
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD
yes your correct currently all statistics level are at Level 1
please let me know that if i changed the level 1 to level 2 and immediately run the script will i get the output( i tried but no luck got the same output:smileysilly: as above one)
and please let me know how to get the ESX host name correspond to the VM where its residing with this script
I'm afraid you will have to wait at least 2 weeks (if you want data from the past 2 weeks).
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
ya got it once changed the statistics level then only the vcenter will start capture the data
please help me on to get the esx host details of that VM
Try changing the New-Object like this
New-Object PSObject -Property @{
Folder = $folder.Name
VM = $_.Values[0]
Host = $_.Group[0].Entity.Host.Name
Memactive = $Memactive
Memconsumed = $Memconsumed
Memgranted = $Memgranted
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD
Thanks for the timely help and Thank you very much.got the exact output from the script what i need
How can we modify the script to export usage in Gb ?
Since the value is in KB, you'll have to divide by 1 MB.
And you probably want to round the value.
Something like this
$Memactive = [math]::Round(($_.Group | where {$_.MetricId -eq "Mem.active.average"} | Measure-Object -Property Value -Average | Select -ExpandProperty Average)/1MB)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference