Hi,
Can you please help with the below:
1. This script is reporting the VMDKs of Powered Off VMs too as Orphaned.
2. We have a mix of Thick and Thin provisioned disks in our environment, so can we report only used size of the VMDK instead of Provisioned space. Currently it is fetching Provisioned size (wherever there are thick disks), which doesn't give real number for the total orphaned space that can be reclaimed, if we can fetch used size then the real orphaned space would make more sense.
3. It's reporting the disks of VM templates as Orphaned, how do we filter this out completely.
4. Also, what changes to the code can we make to filter out datastores with local (case insensitive) in name, these are very rare in our environment but still want to filter out before hand).
Appreciate your inputs. Thanks.
Current Version of the Script (main code):
report = @()
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 = try {
$dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)
}
Catch {
$Error1 = $Error[0].Exception.Message
LogWrite "On $vCenter - Error: $Error1
Skipping the Datastore."
}
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))){
$row = "" | Select DataStore, Path, ProbablyOrphanedFile, SizeGB, LastModifiedOn, Host, SizeBytes
$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
$row.SizeBytes = $fileResult.FileSize
$report += $row
}
}
}
}
}
$ovmdkscount = $report.Count
$totalorphanedspace = $report | Measure-Object -sum -property SizeBytes | select -expand Sum | foreach { [Math]::Round($_ / 1TB, 1) }
$report| select * -exclude SizeBytes | ConvertTo-Html –title "$vCenter - Orphaned VMDK Report" –body "<H2>$vCenter - Orphaned VMDK Report</H2>" -head $Header | Out-File $BasePath\$Date\$vCenter-OrphanedVMDKs-$Date.htm