VMware

Gather Performance Statistics on Guest VM’s based on required tags

I’ve been working on a script to help me gather memory, CPU, and Disk utilization for a set of VM’s that have a tag associated with them. I’d like the Script to show me not just the averages, but the averages at 5-minute intervals for 1 day.

This is what I’ve found online and played around with, but can’t seem to get the tags to be work.

My test lab has tag category of Lab-Systems. With Assigned Tag of Work-Systems, Test-Systems, or MGT.

$allvms = @()

$vms = Get-Vm

$start = (Get-Date).AddDays(-7)

$metrics = “cpu.usage.average”,”mem.usage.average”

$stats = Get-Stat -Entity $vms -Start $start -Stat $metrics

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

$vmstat = “” | Select VmName, Day, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin

$vmstat.VmName = $_.Values[1]

$[vmstat.Day](https://vmstat.Day) = $_.Group[0].Timestamp.Date

$cpu = $_.Group | where {$_.MetricId -eq “cpu.usage.average”} | Measure-Object -Property value -Average -Maximum -Minimum

$mem = $_.Group | where {$_.MetricId -eq “mem.usage.average”} | Measure-Object -Property value -Average -Maximum -Minimum

$vmstat.CPUMax = [int]$cpu.Maximum

$vmstat.CPUAvg = [int]$cpu.Average

$vmstat.CPUMin = [int]$cpu.Minimum

$vmstat.MemMax = [int]$mem.Maximum

$vmstat.MemAvg = [int]$mem.Average

$vmstat.MemMin = [int]$mem.Minimum

$allvms += $vmstat

}

$allvms |

Export-Csv “H:\VMs.csv” -noTypeInformation

​

​

I’m able to pull just the Tag Category with this command:

get-VM | where {$_.Powerstate -eq “poweredOn”} |Select Name,PowerState, NumCpu, MemoryMB, @{Name=”Tags”;Expression={(Get-TagAssignment -Category “Lab-Systems” $_).Tag.Name}}| Where {$_.Tags}

But can’t seem to get the command to pull the Assigned Tag. It gives me nothing when I change the command to this:

get-VM | where {$_.Powerstate -eq “poweredOn”} |Select Name,PowerState, NumCpu, MemoryMB, @{Name=”Tags”;Expression={(Get-TagAssignment -Name”Test-Systems” $_).Tag.Name}}| Where {$_.Tags}

​

I have verified with the get-tag output that the Name is set. Any help would be appreciated.

​

Thanks in advance!


View Reddit by Fingers624View Source

Related Articles

One Comment

  1. ok, I think I’ve figured it out using the following modifications. I added a tag variable and I can then update the tag name to match my required virtual machines. I then add the variable to the Get-VM. Resulting in the following:

    ​

    ​

    $allvms = @()

    $tag = get-tag -name MGT

    $vms = Get-Vm -tag $tag

    $start = (Get-Date).AddDays(-7)

    $metrics = “cpu.usage.average”,”mem.usage.average”

    $stats = Get-Stat -Entity $vms -Start $start -Stat $metrics

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

    $vmstat = “” | Select VmName, Day, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin

    $vmstat.VmName = $_.Values[1]

    $[vmstat.Day](https://vmstat.Day) = $_.Group[0].Timestamp.Date

    $cpu = $_.Group | where {$_.MetricId -eq “cpu.usage.average”} | Measure-Object -Property value -Average -Maximum -Minimum

    $mem = $_.Group | where {$_.MetricId -eq “mem.usage.average”} | Measure-Object -Property value -Average -Maximum -Minimum

    $vmstat.CPUMax = [int]$cpu.Maximum

    $vmstat.CPUAvg = [int]$cpu.Average

    $vmstat.CPUMin = [int]$cpu.Minimum

    $vmstat.MemMax = [int]$mem.Maximum

    $vmstat.MemAvg = [int]$mem.Average

    $vmstat.MemMin = [int]$mem.Minimum

    $allvms += $vmstat

    }

    $allvms |

    Export-Csv “H:\VMs.csv” -noTypeInformation

Leave a Reply

Your email address will not be published. Required fields are marked *

Close