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
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
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
This works but the code runs slower, I guess because of the measure-object?
What if I wanted to list totals per datacenter?
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
This works great. Thank you!
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.
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
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.
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
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 ??
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
Yes LucD, all VMs are powered on with VMware tools installed and upgraded to the latest version on that cluster.
Thanks.
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
Yes LucD, i can see all VMs OS on all Clusters
Thanks.
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
&{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 -UseCultureLucD, here the script, do you see something wrong?
Thanks.
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