Automation

 View Only
Expand all | Collapse all

Reporting resource usage per VMfolder

  • 1.  Reporting resource usage per VMfolder

    Posted Jun 24, 2013 01:19 PM

    Hi guys,

    I need to get a report that will look into a vCenter's VM folders and will report back the total amount of RAM used by each folder (provisioned and active ideally), CPU used (MHz) and maybe disk space used

    We organize VMs by product/purpose, our idea is that by looking at their folders we are able to estimate total amount of resource usage per product. I'd like to be able to schedule this as a weekly task and get the result sent in HTML form via email.

    I've tried piecing together code snippets that I've found but none seem to do the trick.

    Any help would be greatly appreciated.



  • 2.  RE: Reporting resource usage per VMfolder

    Posted Jun 27, 2013 05:56 AM

    Where are you stuck?

    To get your report you could: Get-VM -Location FolderName | FT Name, *CPU*, MemoryGB, *space*



  • 3.  RE: Reporting resource usage per VMfolder

    Posted Jul 10, 2013 10:17 AM

    So I'm using this:

    Get-VM  -location "folder" | Select-Object Name, NumCPU, MemoryGB | Export-Csv "C:\CPU_Memory.csv" -NoTypeInformation

    How can I get the script to calculate the totals for the NumCpu and MemoryGB and add them to the csv ?

    Also, this works per folder, how can I modify this to work per vCenter. What I'd ideally end up with is a script that looks into each folder and reports on the total NumCPU and MemoryGB values.

    Thanks!



  • 4.  RE: Reporting resource usage per VMfolder

    Posted Jul 10, 2013 10:29 AM

    The following PowerCLI script will report the total number of virtual CPU's and MemoryGB per VM folder:

    Get-Folder -Type VM |

    ForEach-Object {

      $Folder = $_

      $VMs = $Folder | Get-VM

      $Folder |

      Select-Object -Property Name,

        @{N="TotalNumCpu";E={$VMs | Measure-Object -Property NumCpu -Sum | Select-Object -ExpandProperty Sum}},

        @{N="TotalMemoryGB";E={$VMs | Measure-Object -Property MemoryGB -Sum | Select-Object -ExpandProperty Sum}}

    }



  • 5.  RE: Reporting resource usage per VMfolder

    Posted Jul 10, 2013 11:14 AM

    This will give you the list per vCenter, provided you are not running in multiple mode, with the full folderpath (which can be handy in case of duplciate names).

    And it uses only 1 Get-VM call

    Get-VM | Group-Object -Property {$_.Folder.Id} |
    Select @{N="Folder";E={
     
    $folder = $_.Group[0].Folder
     
    $path = $folder.Name
     
    while($folder.Parent){
       
    $folder = $folder.Parent
       
    $path = $folder.Name + "/" + $path
      }
     
    $path
      }}
    ,
     
    @{N="NumCPU";E={$_.Group | Measure-Object -Property NumCpu -Sum | Select -ExpandProperty Sum}},
     
    @{N="MemoryGB";E={$_.Group | Measure-Object -Property MemoryGB -Sum | Select -ExpandProperty Sum}}


  • 6.  RE: Reporting resource usage per VMfolder

    Posted Jul 10, 2013 12:40 PM

    Thank you both, this is exactly what I was looking for!

    Last question - how can I format the output into an html file or csv that I can mail after the script runs?



  • 7.  RE: Reporting resource usage per VMfolder

    Posted Jul 10, 2013 12:44 PM

    You can pipe the output to the Export-CSV cmdlet to generate a .csv file or to the ConvertTo-Html cmdlet to generate a html file.



  • 8.  RE: Reporting resource usage per VMfolder

    Posted Jul 10, 2013 01:00 PM

    So I've added this to the end of the script:

    ConvertTo-HTML | Out-File "C:\Users\test.html"

    I see the right output on the console, but the html file is empty:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <title>HTML TABLE</title>

    </head><body>

    <table>

    </table>

    </body></html>

    I'm sure I'm missing something



  • 9.  RE: Reporting resource usage per VMfolder

    Posted Jul 10, 2013 01:02 PM

    That will not work with a Foreach loop like that.



  • 10.  RE: Reporting resource usage per VMfolder
    Best Answer

    Posted Jul 10, 2013 01:00 PM

    Like this

    Get-VM | Group-Object -Property {$_.Folder.Id} |
    Select @{N="Folder";E={
     
    $folder = $_.Group[0].Folder
     
    $path = $folder.Name
     
    while($folder.Parent){
       
    $folder = $folder.Parent
       
    $path = $folder.Name + "/" + $path
      }
     
    $path
      }}
    ,
     
    @{N="NumCPU";E={$_.Group | Measure-Object -Property NumCpu -Sum | Select -ExpandProperty Sum}},
     
    @{N="MemoryGB";E={$_.Group | Measure-Object -Property MemoryGB -Sum | Select -ExpandProperty Sum}} |
    Export-Csv C:\report.csv -NoTypeInformation -use


  • 11.  RE: Reporting resource usage per VMfolder

    Posted Jul 10, 2013 02:00 PM

    Right on the money!! Thank you very much.



  • 12.  RE: Reporting resource usage per VMfolder

    Posted Jul 12, 2013 08:46 AM

    Hi,

    I want to add a column for the total number of VMs per folder, I've added this:

    @{N="Folder";E={$_.Name}},@{N="Number of VM's";E={($_|Get-VM|Measure-Object).Count}},

    --------------

    Get-VM | Group-Object -Property {$_.Folder.Id} |

    Select @{N="Folder";E={

      $folder = $_.Group[0].Folder

      $path = $folder.Name

      while($folder.Parent){

        $folder = $folder.Parent

        $path = $folder.Name + "/" + $path

      }

      $path

      }},

      @{N="Folder";E={$_.Name}},@{N="Number of VM's";E={($_|Get-VM|Measure-Object).Count}},

      @{N="NumCPU";E={$_.Group | Measure-Object -Property NumCpu -Sum | Select -ExpandProperty Sum}},

      @{N="MemoryGB";E={$_.Group | Measure-Object -Property MemoryGB -Sum | Select -ExpandProperty Sum}} |

    Export-Csv C:\report.csv -NoTypeInformation -use

    It fails to run, what am I missing ?



  • 13.  RE: Reporting resource usage per VMfolder

    Posted Jul 12, 2013 08:52 AM

    Try like this

    Get-VM | Group-Object -Property {$_.Folder.Id} |
    Select @{N="Folder";E={
     
    $folder = $_.Group[0].Folder
     
    $path = $folder.Name
     
    while($folder.Parent){
       
    $folder = $folder.Parent
       
    $path = $folder.Name + "/" + $path
      }
     
    $path
      }}
    ,
     
    @{N="NumCPU";E={$_.Group | Measure-Object -Property NumCpu -Sum | Select -ExpandProperty Sum}},
     
    @{N="MemoryGB";E={$_.Group | Measure-Object -Property MemoryGB -Sum | Select -ExpandProperty Sum}},
     
    @{N="Number of VM's";E={$_.Group.Count}} |
    Export-Csv C:\report.csv -NoTypeInformation -use

    The Group property in the objects returned by the Group-Object cmdlet, holds all the VMs in a specific folder.

    So we just need to count the number of entries.



  • 14.  RE: Reporting resource usage per VMfolder

    Posted Jul 12, 2013 09:59 AM

    Thanks again!

    I added a row to get ProvisionedSpaceGB, and it works good:

    @{N="ProvisionedSpaceGB";E={$_.Group | Measure-Object -Property ProvisionedSpaceGB -Sum | Select -ExpandProperty Sum}},

    But it comes out with a lot of decimals (6 I think).


    I was reading this PowerCLI 4.1 brings the New-VIProperty cmdlet | LucD notes

    I tried doing this:

    @{N="ProvisionedSpaceGB";E={"{0:f1}"{$_.Group | Measure-Object -Property ProvisionedSpaceGB -Sum | Select -ExpandProperty Sum}},



  • 15.  RE: Reporting resource usage per VMfolder

    Posted Jul 12, 2013 10:55 AM

    Try it like this

    @{N="ProvisionedSpaceGB";E={"{0:f1}" -f ($_.Group | Measure-Object -Property ProvisionedSpaceGB -Sum | Select -ExpandProperty Sum)}},

    You forgot to add the format operator.