1 2 Previous Next 22 Replies Latest reply on Jul 13, 2014 9:43 AM by jessem

    Script to collect certain stats about hosts and VMs

    jessem Enthusiast

      I am trying to accomplish getting the following stats put in a script.

       

      When I run this script, it should be run per 'vcenter' and output the following.

       

      Name of cluster / Number of hosts/ Number of VMs and Templates / Assigned Total Memory of all VMs on that cluster (not of what's actually being in use)

       

      So for example, in cluster XYZ with 4 hosts, if I have 20 VMs that I have 4GB of RAM to each VM, the output should be 80GB

       

      XYZ     4     20     80GB

        • 1. Re: Script to collect certain stats about hosts and VMs
          LucD Guru
          Community WarriorsvExpertUser Moderators

          Try something like this

          Get-Cluster |
          Select Name,
           
          @{N="Hosts";E={$_.ExtensionData.Host.Count}},
           
          @{N="VM";E={
             
          Get-View -Id $_.ExtensionData.Host -Property VM |
             
          %{$_.Vm.Count} | Measure-Object -Sum | Select -ExpandProperty Sum}},
           
          @{N="Memory (MB)";E={
             
          Get-View -Id (Get-View -Id $_.ExtensionData.Host -Property VM | %{$_.Vm}) -Property "Config.Hardware.MemoryMB" |
             
          %{$_.Config.Hardware.MemoryMB} | Measure-Object -Sum |
             
          Select -ExpandProperty Sum
            }}
           
           
          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: Script to collect certain stats about hosts and VMs
            jessem Enthusiast

            great, should I use this to output it?

             

            Export-Csv c:\report.csv -NoTypeInformation -UseCulture

            • 3. Re: Script to collect certain stats about hosts and VMs
              LucD Guru
              User ModeratorsvExpertCommunity Warriors

              Yes, you can pipe the results to the Export-Csv cmdlet

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: Script to collect certain stats about hosts and VMs
                jessem Enthusiast

                Ok, I must be doing something wrong...here's the script...

                 

                Get-Cluster |

                Select Name,

                  @{N="Hosts";E={$_.ExtensionData.Host.Count}},

                  @{N="VM";E={

                    Get-View -Id $_.ExtensionData.Host -Property VM |

                    %{$_.Vm.Count} | Measure-Object -Sum | Select -ExpandProperty Sum}},

                  @{N="Memory (MB)";E={

                    Get-View -Id (Get-View -Id $_.ExtensionData.Host -Property VM | %{$_.Vm}) -Property "Config.Hardware.MemoryMB" |

                    %{$_.Config.Hardware.MemoryMB} | Measure-Object -Sum |

                    Select -ExpandProperty Sum

                  }}

                  Export-Csv c:\report.csv -NoTypeInformation -UseCulture

                 

                I am guessing I am piping this wrong???  When I run it I get: cmdlet Export-Csv at command pipeline position 1, Supply values for the following paramenters: InputObject:

                • 5. Re: Script to collect certain stats about hosts and VMs
                  LucD Guru
                  Community WarriorsUser ModeratorsvExpert

                  Those last 2 lines should be

                  }} | Export-Csv c:\report.csv -NoTypeInformation -UseCulture

                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                  • 6. Re: Script to collect certain stats about hosts and VMs
                    jessem Enthusiast

                    Perfect, I guess I was just inserting it in the wrong place.  Also, If I wanted to grab total CPU of each cluster, this doesn't work right...

                     

                    Get-Cluster |

                    Select Name,

                      @{N="Hosts";E={$_.ExtensionData.Host.Count}},

                      @{N="VM";E={

                        Get-View -Id $_.ExtensionData.Host -Property VM |

                        %{$_.Vm.Count} | Measure-Object -Sum | Select -ExpandProperty Sum}},

                      @{N="CPU";E={

                      Get-View -Id (Get-View -Id $_.ExtensionData.Host -Property VM | %{$_.Vm}) -Property "Config.Hardware.NumCPU" |

                      %{$_.Config.Hardware.NumCPU} | Measure-Object -Sum | 

                      @{N="Memory (MB)";E={

                        Get-View -Id (Get-View -Id $_.ExtensionData.Host -Property VM | %{$_.Vm}) -Property "Config.Hardware.MemoryMB" |

                        %{$_.Config.Hardware.MemoryMB} | Measure-Object -Sum |

                        Select -ExpandProperty Sum

                      }} | Export-Csv c:\report.csv -NoTypeInformation -UseCulture

                    • 7. Re: Script to collect certain stats about hosts and VMs
                      LucD Guru
                      Community WarriorsUser ModeratorsvExpert

                      I suspect you forgot the Select at the end to retrieve the sum.

                      @{N="CPU";E={

                        Get-View -Id (Get-View -Id $_.ExtensionData.Host -Property VM | %{$_.Vm}) -Property "Config.Hardware.NumCPU" |

                        %{$_.Config.Hardware.NumCPU} | Measure-Object -Sum |  Select -ExpandProperty Sum}},

                      Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                      • 8. Re: Script to collect certain stats about hosts and VMs
                        jessem Enthusiast

                        Once again you have saved me...worked as you laid it out!  Thanks!  Again

                        • 9. Re: Script to collect certain stats about hosts and VMs
                          jessem Enthusiast

                          Hello, I completely forgot to say I also needed the Datacenter Name as well. So would I add the following?

                           

                          Get-Cluster |

                          Select Name,

                           

                          Get-Datacenter |

                          Select Name,

                          • 10. Re: Script to collect certain stats about hosts and VMs
                            LucD Guru
                            Community WarriorsUser ModeratorsvExpert

                            That should work, just use the ExpandProperty parameter on the Select.

                            Get-Cluster -VMHost $_ | Select -ExpandProperty Name

                            Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                            • 11. Re: Script to collect certain stats about hosts and VMs
                              jessem Enthusiast

                              Once again, I must be doing something wrong.  I am running PowerCLI 5.1 R2 and it states a parameter cannot be found 'VMHost'.  Here's the script....

                               

                              Get-Datacenter |

                              Select Name,

                              Get-Cluster -VMHost $_ | Select -ExpandProperty Name

                              Select Name,

                                @{N="Hosts";E={$_.ExtensionData.Host.Count}},

                                @{N="VM";E={

                                  Get-View -Id $_.ExtensionData.Host -Property VM |

                                  %{$_.Vm.Count} | Measure-Object -Sum | Select -ExpandProperty Sum}},

                                @{N="Memory (MB)";E={

                                  Get-View -Id (Get-View -Id $_.ExtensionData.Host -Property VM | %{$_.Vm}) -Property "Config.Hardware.MemoryMB" |

                                  %{$_.Config.Hardware.MemoryMB} | Measure-Object -Sum |

                                  Select -ExpandProperty Sum

                                }} | Export-Csv c:\report2.csv -NoTypeInformation -UseCulture

                               

                               

                              I want the list to include the datacenter and the cluster.

                              • 12. Re: Script to collect certain stats about hosts and VMs
                                jessem Enthusiast

                                I have tried the select expand property numerous ways and I just cannot get it to work.  Still get the VMHost error.  What am I doing wrong?

                                • 13. Re: Script to collect certain stats about hosts and VMs
                                  LucD Guru
                                  Community WarriorsUser ModeratorsvExpert

                                  You need to do that in a calculated property.

                                  Something like this

                                   

                                  Get-Cluster |

                                  Select Name,

                                    @{N="Datacenter";E={Get-Datacenter -Cluster $_ | Select -ExpandProperty Name}},

                                    @{N="Hosts";E={$_.ExtensionData.Host.Count}},

                                    @{N="VM";E={

                                      Get-View -Id $_.ExtensionData.Host -Property VM |

                                      %{$_.Vm.Count} | Measure-Object -Sum | Select -ExpandProperty Sum}},

                                    @{N="Memory (MB)";E={

                                      Get-View -Id (Get-View -Id $_.ExtensionData.Host -Property VM | %{$_.Vm}) -Property "Config.Hardware.MemoryMB" |

                                      %{$_.Config.Hardware.MemoryMB} | Measure-Object -Sum |

                                      Select -ExpandProperty Sum

                                    }} | Export-Csv c:\report2.csv -NoTypeInformation -UseCulture

                                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                  • 14. Re: Script to collect certain stats about hosts and VMs
                                    jessem Enthusiast

                                    I tried that and no luck.  The output shown below doesn't give me any Datacenter label.  I have attached an image to show.

                                     

                                    Would it be easier if we had Datacenter in the first column, and then cluster in the second column?

                                     

                                    screenshot.GIF

                                    1 2 Previous Next