Hi All
I would like to seek your help as I need a script to get all VM resources allocation and consumption and have a csv & htm report.
The objective is to get all VM inventory per Datacenter\Cluster\Department Folders\ and within this are No. of VMs + Allocated CPU + Allocated Memory + Provisioned Disk.
Here is the sample report >>>
Consumer | No. of Virtual Machines | Allocated CPU | Allocated Memory | Disk Provisioned |
---|---|---|---|---|
Datacenter A\ Cluster 1 \ Corp | 10 | x | y | z |
Datacenter A\ Cluster 1 \ Finance | 6 | x | y | z |
Datacenter B\ Cluster 1 \ Corp | 15 | x | y | z |
Datacenter B\ Cluster 2 \ Production | 25 | x | y | z |
By this way I can see the consumption of all departments in our infrastructure.
I am not sure how you use the folders per cluster. Afaik you can't create a folder in the 'Hosts and Clusters' view below a cluster. The following script will give you the requested information without the folders. If you elaborate on the folders maybe I can add them later.
Get-Datacenter |
Sort-Object -Property Name |
ForEach-Object {
$Datacenter = $_
foreach ($Cluster in (Get-Cluster -Location $Datacenter | Sort-Object -Property Name))
{
$VMs = Get-VM -Location $Cluster
New-Object -TypeName PSObject -Property @{
Datacenter = $Datacenter.Name
Cluster = $Cluster.Name
'No. of Virtual Machines' = $VMs | Measure-Object | Select-Object -ExpandProperty Count
'Allocated CPU' = $VMs | Measure-Object -Property NumCpu -Sum | Select-Object -ExpandProperty Sum
'Allocated memory (GB)' = $VMs | Measure-Object -Property MemoryGB -Sum | Select-Object -ExpandProperty Sum
'Disk Provisoned (GB)' = $VMs | Measure-Object -Property ProvisionedSpaceGB -Sum | Select-Object -ExpandProperty Sum
} | Select-Object -Property Datacenter,Cluster,'No. of Virtual Machines',
'Allocated CPU','Allocated memory (GB)','Disk Provisoned (GB)'
}
}
The Folders I'm talking into are the ones created in "VMs and Templates" inventory view or Folder objects.
We have arranged our folders per department.
So, basically you get the Datacenter | then Cluster | then Folders | then get the needed information.
Hope this helps.
Try it like this
New-VIProperty -Name 'FolderPath' -ObjectType 'VirtualMachine' -Value {
param($vm)
$path = @()
$excludeFolders = "Datacenters","vm"
$parent = $vm.ExtensionData
while($parent.Parent){
$parent = Get-View $parent.Parent
if($excludeFolders -notcontains $parent.Name){
$path += $parent.Name
}
}
[string]::Join('/',$path[($path.Count -1)..0])
} -Force -BasedOnExtensionProperty 'Parent' | Out-Null
Get-VM | Group-Object FolderPath | %{
New-Object PSObject -Property @{
Consumer = $_.Name
"No. of Virtual Machines" = $_.Group.Count
"Allocated CPU" = $_.Group | Measure-Object -Property NumCPU -Sum | Select -ExpandProperty Sum
"Allocated Memory" = $_.Group | Measure-Object -Property MemoryGB -Sum | Select -ExpandProperty Sum
"Disk Provisioned" = $_.Group | Measure-Object -Property ProvisionedSpaceGB -Sum | Select -ExpandProperty Sum
}
}
It uses a VIProperty to get the blue folderpath, and then groups the VM on this property.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference