PowerCLI

 View Only
Expand all | Collapse all

How to generate specific performance report with Powercli

benso37

benso37Dec 01, 2014 12:17 PM

  • 1.  How to generate specific performance report with Powercli

    Posted Nov 26, 2014 08:27 PM

    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.



  • 2.  RE: How to generate specific performance report with Powercli

    Posted Nov 26, 2014 10:02 PM

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



  • 3.  RE: How to generate specific performance report with Powercli

    Posted Nov 26, 2014 10:46 PM

    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.



  • 4.  RE: How to generate specific performance report with Powercli

    Posted Dec 01, 2014 12:17 PM

    Any other ideas?



  • 5.  RE: How to generate specific performance report with Powercli

    Posted Dec 01, 2014 12:54 PM

    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 ?



  • 6.  RE: How to generate specific performance report with Powercli

    Posted Dec 01, 2014 01:02 PM

    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.



  • 7.  RE: How to generate specific performance report with Powercli

    Posted Dec 01, 2014 03:00 PM

    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



  • 8.  RE: How to generate specific performance report with Powercli

    Posted Dec 01, 2014 04:13 PM

    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



  • 9.  RE: How to generate specific performance report with Powercli

    Posted Dec 01, 2014 04:17 PM

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



  • 10.  RE: How to generate specific performance report with Powercli

    Posted Dec 01, 2014 07:03 PM

    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



  • 11.  RE: How to generate specific performance report with Powercli

    Posted Dec 01, 2014 07:37 PM

    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 ?



  • 12.  RE: How to generate specific performance report with Powercli

    Posted Dec 01, 2014 08:00 PM

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



  • 13.  RE: How to generate specific performance report with Powercli

    Posted Dec 01, 2014 08:04 PM

    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.



  • 14.  RE: How to generate specific performance report with Powercli

    Posted Dec 02, 2014 01:14 PM

    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?



  • 15.  RE: How to generate specific performance report with Powercli

    Posted Dec 02, 2014 01:17 PM

    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



  • 16.  RE: How to generate specific performance report with Powercli

    Posted Dec 02, 2014 01:22 PM

    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?



  • 17.  RE: How to generate specific performance report with Powercli

    Posted Dec 02, 2014 01:24 PM

    Use the Disconnect-VIServer cmdlet.

    Or close/open the PowerCLI prompt.



  • 18.  RE: How to generate specific performance report with Powercli

    Posted Dec 02, 2014 02:00 PM

    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.



  • 19.  RE: How to generate specific performance report with Powercli

    Posted Dec 02, 2014 02:06 PM

    Perhaps try the following, define a longer period, let's say 7 days.

    And then check if any data is returned. Once you have the data, you should have a look at the Timestamp in the returned counters.

    $dayEnd = Get-Date

    $dayStart = $dayEnd.AddDays(-7)



  • 20.  RE: How to generate specific performance report with Powercli

    Posted Dec 02, 2014 02:22 PM

    I get the following error after adding your last mod.

    Get-Stat : Cannot validate argument on parameter 'Finish'. The argument is null

    or empty. Supply an argument that is not null or empty and then try the comman

    d again.

    At C:\Windows\System32\WindowsPowerShell\v1.0\VMwareReportscripts7.ps1:11 char:

    67

    + $stats = Get-Stat -Entity $vm -Stat $stat -Start $dayStart -Finish <<<<  $day

    End

        + CategoryInfo          : InvalidData: (:) [Get-Stat], ParameterBindingVal

       idationException

        + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom

       ation.ViCore.Cmdlets.Commands.GetViStats

    -----------------------------------------------

    This is what my current script looks like. First line deleted...

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

    Write-Output "VMs found $($vm.Count)"

    $dayStart = Get-Date

    $dayEnd = $dayEnd.AddDays(-7)

    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

    $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

        }

    }

    $report | Select VM,CPUavg,Memavg |

    Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture



  • 21.  RE: How to generate specific performance report with Powercli

    Posted Dec 02, 2014 02:27 PM

    You swapped the calculation of the values around, Start should be before Finish in time.



  • 22.  RE: How to generate specific performance report with Powercli

    Posted Dec 02, 2014 12:51 AM

    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


  • 23.  RE: How to generate specific performance report with Powercli

    Posted Dec 02, 2014 06:22 AM

    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

        }

    }



  • 24.  RE: How to generate specific performance report with Powercli

    Posted Dec 02, 2014 10:56 PM

    ------------------------ ****** ---------------------------------------- ****** -------------------------------

    wrt

    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

        }

    }

    ------------------------ ****** ---------------------------------------- ****** -------------------------------

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

    returned the vm name  itself

    ESX =  $_.Group[0].Entity.vmhost | Select -ExpandProperty Name


    will do ?



  • 25.  RE: How to generate specific performance report with Powercli

    Posted Dec 09, 2014 08:39 PM

    I had to step away from this for a couple of days to work on something else but I'm back to work on it again. I noticed that the initial request wasn't very clear. The current script gives me the average CPU and Memory for a set time frame. For example, 12PM to 3PM I get a table like the following.

                         

    VMCPUavgMemavg
    MyVM123.43534512.656534
    MyVM243.55433234.464545
    MyVM354.56455165.234232

    Is it possible to have the script do something like the following: If there's a way to generate against the VM Hos or cluster that will be great, If not, I don't mind generating on each VM.

    TimeMemAvgUsageCPUAvgUsage
    12/9/2014 11:05:0030.996.01
    12/9/2014 11:10:0029.995.8
    12/9/2014 11:15:00254.69
    12/9/2014 11:20:0025.994.99
    12/9/2014 11:25:0025.995.41
    12/9/2014 11:30:0027.995.38


  • 26.  RE: How to generate specific performance report with Powercli

    Posted Dec 10, 2014 07:55 AM

    You mean like this ?

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

    Write-Output "VMs found $($vm.Count)"

    $dayEnd = Get-Date

    $dayStart = $dayEnd.AddDays(-1)

    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

    $report = $stats | Group-Object -Property {$_.Entity.Name},Timestamp | %{

        New-Object PSObject -Property @{

            VM = $_.Name

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

            Cluster = Get-Cluster -VM $_.Group[0].Entity | Select -ExpandProperty Name

            Timestamp = $_.Group[0].Timestamp

            CPUavg = $_.Group | Where {$_.MetricID -eq 'cpu.usage.average'} | Select -ExpandProperty Value

            Memavg = $_.Group | Where {$_.MetricID -eq 'mem.usage.average'} | Select -ExpandProperty Value

        }

    }

    $report | Select VM,CPUavg,Memavg |

    Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture



  • 27.  RE: How to generate specific performance report with Powercli

    Posted Dec 10, 2014 03:42 PM

    Thank you LucD.

    That will work for me. I tried to add networking to the script but it returns system.object[] in the Netavg column. Do you know what I'm doing wrong?

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

    Write-Output "VMs found $($vm.Count)"

    $dayEnd = Get-Date

    $dayStart = $dayEnd.AddDays(-1)

    Write-Output "From $dayStart"

    Write-Output "To $dayEnd"

    $stat = 'cpu.usage.average','mem.usage.average','net.usage.average'

    $stats = Get-Stat -Entity $vm -Stat $stat -Start $dayStart -Finish $dayEnd

    $report = $stats | Group-Object -Property {$_.Entity.Name},Timestamp | %{

        New-Object PSObject -Property @{

            VM = $_.Name

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

            Cluster = Get-Cluster -VM $_.Group[0].Entity | Select -ExpandProperty Name

            Timestamp = $_.Group[0].Timestamp

            CPUavg = $_.Group | Where {$_.MetricID -eq 'cpu.usage.average'} | Select -ExpandProperty Value

            Memavg = $_.Group | Where {$_.MetricID -eq 'mem.usage.average'} | Select -ExpandProperty Value

            Netavg = $_.Group | Where {$_.MetricID -eq 'net.usage.average'} | Select -ExpandProperty Value

        }

    }

    $report

    $report | Select VM,CPUavg,Memavg,Netavg |

    Export-Csv "C:\VMreports.csv" -NoTypeInformation -UseCulture



  • 28.  RE: How to generate specific performance report with Powercli

    Posted Dec 10, 2014 05:46 PM

    For the network counter there are multiple instances, you can select the 'average' instance.

    Netavg = $_.Group | Where {$_.MetricID -eq 'net.usage.average' -and $_.Instance -eq ''} | Select -ExpandProperty Value



  • 29.  RE: How to generate specific performance report with Powercli

    Posted Dec 10, 2014 06:24 PM

    I modified the following line as suggested but the Netavg column doesn't generate any output. It comes back blank. I wonder if it's because I have multiple Network interfaces for each VM.



    Netavg = $_.Group | Where {$_.MetricID -eq 'net.usage.average' -and $_Instance -eq 'average'} | Select -ExpandProperty Value



  • 30.  RE: How to generate specific performance report with Powercli

    Posted Dec 10, 2014 07:00 PM

    The Instance value should not be 'average' but an empty string (as in my example).

    The average value has an Instance value of ''



  • 31.  RE: How to generate specific performance report with Powercli

    Posted Dec 10, 2014 07:20 PM

    That's how I did it the first time but that didn't work.



  • 32.  RE: How to generate specific performance report with Powercli

    Posted Dec 10, 2014 07:57 PM

    Can you try running with the following line, I would like to check which instances you get.

    Netavg = [string]::Join('|',($_.Group | Where {$_.MetricID -eq 'net.usage.average'} | Select -ExpandProperty Instance))



  • 33.  RE: How to generate specific performance report with Powercli

    Posted Dec 11, 2014 02:30 PM

    VM

    CPUavgMemavgNetavg
    MyVm001, 12/11/2014 13:23:2013.6914.994000|vmnic0|vmnic2|vmnic1|vmnic4|vmnic3|vmnic5|

    That's what the report looks like.



  • 34.  RE: How to generate specific performance report with Powercli

    Posted Dec 11, 2014 02:39 PM

    That last vertical bar indicates that there is an instance with a blank value.

    So the Where-clause I gave earlier should work.

    Perhaps you could attach the complete script you are using, there might be a typo perhaps ?



  • 35.  RE: How to generate specific performance report with Powercli

    Posted Dec 11, 2014 04:24 PM

    Here's my script.

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

    Write-Output "VMs found $($vm.Count)"

    $dayEnd = Get-Date

    $dayStart = $dayEnd.AddDays(-1)

    Write-Output "From $dayStart"

    Write-Output "To $dayEnd"

    $stat = 'cpu.usage.average','mem.usage.average','net.usage.average'

    $stats = Get-Stat -Entity $vm -Stat $stat -Start $dayStart -Finish $dayEnd

    $report = $stats | Group-Object -Property {$_.Entity.Name},Timestamp | %{

        New-Object PSObject -Property @{

            VM = $_.Name

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

            Cluster = Get-Cluster -VM $_.Group[0].Entity | Select -ExpandProperty Name

            Timestamp = $_.Group[0].Timestamp

            CPUavg = $_.Group | Where {$_.MetricID -eq 'cpu.usage.average'} | Select -ExpandProperty Value

            Memavg = $_.Group | Where {$_.MetricID -eq 'mem.usage.average'} | Select -ExpandProperty Value

            Netavg = [string]::join('|',($_.Group | Where {$_.MetricID -eq 'net.usage.average'} | Select -ExpandProperty Instance))

        }

    }

    $report | Select VM,CPUavg,Memavg,Netavg | Sort-Object VM | Export-Csv "C:\temp\VMreports.csv" -NoTypeInformation -UseCulture



  • 36.  RE: How to generate specific performance report with Powercli

    Posted Dec 11, 2014 05:54 PM

    Try like this

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

    Write-Output "VMs found $($vm.Count)"

    $dayEnd = Get-Date

    $dayStart = $dayEnd.AddDays(-1)

    Write-Output "From $dayStart"

    Write-Output "To $dayEnd"

    $stat = 'cpu.usage.average','mem.usage.average','net.usage.average'

    $stats = Get-Stat -Entity $vm -Stat $stat -Start $dayStart -Finish $dayEnd

    $report = $stats | Group-Object -Property {$_.Entity.Name},Timestamp | %{

        New-Object PSObject -Property @{

            VM = $_.Name

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

            Cluster = Get-Cluster -VM $_.Group[0].Entity | Select -ExpandProperty Name

            Timestamp = $_.Group[0].Timestamp

            CPUavg = $_.Group | Where {$_.MetricID -eq 'cpu.usage.average'} | Select -ExpandProperty Value

            Memavg = $_.Group | Where {$_.MetricID -eq 'mem.usage.average'} | Select -ExpandProperty Value

            Netavg = $_.Group | Where {$_.MetricID -eq 'net.usage.average' -and $_.Instance -eq ''} | Select -ExpandProperty Value

        }

    }

    $report | Select VM,CPUavg,Memavg,Netavg | Sort-Object VM |

    Export-Csv "C:\temp\VMreports.csv" -NoTypeInformation -UseCulture