Automation

 View Only
  • 1.  VM CPU and Memory average usage of the selected VM

    Posted Jul 19, 2022 06:46 AM

    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.



  • 2.  RE: VM CPU and Memory average usage of the selected VM
    Best Answer

    Posted Jul 19, 2022 07:47 AM

    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.



  • 3.  RE: VM CPU and Memory average usage of the selected VM

    Posted Jul 19, 2022 10:04 AM

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

    -Regards,