Here is what I was saying about the $orphanSize = 0
foreach ($vCenter in $vCenters) {
$orphanSize = 0 # <<< since you want to tally orphan size per vcenter, for each vcenter you n...
See more...
Here is what I was saying about the $orphanSize = 0
foreach ($vCenter in $vCenters) {
$orphanSize = 0 # <<< since you want to tally orphan size per vcenter, for each vcenter you need to set it back to zero
$Report= @()
$arrUsedDisks = Get-View -ViewType VirtualMachine | % {$_.Layout} | % {$_.Disk} | % {$_.DiskFile}
Get-View -ViewType Datastore -Property Name,Browser,Host | %{
$ds = $_
$dsBrowser = Get-View $ds.browser
$rootPath = "[" + $ds.Name + "]"
$searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec -property @{matchPattern="*.vmdk";details=New-Object VMware.Vim.FileQueryFlags -property @{filesize=$true;modification=$true} }
$searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)
foreach ($folder in $searchResult) {
foreach ($fileResult in $folder.File) {
if ($fileResult.Path -match "-flat.vmdk"-and ($fileResult.Modification -lt $DaysOld)) {
if (-not ($arrUsedDisks -contains ($folder.FolderPath + $fileResult.Path))){
$orphanSize += $fileResult.FileSize
$row = "" | Select DataStore, Path, ProbablyOrphanedFile, SizeGB, LastModifiedOn, Host
$row.DataStore = $ds.Name
$row.Path = $folder.FolderPath
$row.ProbablyOrphanedFile = $fileResult.Path
$row.SizeGB = [math]::Round($fileResult.FileSize/1GB,1)
$row.LastModifiedOn = $fileResult.Modification
$row.Host = (Get-View $ds.Host[0].Key).Name
$report += $row
}
}
}
}
}
#also the following 5 lines were originally not part of your foreach vcenter loop
$ovmdkscount = $report.Count
$totalorphanedspace = ([Math]::Round($orphanSize/1GB,1))
$report | ConvertTo-Html –title "$vCenter - Orphaned VMDK Report" –body "<H2>$vCenter - Orphaned VMDK Report</H2>" -head $Header | Out-File $BasePath\$Date\$vCenter-OrphanedVMDKs-$Date.htm
$text = "$vCenter has got $ovmdkscount Orphaned VMDKs - Total Orphaned Space that could be reclaimed is $totalorphanedspace GB"
$text | Out-File $BasePath\$Date\ReportedvCentersStatus.txt -Append -Force
}
Not sure if that will solve your problem or not but that was a problem. Alternatively you could add this to each of your rows: $row.SizeBytes = $fileResult.FileSize and then calculate the total size at the end: $otherTotalOrphanedSpace = $report | Measure-Object -sum -property SizeBytes | select -expand Sum | foreach { [Math]::Round($_ / 1GB, 1) }