i am new to powercli and am not a good scriptor yet however, i need a script that will capture the max, min, avg cpu & mem usage stats per esxhost & vm for the past month and import into excel so i can create a pivot chart. key is i need to see the cpu/mem usage data per vm per host. can anyone help me with this. i have spent the last two weeks trying to work with powercli to do this and i am unable produce the results i want.
You can use the Start and Finish parameters on the Get-Stat cmdlet for that.
In the example above the Start parameter says to go back for 30 days.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks a lot LucD, this script is really great and easy to use.
I just have a little question.
When I do a manual get-stat I have some missing value:
Get-Stat -Entity "Cluster" -start (get-date).AddDays(-30) -Finish (Get-Date) -MaxSamples 10000 -stat mem.consumed.average
mem.consumed.average 14/03/2013 16:00:00 60007803 KB
mem.consumed.average 14/03/2013 14:00:00 60187842 KB
mem.consumed.average 14/03/2013 12:00:00 61190654 KB
mem.consumed.average 14/03/2013 10:00:00 62690097 KB
mem.consumed.average 12/03/2013 15:00:00 1 KB
mem.consumed.average 12/03/2013 13:00:00 60583747 KB
mem.consumed.average 12/03/2013 11:00:00 60868430 KB
mem.consumed.average 12/03/2013 09:00:00 62747355 KB
mem.consumed.average 12/03/2013 07:00:00 32911339 KBScript result :
ClustName : Cluster
MemMax : 34650514
MemAvg : 31216920
MemMin : 1
CPUMax : 19358
CPUAvg : 8761
CPUMin : 1
How can we exclude these missing values ?
Thanks a lot !
Edit :
In fact it was easy... by adding to the get-stat :
| where { $_.value -ne "1"}
Yes, when you have gaps in the data, the returned metrics seem to have a value of 1 or -1.
Best to ignore those.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello LucD !
I have tried to merge your different scripts into one complete script but no luck.
Currently I'm using 3 different scripts to get monthly reports about vm: cpu, uptime, memory total, mem (avg usage), hdd disks on VM's, disk sizes and their datastores.
Im not good at all in scripting, but it seems that script is more flexible than using vfoglight. If you could help, I really appreciate it.
K.
Can you give some more details on what you want ?
Do you want 1 values for each of these metrics for the complete month ?
Or for each interval during the month ?
Perhaps a sample skeleton layout of the envisaged report would be handy.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you for quick reply !
I need monthly total for each VM. It looks like this:
VM-name | CPU | Uptime | Memory total | Memory avg | HDD size | HDD Datastore
a1 2 156h 12GB 9.7GB Disk1 60GB a1_demo/dd.vmdk
Disk1 80GB a2_demo/dd.vmdk
a2 ...
My current script (faulty) looks like this:
$VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {
ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {
ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name )) {
$SnapSize=Get-Snapshot -VM $VM | Measure-Object -Property SizeGB -Sum
"" | Select-Object -Property @{N="VM";E={$VM.Name}},
@{N="Datacenter";E={$Datacenter.name}},
@{N="Folder";E={$VM.Folder}},
@{N="Host";E={$VM.VMHost}},
@{N="Cluster";E={$Cluster.Name}},
@{N="# CPU";E={$VM.NumCpu}},
@{N="Uptime";E={(new-timespan $_.ExtensionData.Summary.Runtime.BootTime $end).days}},
@{N="MemoryUsageGB"; E={[Math]::Round($_.MemoryUsageGB, 3)}}, @{N="MemoryTotalGB"; E={[Math]::Round($_.MemoryTotalGB, 3)}},
@{N="MemoryUsage%"; E={"{0:P0}" -f [Math]::Round($_.MemoryUsageGB/$_.MemoryTotalGB, 3)}},
@{N="RAM";E={$VM.MemoryGB}},
@{N="IP";E={$VM.Guest.IPAddress}},
@{N="Hard Disk";E={$HardDisk.Name}},
@{N="HD Size";E={$HardDisk.CapacityGB}},
@{N="HD Type";E={$HardDisk.StorageFormat}},
@{N="Snap Size";E={$SnapSize.Sum}},
@{N="VMDK Size";E={($vm.extensiondata.layoutex.file|?{$_.name -contains $harddisk.filename.replace(".","-flat.")}).size/1GB}},
@{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},
@{N="VMDKpath";E={$HardDisk.FileName.Split("]")[1]}}
}
}
}
}$VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "C:\file"
Try something like this
$VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {
$vms = Get-Cluster -Name $Cluster | Get-VM
foreach($vmStat in (Get-Stat -Entity $vms -Start (Get-Date).AddMonths(-1) -Stat "mem.usage.average" |
Group-Object -Property {$_.Entity.Name})){
$memAvgGB = $vmStat.Group[0].Entity.MemoryGB * ($vmStat.Group | Measure-Object -Property Value -Average | Select -ExpandProperty Average) / 100
ForEach ($HardDisk in (Get-HardDisk -VM $vmStat.Values[0] | Sort-Object -Property Name)) {
"" | Select-Object -Property @{N="VM-name";E={$vmStat.Values[0]}},
@{N="CPU";E={$vmStat.Group[0].Entity.NumCpu}},
@{N="Uptime (Days)";E={(New-TimeSpan $vmStat.Group[0].Entity.ExtensionData.Summary.Runtime.BootTime (Get-Date)).days}},
@{N="Memory Total (GB)";E={$vmStat.Group[0].Entity.MemoryGB}},
@{N="Memory Avg (GB)";E={[Math]::Round($memAvgGB, 3)}},
@{N="HDD";E={$HardDisk.Name}},
@{N="HDD Capacity (GB)";E={$HardDisk.CapacityGB}},
@{N="HDD Datastore";E={$HardDisk.FileName.Split("]")[1].Trim(' ')}}
}
}
}
}
$VmInfo
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Thanks for your posts.
I am looking for a powercli script to get the performance reports (CPU/Memory usage) for selected VM's. Duration is between 9AM-5PM for 10/20 business days. CSV output is better to represent the values in chart.
Environment is ESXi 4.1 / VC 4.1.
Appreciate your help.
I did a post in PowerCLI & vSphere statistics – Part 2 – Come together on selecting business hours for a statistics report.
Have a look, and let me know if that helps.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank LucD, I am beginner in powercli, hoping my status will change in weeks as I am going through your articles.
I checked the script , looks like its meant for ESXi host, but I am looking for performance reports of the Virtual Machines. If I can list more than one VM that would be great.
Thanks..
meanwhile, I tried to run the script for ESXi host CPU average and get the below error.
Export-Csv : A parameter cannot be found that matches parameter name 'UseCulture'.
At C:\pf.ps1:15 char:78
+ $report | Export-Csv "C:\BusinessHours-cpu.csv" -NoTypeInformation -UseCulture <<<<
It looks as if you are still running PowerShell v1.
The UseCulture switch was introduced in PowerShell v2.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD, I will try with PowerShell 2, Can you check on pcli script for performance reports of the Virtual Machines?. If I can list more than one VM that would be great.
Duration is between 9AM-5PM for 10/20 business days. CSV output is better to represent the values in chart.
for each Virtual Machine, I need the performance report during business hours excluding weekends. It should include hourly metrics/values for selected period (ex:- 15 days), CPU & Memory utilization for trend analysis with a chart. The main motive of this report is to understand the current utilization. Attached sample report. The output should like this with values in an excel /CSV file.
Hi to All!
I have trouble with getting stat when running from Win2008
Win7 works perfect.
I was trying to use:
Connect-VIServer <server> -User <user> -Password <password>
$allvms = @()
$allhosts = @()
$hosts = Get-VMHost
$vms = Get-Vm
foreach($vmHost in $hosts){
$hoststat = "" | Select HostName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin
$hoststat.HostName = $vmHost.name
$statcpu = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-30) -Finish (Get-Date)-MaxSamples 10000 -stat cpu.usage.average
$statmem = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-30) -Finish (Get-Date)-MaxSamples 10000 -stat mem.usage.average
$cpu = $statcpu | Measure-Object -Property value -Average -Maximum -Minimum
$mem = $statmem | Measure-Object -Property value -Average -Maximum -Minimum
$hoststat.CPUMax = $cpu.Maximum
$hoststat.CPUAvg = $cpu.Average
$hoststat.CPUMin = $cpu.Minimum
$hoststat.MemMax = $mem.Maximum
$hoststat.MemAvg = $mem.Average
$hoststat.MemMin = $mem.Minimum
$allhosts += $hoststat
}
windows7 gives me output values
"HostName","MemMax","MemAvg","CPUMax","CPUAvg"
"autoesx02.esx","71","54","37","16"
"autoesx01.esx","56","48","44","24"
windows 2008r2
"HostName","MemMax","MemAvg","CPUMax","CPUAvg"
"autoesx02.esx","","","",""
"autoesx01.esx","","","",""
What it can be? Please help
Thanks in advance
Do both platforms have the same PowerShell version ? Do a $psversiontable
Are both platforms 32-bit ?
Any error messages ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for fast reply
Both platforms are 64-bit
I was trying to run script in 32-bit and 64-bit Vmware PowerCLI
win2008r2 vmware powercli ver
PowerCLI C:\temp> $psversiontable
Name Value
---- -----
CLRVersion 2.0.50727.5472
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
win7 vmware powercli ver
PowerCLI C:\temp\vmware_scripts> $psversiontable
Name Value
---- -----
CLRVersion 2.0.50727.5472
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
And also I found tha if run on win2008r2 next command it gives responce
PowerCLI C:\temp> Get-Stat -Entity "autoesx02.esx" -Stat cpu.usage.
average -RealTime -MaxSamples 10
cpu.usage.average 10/31/2013 6:46:00 AM 5.83 % 30
cpu.usage.average 10/31/2013 6:45:40 AM 5.7 % 30
cpu.usage.average 10/31/2013 6:45:20 AM 14.83 % 30
cpu.usage.average 10/31/2013 6:45:00 AM 6.25 % 30
cpu.usage.average 10/31/2013 6:44:40 AM 5.38 % 30
....
but if run command like
PowerCLI C:\temp> Get-VMHost -Name "autoesx02.esx" | Get-Stat
-Stat cpu.usage.average -Start (Get-Date).AddDays(-1) -Finish(Get-Date) -MaxSam
ples 10
it does not return anythong
just start new line, when dubug(set-psdebug -trace 2) - nothing special, no errors
Does
Get-VMHost -Name "autoesx02.esx"
return anything when run on it's won ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes that works
Name ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz
---- --------------- ---------- ------ ----------- -----------
autoesx02.esx... Connected PoweredOn 16 7789 38384
and no metter which get for Vm or VMHost
Also when use "-Realtime" that gives responce, when add "-start -finish" -does not
Are you running this against a vCenter or an ESXi server ?
To which did you the Connect-VIServer ?
That could also mean that your Statistic Level on the vCenter is not set up correctly.
Check what it says in <Administration><vCenter Server Settings><Statistics> in the vSphere client.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference