I got a script that work but want to include a subtotal for each VM and not sure how to insert/add a subtotal line at end of each VM. The output report will be in CSV.
Any help will be greatly appreciated.
$FinalResult = @()
$ALLvms = Get-View -ViewType “VirtualMachine”
$allvms | foreach-object {
$vm = $_
$vmname = $vm.name
$Drives = $vm.Guest.Disk
$drives | foreach-object {
$driveC = $_
$object = New-Object -TypeName PSObject
$Capacity = “{0:N0}” -f [math]::Round($DriveC.Capacity / 1MB)
$Freespace = “{0:N0}” -f [math]::Round($DriveC.FreeSpace / 1MB)
$Percent = [math]::Round(($FreeSpace)/ ($Capacity) * 100)
$PercentFree = “{0:P0}” -f ($Percent/100)
$object | Add-Member -MemberType NoteProperty -Name “Server Name” -Value $vmname
$object | Add-Member -MemberType NoteProperty -Name Disk -Value $Drivec.DiskPath
$object | Add-Member -MemberType NoteProperty -Name “Capacity MB” -Value $Capacity
$object | Add-Member -MemberType NoteProperty -Name “Free MB” -Value $FreeSpace
$object | Add-Member -MemberType NoteProperty -Name “Free %” -Value $PercentFree
$finalResult += $object
}
}
$finalResult | Export-Csv “C:DiskUsageReport.csv” -NoTypeInformation -UseCulture # dump the report to .csv