I'm a newbie with powercli and my boss is asking me to generate an hourly report on cpu average, memory average, disk i/o, and network i/o. Just picked up a book VMWare vSphere PowerCLI Reference but I'm overwhelmed with all the information. Wondering if there is a similar script from anyone out there that I can use or tailor according to my need.
Thanks
Steve
You can pipe the result to the Export-Csv cmdlet, like this.
The output will be one VM per row, and the values in different columns
$vms = Get-VM
$start = (Get-Date).AddHours(-1)
Get-Stat -Entity $vms -Start $start -ErrorAction SilentlyContinue |
Group-Object -Property {$_.Entity.Name} |
Select @{N='VM';E={$_.Name}},
@{N='CPU(%)';E={"{0:N1}" -f ($_.Group | where{$_.MetricId -eq 'cpu.usage.average'} | Measure-Object -Property Value -Average | select -ExpandProperty Average)}},
@{N='Memory(%)';E={"{0:N1}" -f ($_.Group | where{$_.MetricId -eq 'mem.usage.average'} | Measure-Object -Property Value -Average | select -ExpandProperty Average)}},
@{N='Net(KBps)';E={"{0:N2}" -f ($_.Group | where{$_.MetricId -eq 'net.usage.average'} | Measure-Object -Property Value -Average | select -ExpandProperty Average)}},
@{N='Disk(KBps';E={"{0:N2}" -f ($_.Group | where{$_.MetricId -eq 'disk.usage.average'} | Measure-Object -Property Value -Average | select -ExpandProperty Average)}} |
Export-Csv .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Are these statistics to be on VMs or ESXi nodes?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Of all the vms on my vCenter.
Thanks
Steve
Ok, let's start with a simple one.
This will get the values for the past hour for all VMs, and then calculate the average over that hour for all 4 categories.
Note that when you use the Get-Stat cmdlet without specifying any metrics on the Stat parameter, the cmdlet returns a number of metrics by default.
We use the ErrorAction on the Get-Stat to avoid error messages for when there are not statistics available for that VM at the time speficied on the Start parameter.
We call Get-Stat for all VMs in one call, then we use the Group-Object cmdlet to split up the results per VM.
In the object returned by the Group-Object cmdlet we have the key (Name) on which we grouped, and all the values under a property named Group.
On the objects present in Group, we now pull out the objects for specific metrics. Those we average.
We do that in a calculated property on the Select (short for Select-Object) cmdlet.
$vms = Get-VM
$start = (Get-Date).AddHours(-1)
Get-Stat -Entity $vms -Start $start -ErrorAction SilentlyContinue |
Group-Object -Property {$_.Entity.Name} |
Select @{N='VM';E={$_.Name}},
@{N='CPU(%)';E={"{0:N1}" -f ($_.Group | where{$_.MetricId -eq 'cpu.usage.average'} | Measure-Object -Property Value -Average | select -ExpandProperty Average)}},
@{N='Memory(%)';E={"{0:N1}" -f ($_.Group | where{$_.MetricId -eq 'mem.usage.average'} | Measure-Object -Property Value -Average | select -ExpandProperty Average)}},
@{N='Net(KBps)';E={"{0:N2}" -f ($_.Group | where{$_.MetricId -eq 'net.usage.average'} | Measure-Object -Property Value -Average | select -ExpandProperty Average)}},
@{N='Disk(KBps';E={"{0:N2}" -f ($_.Group | where{$_.MetricId -eq 'disk.usage.average'} | Measure-Object -Property Value -Average | select -ExpandProperty Average)}}
The PowerCLI Reference is probably not the best book if you are just starting, it assumes a certain knowledge of PowerShell and a bit of PowerCLI.
There are some quite good introductory books on PowerShell and PowerCLI.
Have a look at these posts for pointers: PowerShell study guide – core concepts and the PowerCLI study guide – core concepts
If you want learn a bit more on handling vSphere statistical data with PowerCLI, you could browse through the series of posts I did on PowerCLI and Statistics.
It explains some of the core concepts, and then builds on that.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank You Luc. I ran the script and it came out something like below. I'm just copying a few instead of all the VMs from the output.
VM | : V0AA0391_Linux |
CPU(%) | : 0.3 |
Memory(%) : 0.9
Net(KBps) : 0.00
Disk(KBps : 0.21
VM | : V0AA0311_W2k8 |
CPU(%) | : 4.1 |
Memory(%) : 14.9
Net(KBps) : 23.90
Disk(KBps : 36.90
VM | : V0AA0255_Linux |
CPU(%) | : 2.8 |
Memory(%) : 0.0
Net(KBps) : 0.00
Disk(KBps : 0.75
VM | : V0AA0290_Linux |
CPU(%) | : 1.0 |
Memory(%) : 0.9
Net(KBps) : 1.00
Disk(KBps : 14.83
I'm now trying to export it to a file share. Also, is there a way to have the output in a horizontal format? It's probably easier to read.
Export-Csv '\\h0ac0001-file\IT\SYSTEMS GROUP\VMsPerf.csv' -NoTypeInformation -UseCulture
Thanks
Steve
You can pipe the result to the Export-Csv cmdlet, like this.
The output will be one VM per row, and the values in different columns
$vms = Get-VM
$start = (Get-Date).AddHours(-1)
Get-Stat -Entity $vms -Start $start -ErrorAction SilentlyContinue |
Group-Object -Property {$_.Entity.Name} |
Select @{N='VM';E={$_.Name}},
@{N='CPU(%)';E={"{0:N1}" -f ($_.Group | where{$_.MetricId -eq 'cpu.usage.average'} | Measure-Object -Property Value -Average | select -ExpandProperty Average)}},
@{N='Memory(%)';E={"{0:N1}" -f ($_.Group | where{$_.MetricId -eq 'mem.usage.average'} | Measure-Object -Property Value -Average | select -ExpandProperty Average)}},
@{N='Net(KBps)';E={"{0:N2}" -f ($_.Group | where{$_.MetricId -eq 'net.usage.average'} | Measure-Object -Property Value -Average | select -ExpandProperty Average)}},
@{N='Disk(KBps';E={"{0:N2}" -f ($_.Group | where{$_.MetricId -eq 'disk.usage.average'} | Measure-Object -Property Value -Average | select -ExpandProperty Average)}} |
Export-Csv .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you Luc. The script ran like a charm and everything came out exactly how I wanted. I will read up on your recommendations and hopefully, i can do more with it as far as adding more criteria and exporting it to html or graphical format for upper management.
Thanks again.
Steve
Can you open a new thread for your request?
That makes it easier for other users to find the thread.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Can you run simple PowerCLI cmdlets?
Something like
Get-PowerCLIConfiguration
Connect-VIServer -Server <your-vCenter> -User <user> -Password <pswd>
Get-VMHost
Get-VM
Disconnect-VIServer -Confirm:$false
Store those line in a file with the filetype .ps1
Then from the PowerShell prompt, enter the path to the .ps1 file (for example C:\Scripts\test.ps1)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Thanks for your response. I was able to connect to VC and run the script given above to get performance report, but for VC it taking lot of time to response or I can say not getting output. Same script for VM performance report i have tried on Host where its working fine and giving proper output.
Is the script only designed for host or it is taking more time for VC as there are many VM's on that VC.
Thanks
The script will work with a connection to any vSphere server, beit a vCenter or an ESXi node.
The longer runtime with a vCenter connection is most probably due to the higher number of VMs.
But it could also be due to the amount of data you pull from the vSphere server.
On an ESXi node, you will find only 1 hour of statistical data, on a vCenter, the data can go back to up to 1 year.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD,
Is there any other way to pull performance report or VM metrics of all the VM's under a VC.
Depends what you want to see in that report.
If you are only interested in the current performance of the VM, you can fetch the values from the VirtualMachineQuickStats object.
That's a (very) small subset of the metrics available with
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
I'm very new to powercli, how to get Host performance graphical report... is it possible ?
What kind of graphical report do you have in mind?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for response, I'm looking for host and individual current CPU,datastore &memory consumption report...
That's not what I mean.
Are for example graphs that can be produced in Excel an option?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry bro...7 days old details