PowerCLI

 View Only
  • 1.  Get statistics by VM folder

    Posted Sep 18, 2020 07:53 PM

    Hi guys, I already took a look at the other discussions, but I haven't found exactly what I need.

    I saw some examples of LucD, but none that helped me. We want to generate a resource usage report for each VM folder.  In the graphical interface I have the information, as shown in the image below, but via PowerCLI I didn't find it.

    Thank you



  • 2.  RE: Get statistics by VM folder

    Posted Sep 18, 2020 08:15 PM

    You could do something like this

    $folderName = 'MyFolder'

    $cpu,$memory,$storage = Get-Folder -Name $folderName | Get-VM |

    select @{N='CPU';E={$_.ExtensionData.Summary.Quickstats.OverallCpuUsage}},

        @{N='Memory';E={$_.ExtensionData.Summary.Quickstats.GuestMemoryUsage}},

        UsedSpaceGB |

    Measure-Object -Property CPU,Memory,UsedSpaceGB -Sum |

    Select -ExpandProperty Sum


    '' | Select @{N='Folder';E={$folderName}},@{N='CPU';E={$cpu}},@{N='Memory';E={$memory}},@{N='Storage';E={[math]::Round($storage,2)}}



  • 3.  RE: Get statistics by VM folder

    Posted Sep 18, 2020 08:56 PM

    Thanks alot LucD, that was exactly what I needed. I put a foreach to get the information for each folder and now I would like to export it to a CSV. Can you help me with this?

    $folders = Get-Folder -Location VIRTUAL_MACHINES -Type VM -NoRecursion

                   

    foreach($folder in $folders){

        $cpu,$memory,$storage = Get-Folder -Name $folder | Get-VM |

        select @{N='CPU';E={$_.ExtensionData.Summary.Quickstats.OverallCpuUsage}},

            @{N='Memory';E={$_.ExtensionData.Summary.Quickstats.GuestMemoryUsage}},

            UsedSpaceGB |

        Measure-Object -Property CPU,Memory,UsedSpaceGB -Sum |

        Select -ExpandProperty Sum

        '' | Select @{N='Folder';E={$folder}},@{N='CPU';E={$cpu}},@{N='Memory';E={$memory}},@{N='Storage';E={[math]::Round($storage,2)}}

    }



  • 4.  RE: Get statistics by VM folder
    Best Answer

    Posted Sep 18, 2020 09:16 PM

    One way to do that could be

    $folders = Get-Folder -Location VIRTUAL_MACHINES -Type VM -NoRecursion

    $report = @()


    foreach($folder in $folders){

        $cpu,$memory,$storage = Get-Folder -Name $folder | Get-VM |

        select @{N='CPU';E={$_.ExtensionData.Summary.Quickstats.OverallCpuUsage}},

            @{N='Memory';E={$_.ExtensionData.Summary.Quickstats.GuestMemoryUsage}},

            UsedSpaceGB |

        Measure-Object -Property CPU,Memory,UsedSpaceGB -Sum |

        Select -ExpandProperty Sum

        $report += '' | Select @{N='Folder';E={$folder}},@{N='CPU';E={$cpu}},@{N='Memory';E={$memory}},@{N='Storage';E={[math]::Round($storage,2)}}

    }


    $report | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture



  • 5.  RE: Get statistics by VM folder

    Posted Mar 10, 2021 01:32 AM

    Would there be a way to do this but track the metrics over a period of time per folder?



  • 6.  RE: Get statistics by VM folder

    Posted Mar 10, 2021 06:46 AM

    That would require using the Get-Stat cmdlet.



  • 7.  RE: Get statistics by VM folder

    Posted Jun 04, 2021 10:51 AM

    Hello LucD,

    Would be possible to get it to work for top level Folders?



  • 8.  RE: Get statistics by VM folder

    Posted Jun 04, 2021 11:44 AM

    You can start with

     

    $root = Get-Folder -Name Datacenters | Get-Folder -Name vm
    $folders = Get-Folder -Location $root -NoRecursion -Type VM
    

     



  • 9.  RE: Get statistics by VM folder

    Posted Jun 04, 2021 01:09 PM

    Thank you for your fast answer, but unfortunately it returns nothing.

     

    PS C:\Users\xxxx-admin> $root

    Name Type
    ---- ----
    Datacenters Datacenter

     

    PS C:\Users\xxx-admin> Get-Folder -Location $root -Type VM -NoRecursion

    PS C:\Users\xxx-admin>



  • 10.  RE: Get statistics by VM folder

    Posted Jun 04, 2021 01:15 PM

    I forgot the hidden 'vm' folder.
    Code above has been updated



  • 11.  RE: Get statistics by VM folder

    Posted Jun 04, 2021 01:53 PM

    Thank you LucD, it is working now.