VMware Cloud Community
rsantos1
Contributor
Contributor
Jump to solution

VMs by OS in datacenter

Hello folks,

I am trying to write a script that totalizes VMs in a datacenter by operating system. Here is what I have so far:

get-vm -location (get-datacenter -name "DC1") | %{

     $totals[$_.ExtensionData.Config.guestFullName]++

}

$totals

This works for datacenter DC1, but what should I do if I want to collect totals for all datacenters in my vCenter? I'd also like to export this data as html so that it can be read as a report in a browser.

Your help is greatly appreciated.

Thanks!

Rob

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could do something like this to have a report for each datacenter

foreach($dc in Get-Datacenter){
  Get-VM -Location $dc | 
  Group-Object -Property {$_.ExtensionData.Config.guestFullName} | %{
    Select -InputObject $_ -Property @{N="Datacenter";E={$dc.Name}},Name,@{N="Count";E={$_.Group | Measure-Object | Select -ExpandProperty Count}}
  }
}


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

View solution in original post

Reply
0 Kudos
16 Replies
LucD
Leadership
Leadership
Jump to solution

Let PowerShell do the grouping and counting for you.

Something like this

Get-VM -location (get-datacenter -name "DC1") | 
Group-Object -Property {$_.ExtensionData.Config.guestFullName} | %{
  Select -InputObject $_ -Property Name,@{N="Count";E={$_.Group | Measure-Object | Select -ExpandProperty Count}}
}


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

rsantos1
Contributor
Contributor
Jump to solution

This works but the code runs slower, I guess because of the measure-object?

What if I wanted to list totals per datacenter?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could do something like this to have a report for each datacenter

foreach($dc in Get-Datacenter){
  Get-VM -Location $dc | 
  Group-Object -Property {$_.ExtensionData.Config.guestFullName} | %{
    Select -InputObject $_ -Property @{N="Datacenter";E={$dc.Name}},Name,@{N="Count";E={$_.Group | Measure-Object | Select -ExpandProperty Count}}
  }
}


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

Reply
0 Kudos
rsantos1
Contributor
Contributor
Jump to solution

This works great. Thank you!

Reply
0 Kudos
edelldieguez
Contributor
Contributor
Jump to solution

Hi Guys,

i know this's an old threat but i'm interested in see how can i get a .csv output with this info.

Thanks in advance.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Since this uses a ForEach statement, you can use the trick with the code block to pipe the results to a CSV file.

&{foreach($dc in Get-Datacenter){
  Get-VM -Location $dc | 
  Group-Object -Property {$_.ExtensionData.Config.guestFullName} | %{
    Select -InputObject $_ -Property @{N="Datacenter";E={$dc.Name}},Name,@{N="Count";E={$_.Group | Measure-Object | Select -ExpandProperty Count}}
  }
}} | Export-Csv C:\report.csv -NoTypeInformation -UseCulture 


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

Reply
0 Kudos
edelldieguez
Contributor
Contributor
Jump to solution

Thank you LucD, it works fine. one more thing, if i want to add per clusters? like: datacenter - Cluster - VMs - OS., we have just one Datacenter with several Clusters.

Thanks in advance.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Just add another loop

&{foreach($dc in Get-Datacenter){
    foreach($cluster in Get-Cluster -Location $dc){
      Get-VM -Location $cluster | 
      Group-Object -Property {$_.ExtensionData.Config.guestFullName} | %{
        Select -InputObject $_ -Property @{N="Datacenter";E={$dc.Name}},Name,
        @{N="Cluster";E={$cluster.Name}},
        @{N="Count";E={$_.Group | Measure-Object | Select -ExpandProperty Count}}       }     }   }} | Export-Csv C:\report.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos
edelldieguez
Contributor
Contributor
Jump to solution

LucD, do you know why is giving me only one VM with 2003 OS in a cluster where i have 16 VMs all with 2008 R2 OS ??

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are the VMs you are not seeing powered on ?

And do they have the VMware Tools installed ?


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

Reply
0 Kudos
edelldieguez
Contributor
Contributor
Jump to solution

Yes LucD, all VMs are powered on with VMware tools installed and upgraded to the latest version on that cluster.

Thanks.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you see the correct OS for each VM when you do this ?

&{foreach($dc in Get-Datacenter){
    foreach($cluster in Get-Cluster -Location $dc){
      Get-VM -Location $cluster | 
      Select Name,@{N="DC";E={$dc.Name}},@{N="Cluster";E={$cluster.Name}},
        @{N="OS";E={$_.ExtensionData.Config.guestFullName}}     }   }} | Export-Csv C:\report.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos
edelldieguez
Contributor
Contributor
Jump to solution

Yes LucD, i can see all VMs OS on all  Clusters

Thanks.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then I'm stumped I'm afraid.

Just ran the script in my environment and it returns the correct numbers.


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

Reply
0 Kudos
edelldieguez
Contributor
Contributor
Jump to solution

&{foreach($dc in Get-Datacenter){
    foreach($cluster in Get-Cluster -Location $dc){
      Get-VM -Location $cluster |
      Group-Object -Property {$_.ExtensionData.Config.guestFullName} | %{
        Select -InputObject $_ -Property @{N="Datacenter";E={$dc.Name}},Name,
        @{N="Cluster";E={$cluster.Name}},
        @{N="Count";E={$_.Group | Measure-Object | Select -ExpandProperty Count}}
      }
    }
}} | Export-Csv C:\report.csv -NoTypeInformation -UseCulture

LucD, here the script, do you see something wrong?

Thanks.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Script seems to be identical to my version.

Just did a copy/paste and ran it, the same correct results.


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

Reply
0 Kudos