Hi
I am new to PowerCLI. I am looking to Automate getting "Report Performance" for set of VM's (not all) for a particular DATE/Time Range.
So basically i am looking to acheive the following
If anyone can guide me in right path it will be helpful. I am using latest version 5.1.0 Release 1
Thanks
Sukhdeep
Have a look at my PowerCLI & vSphere statistics – Part 2 – Come together post.
It has some samples that show how to report over specific time frames.
In my other posts in that series you'll find info on the other aspects of your questions.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD for Response
$esxName = "my-ESX-hostname" $esxImpl = Get-VMHost -Name $esxName $todayMidnight = (Get-Date -Hour 0 -Minute 0 -Second 0).AddMinutes(-1) $stats = Get-Stat -Entity $esxImpl -Stat cpu.usage.average -Start $todayMidnight.AddDays(-5) -Finish $todayMidnight.AddDays(-4) $groups = $stats | Group-Object -Property {$_.Timestamp.Hour, $_.Instance} $report = $groups | % { New-Object PSObject -Property @{ Description = $_.Group[0].Description Entity = $_.Group[0].Entity EntityId = $_.Group[0].EntityId Instance = $_.Group[0].Instance MetricId = $_.Group[0].MetricId Timestamp = $_.Group[0].Timestamp.Date.AddHours($_.Group[0].Timestamp.Hour) Unit = $_.Group[0].Unit Value = [math]::Round(($_.Group | Measure-Object -Property Value -Average).Average, 2) } } $report | Export-Csv "C:\Hourly-cpu.csv" -NoTypeInformation -UseCulture
In the above script, where can one Parameterize name of VM's ?
In that particular script I get the statistics for an ESX(i) host, but the same principle holds for some VMs.
Something like this for example (it will get statistics for all VMs)
$vmImpl = Get-VM $todayMidnight = (Get-Date -Hour 0 -Minute 0 -Second 0).AddMinutes(-1) $stats = Get-Stat -Entity $vmImpl -Stat cpu.usage.average -Start $todayMidnight.AddDays(-5) -Finish $todayMidnight.AddDays(-4) $groups = $stats | Group-Object -Property {$_.Timestamp.Hour, $_.Instance} $report = $groups | % { New-Object PSObject -Property @{ Description = $_.Group[0].Description Entity = $_.Group[0].Entity EntityId = $_.Group[0].EntityId Instance = $_.Group[0].Instance MetricId = $_.Group[0].MetricId Timestamp = $_.Group[0].Timestamp.Date.AddHours($_.Group[0].Timestamp.Hour) Unit = $_.Group[0].Unit Value = [math]::Round(($_.Group | Measure-Object -Property Value -Average).Average, 2) } } $report | Export-Csv "C:\Hourly-cpu.csv" -NoTypeInformation -UseCulture
It's just a matter of giving the objects for which you want statistics to the Entity parameter on the Get-Stat cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD
I modified the below script but i am unable to get CSV Report for all the "selected" VM. I get it for only "Machine2"
$vmImpl = Get-VM -Name Machine1,Machine2
$todayMidnight = (Get-Date -Hour 0 -Minute 0 -Second 0).AddMinutes(-1)
ForEach ($VM in $vmImpl) {
$stats = Get-Stat -Entity $VM -Stat cpu.usage.average -Start $todayMidnight.AddDays(-2) -Finish $todayMidnight.AddDays(-1)
$groups = $stats | Group-Object -Property {$_.Timestamp.Hour, $_.Instance}
$report = $groups | % {
New-Object PSObject -Property @{
Description = $_.Group[0].Description
Entity = $_.Group[0].Entity
EntityId = $_.Group[0].EntityId
Instance = $_.Group[0].Instance
MetricId = $_.Group[0].MetricId
Timestamp = $_.Group[0].Timestamp.Date.AddHours($_.Group[0].Timestamp.Hour)
Unit = $_.Group[0].Unit
Value = [math]::Round(($_.Group | Measure-Object -Property Value -Average).Average, 2)
}
}
}
$report | Export-Csv "C:\Hourly-cpu.csv" -NoTypeInformation -UseCulture
Try it like this
$report = @()
$vmImpl = Get-VM -Name Machine1,Machine2
$todayMidnight = (Get-Date -Hour 0 -Minute 0 -Second 0).AddMinutes(-1)
ForEach ($VM in $vmImpl) {
$stats = Get-Stat -Entity $VM -Stat cpu.usage.average -Start $todayMidnight.AddDays(-2) -Finish $todayMidnight.AddDays(-1)
$groups = $stats | Group-Object -Property {$_.Timestamp.Hour, $_.Instance}
$report += $groups | % {
New-Object PSObject -Property @{
Description = $_.Group[0].Description
Entity = $_.Group[0].Entity
EntityId = $_.Group[0].EntityId
Instance = $_.Group[0].Instance
MetricId = $_.Group[0].MetricId
Timestamp = $_.Group[0].Timestamp.Date.AddHours($_.Group[0].Timestamp.Hour)
Unit = $_.Group[0].Unit
Value = [math]::Round(($_.Group | Measure-Object -Property Value -Average).Average, 2)
}
}
}
$report | Export-Csv "C:\Hourly-cpu.csv" -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD. That works!
If you could help with two queries
Time | CPU % | MEM % |
---|---|---|
24th Oct 3:00 AM | 5% | 4% |
24th Oct 3:05 AM | 6.7% | 8% |
24th Oct 3:10AM | 7.6% | 4% |
Thanks for all the help
For the time range you can use the same principle as the script above.
You can also use AddHours and AddMinutes instead of AddDays to calculate your Start and Finish parameters.
The 5 minute intervals are only available for the last 24 hours.
After that they are aggregated into longer intervals, as I explained in my PowerCLI & vSphere statistics – Part 1 – The basics post.
The metrics you should use are cpu.usage.average and mem.usage.average.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks! Will try that
I did Interval 30 Minutes for Day(-1) and still i was getting for 1 hour... ? Any idea ?
When you use the IntervalMins parameter, you will get the closest available interval.
But 1 hour intervals are not common, unless you reconfigured the Historical Intervals on your vCenter.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD........All set now!
Just one challenging thing left now.
1. Say time now is 5 AM
2. I wish to get data from 2AM to 3AM
3. I do, Start -AddMinutes(-180) -Finish -AddMinutes(-120)
4.Is there a function to calculate difference current data/time and given date/time in minutes ?
Have a look at the New-Timespan cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I can't get this to work no matter what I do. I fixed the variable issue but the script still runs without any output in the report.csv file. I don't get any errors either. Can you see anything else wrong with the following script? I know your original script was for VM's but I assumed I could use it to pull the performance of all VM's on a particular Esxi host.
Thanks in advance.
Connect-VIServer myserver -User myuser -Password mypass
$allvms = @()
$vm = Get-VM | where {$_.PowerState -eq "PoweredOn"}
$dayStart = Get-Date -Hour 12 -Minute 0 -Second 0 -Month 11 -Day 17 -Year 2014
$dayEnd = Get-Date -Hour 17 -Minute 0 -Second 0 -Month 11 -Day 17 -Year 2014
$stat = 'cpu.usage.average','mem.usage.average'
$stats = Get-Stat -Entity $vm -Stat $stat -Start $dayStart -Finish $dayEnd
$report = $stats | Group-Object -Property {$_.Entity.Name} | %{
New-Object PSObject -Property @{
VM = $_.Name
CPUavg = $_.Group | Where {$_.MetricID -eq 'cpu.usage.average'} | Measure-Object -Property Value -Average | Select -ExpandProperty Average
Memavg = $_.Group | Where {$_.MetricID -eq 'mem.usage.average'} | Measure-Object -Property Value -Average | Select -ExpandProperty Average
}
}
$report | Select VM,CPUavg,Memavg |
Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture
I placed some debugging statements in the code, let me know what it says when you run this version
Connect-VIServer myserver -User myuser -Password mypass
$vm = Get-VM | where {$_.PowerState -eq "PoweredOn"}
Write-Output "VMs found $($vm.Count)"
$dayStart = Get-Date -Hour 12 -Minute 0 -Second 0 -Month 11 -Day 17 -Year 2014
$dayEnd = Get-Date -Hour 17 -Minute 0 -Second 0 -Month 11 -Day 17 -Year 2014
Write-Output "From $dayStart"
Write-Output "To $dayEnd"
$stat = 'cpu.usage.average','mem.usage.average'
$stats = Get-Stat -Entity $vm -Stat $stat -Start $dayStart -Finish $dayEnd
Write-Output "Stats returned $($stats.Count)"
$report = $stats | Group-Object -Property {$_.Entity.Name} | %{
New-Object PSObject -Property @{
VM = $_.Name
CPUavg = $_.Group | Where {$_.MetricID -eq 'cpu.usage.average'} | Measure-Object -Property Value -Average | Select -ExpandProperty Average
Memavg = $_.Group | Where {$_.MetricID -eq 'mem.usage.average'} | Measure-Object -Property Value -Average | Select -ExpandProperty Average
}
}
$report | Select VM,CPUavg,Memavg |
Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry about posting in the other thread. That's what I get for multitasking. I run the updated script you posted and this is the result of the
A block of text that says could not resolve the requested VC server......(in red)
VMs found 12
From 11/17/2014 12:00:00
To 11/17/2014 17:00:00
Stats returned.
Output file is still empty.