Pull VM Performance Metrics Using Tags With PowerCLI

From time to time I have application owners ask for performance metrics on VM’s. In environments that run vRops or other monitoring tools this is usually and easy task, but every so often these request are for test environments or legacy environments that we don’t have monitoring tools enabled or available. If its one server its usually a quick and easy task but when its many it becomes a little more time consuming and more of a hassle. I compiled a quick PowerCLI script to grab VM performance metrics using tags and dump them into a .CSV that I can provide to the customer. By using Tags it allows me to quickly lump VM’s together into a report that takes a few seconds to run and can be easily automated to dump into a network share or emailed. The script is pasted below, just edit the Tags and metrics to fit your needs.

#Captures current date
$date = get-date -Format MM"-"dd"-"yyyy
#Specifies metrics to gather
$counter = "cpu.usage.average","mem.usage.average","virtualdisk.read.average","virtualdisk.write.average","net.usage.average"
#sets collection date
$collect = (Get-Date).AddDays(-1)
#select specific tag
$tags = Get-Tag -Name TestTag
&{foreach($tag in $tags){
$vms = Get-VM -Tag $tag
if($vms){
$stat = Get-Stat -Realtime -Entity $vms -Stat $counter -Start $collect -ErrorAction SilentlyContinue
if($stat){
$stat | Group-Object -Property {$_.Entity.Name} | %{
New-Object PSObject -Property @{
Tag = $tag.Name
VM = $_.Values[0]
Cpu = ($_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Measure-Object -Property Value -Average).Average
Mem = ($_.Group | where {$_.MetricId -eq "mem.usage.average"} | Measure-Object -Property Value -Average).Average
DiskRead = ($_.Group | where {$_.MetricId -eq "virtualdisk.read.average"} | Measure-Object -Property Value -Average).Average
DiskWrite = ($_.Group | where {$_.MetricId -eq "virtualdisk.write.average"} | Measure-Object -Property Value -Average).Average
Net = ($_.Group | where {$_.MetricId -eq "net.usage.average"} | Measure-Object -Property Value -Average).Average}
}
}
}
}} | Export-Csv C:\output\Perf_Report_$date.csv -NoTypeInformation

 

About: Greg Russell

Greg Russell is a Principal Architect Working in Healthcare IT on the East Coast. His primary focus is vSAN, Replication and Disaster Recovery solutions.