VMware Cloud Community
benso37
Contributor
Contributor

How to generate specific performance report with Powercli

I've looked around and found scripts here and there that can do some of what I need but nothing that can do exactly what I want. Unfortunately, I'm new to Powercli/Powershell so I don't know what I'm doing.

I need a script that can generate CPU and Memory utilization for specific dates and time. For example. 12PM to 5PM on the December 17th. Is this possible and can someone point me to a sample script I can modify?

Thanks in advance.

35 Replies
LucD
Leadership
Leadership

Have a look at the 2nd script in my PowerCLI & vSphere statistics – Part 2 – Come together post.


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

0 Kudos
benso37
Contributor
Contributor

LucD,

Thanks for the reply. This was actually one of the scripts I previously looked at. This will only give me the CPU metrics and it doesn't look like it I can modify it (or maybe I don't know how to) modify it to give me the exact date (17th) and time range (12PM to 5PM). The other thing I noticed with that script was that it was getting reports for the ESXi host. I need my report for the VM's on the ESXi hosts.

0 Kudos
benso37
Contributor
Contributor

Any other ideas?

0 Kudos
LucD
Leadership
Leadership

It's a matter of changing the value you pass on the Entity parameter and adding the additional metrics you require.

Can you do that, or do you need an example ?


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

0 Kudos
benso37
Contributor
Contributor

An example would be nice. I'm very new to this stuff. I just started reading a book on Powershell over the weekend. It looks like a good tool for admins in general.

0 Kudos
LucD
Leadership
Leadership

Try like this

$vmName = 'MyVM*'

$vm = Get-VM -Name $vmName

$dayStart = Get-Date "17/12/14 12:00"

$dayEnd = Get-Date "17/12/14 17:00"

$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

Note that you will have to update the value in $vmName.

If you want the report for all the VMs, just put an asterisk in there.

The way you enter the date/time strings depends on how the local settings on your station are configured.

An alternative is to do it like this

$dayStart = Get-Date -Day 17 -Month 12 -Year 2014 -Hour 12 -Minute 0 -Second 0

$dayEnd = Get-Date -Day 17 -Month 12 -Year 2014 -Hour 17 -Minute 0 -Second 0


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

benso37
Contributor
Contributor

LucD,

Thanks a lot of your help. I've modified the script a bit to work for my environment but the file generated is empty. Can you see what I'm doing wrong?

Thanks

Connect-VIServer servername -User myusername -Password mypassword

$allvms = @()

$vms = Get-VM | where {$_.PowerState -eq "PoweredOn"}

$dayStart = Get-Date -Day 17 -Month 11 -Year 2014 -Hour 12 -Minute 0 -Second 0

$dayEnd = Get-Date -Day 17 -Month 11 -Year 2014 -Hour 17 -Minute 0 -Second 0

$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

0 Kudos
LucD
Leadership
Leadership

You store that selected VMs in a variable called $vms, but on the Get-Stat cmdlet you use $vm.


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

0 Kudos
LucD
Leadership
Leadership

I think your last reply was in the wrong thread, see my answer in  13.  Re: Automate Report Performance for DATE/Time Range for Multiple VM's


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

0 Kudos
LucD
Leadership
Leadership

I'll reply in the correct thread :smileygrin:

From that last line it looks as if no statistical data was returned.

Do you collect the statistical data, do you see any data for the 17th of November between those hours in the vSphere client under the Performance tab for each of the VMs ?


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

0 Kudos
benso37
Contributor
Contributor

I do actually see the statistical information (under the performance tab) on 11/17/2014 all the way up to today. 

0 Kudos
LucD
Leadership
Leadership

Yes, but what timestamp does that data have ?

Remember due to aggregation there will be less observations per day.

I suspect the timestamp of the aggregated counter falls outside the timerange specified in the script.


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

0 Kudos
esxi1979
Expert
Expert

How to get host name & cluster in this  ?

in :-

..

$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

    }

}

.....

I tried both below no luck ..

#Host = $_.VMHost
#Host = $_.Name.VMHost
0 Kudos
LucD
Leadership
Leadership

Try like this

$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

        ESX = $_.Group[0].Entity.Name

        Cluster = Get-Cluster -VM $_.Group[0].Entity | Select -ExpandProperty 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

    }

}


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

0 Kudos
benso37
Contributor
Contributor

If that's the case, I should be able to do the following to get some report generated, correct?

$dayStart = Get-Date -Day 1 -Month 12 -Year 2014 -Hour 00 -Minute 0 -Second 0

$dayEnd = Get-Date -Day 1 -Month 12 -Year 2014 -Hour 23 -Minute 59 -Second 0

Another odd thing I just realized is that the number of VM's found don't match the number of VM's that are actually on the Host. The debug feature you enabled to show the VM's found, date/time (from and to) shows that I have 15 VM's on this last Host I just run the script against but in reality, I only have 5 VM's on that Host. What could be causing that?

0 Kudos
LucD
Leadership
Leadership

Yes, that is correct.

I suspect you might have more than one connection open to the same vSphere server.

Display the content of

$global:defaultviservers


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

0 Kudos
benso37
Contributor
Contributor

That command returns four servers. Basically all 4 hosts I've tried to run the script against. How do I close all the connections and start over?

0 Kudos
LucD
Leadership
Leadership

Use the Disconnect-VIServer cmdlet.

Or close/open the PowerCLI prompt.


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

0 Kudos
benso37
Contributor
Contributor

I'm back at where I left of yesterday. I just can't get this thing to give me any data. I was hoping to get some data after changing the time range from 00:00 to 23:59 but that's not working either. I'm lost.

BTW. Thanks for all your help. I really appreciate it.

0 Kudos