VMware Cloud Community
yashatrevm
Enthusiast
Enthusiast
Jump to solution

VM CPU and Memory average usage of the selected VM

Hi,
I am working my ways through powercli and now stuck at this issue. I have written 2 functions, the first gives out the details of average usage of ALL the VMs in the infrastructure, and the second one tries to take input from user asking him which VM he wants to get the stats of. However, I am unable to get desired results with this second function, as is it returning only 1 VM even when there are multiple VMs with specific names.

=================

function getAvgForAllVMs{
$numOfDays = Read-Host "Enter number of days you want to go back to get the stats [Enter a value between 1 to 30]:"

try {
Write-Host "Fetching the details and calculating the average for ALL VMs in the infrastructure, this might take a while..."

Get-VM | Select Name, VMHost, NumCpu, MemoryMB, `
@{N="Cpu.UsageMhz.Average";E={[Math]::Round((($_ |Get-Stat -Stat cpu.usagemhz.average `
-Start (Get-Date).AddDays(-$numOfDays)-IntervalMins 5 -MaxSamples (30) |Measure-Object Value -Average).Average),2)}}, `
@{N="Mem.Usage.Average";E={[Math]::Round((($_ |Get-Stat -Stat mem.usage.average `
-Start (Get-Date).AddDays(-$numOfDays)-IntervalMins 5 -MaxSamples (30) |Measure-Object Value -Average).Average),2)}} | `
Export-Csv c:\Temp\stats.csv

}

catch {
Write-Host -foregroundColor Red -backgroundColor Black "`nCould not find the data, please verify vCenter Server Invetory Objects"
exit
}
}

=================

function getAvgForSelectedVMs{
$vmName = Read-Host "Enter the VM Name or Regex value to be searched : "
$numOfDays = Read-Host "Enter number of days you want to go back to get the stats [Enter a value between 1 to 30]:"

try {
$vmList = Get-VM $vmName
forEach ($vm in $vmList) {
Get-VM $vm | Select Name, VMHost, NumCpu, MemoryMB, `
@{N="Cpu.UsageMhz.Average";E={[Math]::Round((($_ |Get-Stat -Stat cpu.usagemhz.average `
-Start (Get-Date).AddDays(-$numOfDays)-IntervalMins 5 -MaxSamples (30) |Measure-Object Value -Average).Average),2)}}, `
@{N="Mem.Usage.Average";E={[Math]::Round((($_ |Get-Stat -Stat mem.usage.average `
-Start (Get-Date).AddDays(-$numOfDays)-IntervalMins 5 -MaxSamples (30) |Measure-Object Value -Average).Average),2)}} | `
Export-Csv c:\Temp\stats.csv
}
}

catch {
Write-Host -foregroundColor Red -backgroundColor Black "`nCould not find the data, please verify vCenter Server Invetory Objects"
exit
}
}

=============

With the second function, if I give the input for vmName as 'TestVM-*' where ther are multiple VMs as 'TestVM-001', 'TestVM-002', etc.

The resultant is a file showing only 1 entry with details even when there are multiple VMs it should work through.


I am quite new to this and might me completely wrong with my approach, I kindly request you to help me with this one.

Thank you.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You are doing the Export-Csv inside the getAvgForSelectedVMs function and inside the foreach loop, without an Append switch, hence overwriting that CSV each time you loop through the foreach.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

You are doing the Export-Csv inside the getAvgForSelectedVMs function and inside the foreach loop, without an Append switch, hence overwriting that CSV each time you loop through the foreach.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

atreyash
Contributor
Contributor
Jump to solution

Thanks a lot for that help. I added the append switch and it started adding results to the file.

-Regards,

0 Kudos