VMware Cloud Community
jessem
Enthusiast
Enthusiast
Jump to solution

Script to collect certain stats about hosts and VMs

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 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
22 Replies
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

jessem
Enthusiast
Enthusiast
Jump to solution

great, should I use this to output it?

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
jessem
Enthusiast
Enthusiast
Jump to solution

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:

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Those last 2 lines should be

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


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

Reply
0 Kudos
jessem
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jessem
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos
jessem
Enthusiast
Enthusiast
Jump to solution

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,

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

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


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

Reply
0 Kudos
jessem
Enthusiast
Enthusiast
Jump to solution

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.

Reply
0 Kudos
jessem
Enthusiast
Enthusiast
Jump to solution

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?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jessem
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which PowerCLI version are you using ? Do a

Get-PowerCLIVersion


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

Reply
0 Kudos
jessem
Enthusiast
Enthusiast
Jump to solution

5.1 R2, Build 1012425

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That explains, the Get-Datacenter cmdlet with the Cluster parameter was introduced later.

Can you upgrade to PowerCLI 5.5 R2 ?


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

jessem
Enthusiast
Enthusiast
Jump to solution

Ok, so I upgraded and the client is now 5.5 R2, Patch 1.

I re-ran the script and the Datacenter column is still left blank.  I am attaching the exact script I am using.  Do you think I am still missing something?

Reply
0 Kudos
a_p_
Leadership
Leadership
Jump to solution

I could be wrong, but isn't there a minus sign missing in the command?

<<<  ... Get-Datacenter Cluster $_ ...

>>> ... Get-Datacenter -Cluster $_ ...

André