Hi,
I am using the below script to get the usage, this works fine, when I have VM with CPU, Memory and HDD used, but when there are no VMs, CPU, Memory or HDD used, I am unable to get the Available values correctly. Please help!!
$vmInfo = Foreach ($vc in $global:DefaultVIServers)
{
$report = @()
$folder = "Admin"
$allocated = "" | Select "Cluster","Folder Name","Resource_Info","CPU","Memory(GB)","Capacity(GB)","Total VM's","PoweredOn VM's","PoweredOff VM's"
$allocated.'Resource_Info' = 'Allocated'
$allocated.CPU = 20
$allocated.'Memory(GB)' = 40
$allocated.'Capacity(GB)' = 100
$report += $allocated
$report += Get-Folder $folder | Get-VM | Group-Object -Property {$_.Folder.Id} | Select @{N="Cluster";E={Get-Cluster -VM $_.Group}},
@{N="Folder Name";E={$folder = $_.Group[0].Folder; $path = $folder.Name; $path}},
@{N='Resource_Info';E={'Used'}},
@{N="CPU";E={$_.Group | Measure-Object -Property NumCpu -Sum | Select -ExpandProperty Sum}},
@{N="Memory(GB)";E={[math]::Round(($_.Group | Measure-Object -Property MemoryGB -Sum).Sum,0)}},
@{N="Capacity(GB)";E={[math]::Round(($_.Group | Get-HardDisk | Measure-Object -Property CapacityGB -Sum).Sum,0)}},
#@{N="Capacity(GB)";E={"{0:f1}" -f ($_.Group | Get-HardDisk | Measure-Object -Property CapacityGB -Sum | Select -ExpandProperty Sum)}},
@{N="Total VM's";E={$_.Group.Count}},
@{N="PoweredOn VM's";E={($_.Group | Where-Object {$_.PowerState -eq "PoweredOn"}).Count}},
@{N="PoweredOff VM's";E={($_.Group | Where-Object {$_.PowerState -eq "PoweredOff"}).Count}}
$available = "" | Select "Cluster","Folder Name","Resource_Info","CPU","Memory(GB)","Capacity(GB)","Total VM's","PoweredOn VM's","PoweredOff VM's"
$available.'Resource_Info' = 'Available'
$available.CPU = $report[0].CPU - ($report[1..($report.Count -1)].CPU | Measure-Object -Sum).Sum
$available.'Memory(GB)' = $report[0].'Memory(GB)' - ($report[1..($report.Count -1)].'Memory(GB)' | Measure-Object -Sum).Sum
$available.'Capacity(GB)' = $report[0].'Capacity(GB)' - ($report[1..($report.Count -1)].'Capacity(GB)' | Measure-Object -Sum).Sum
$report += $available
$report | Export-Excel -Path $reportlocation
}
$vmInfo
Currently I am getting as below when there are no resources used
But I need as below, when there are no resources used