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
1. I'm not sure how you calculate the content of $arrUsedDisks.
2. See my answer to your other thread
3. You can filter out the Templates on the Get-View cmdlet.
Get-View -ViewType Datastore -Property Name,Browser,Host,Config.Template -Filter @{
'Config.Template'=$false
}
4. I'm not sure this can be doen through the Get-View Filter parameter.
You can always pipe the results of the Get-View cmdlet to a Where-clause, and use a -notmatch operator
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Thanks for your responses so far.
Please find my below updates for specific points:
Get-View -ViewType Datastore -Property Name,Browser,Host,Config.Template -Filter @{
'Config.Template'=$false
} | %{
$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
}
}
Error
Get-View : 12/22/2016 11:01:11 AM Get-View Invalid object specified for the Filter parameter - 'Hashtable{String, Boolean}'. Filter accepts objects of type 'Hashtable{String, String}'.
At E:\Scripts\OrphanedVMDKs\OrphanedVMDKs-complete.ps1:71 char:5
+ Get-View -ViewType Datastore -Property Name,Browser,Host,Config.Template -Fi ...
3. My mistake, that filter should be 'Config.Template'='False'
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hmm..The error is saying there is no such property named config.template, when i use the below:
Get-View -ViewType Datastore -Property Name,Browser,Host,Config.Template -Filter @{'Config.Template'='False'} | %{
$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
}
}
Error:
Get-View : 12/22/2016 3:01:02 PM Get-View Invalid property: Config.Template
At E:\Scripts\OrphanedVMDKs\OrphanedVMDKs-complete.ps1:71 char:5
+ Get-View -ViewType Datastore -Property Name,Browser,Host,Config.Template -Fi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-View], InvalidArgument
+ FullyQualifiedErrorId : Client20_MoServiceImpl_GetNetInteropView_InvalidProperty,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView
This still didnt work. It says there is no such property called Config.Template.
Kindly check.
Get-View -ViewType Datastore -Property Name,Browser,Host,Config.Template -Filter @{'Config.Template'='False'} | %{
$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
}
}
Get-View : 12/22/2016 3:01:02 PM Get-View Invalid property: Config.Template
At E:\Scripts\OrphanedVMDKs\OrphanedVMDKs-complete.ps1:71 char:5
+ Get-View -ViewType Datastore -Property Name,Browser,Host,Config.Template -Fi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-View], InvalidArgument
+ FullyQualifiedErrorId : Client20_MoServiceImpl_GetNetInteropView_InvalidProperty,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView
Config.Template is a VritualMachine property, not a Datastore property.
That was in releation to the question 3., where you asked how to filter out templates.
But as I already remarked earlier, there are obviously parts missing in the script you posted here.
Where do you obtain the Virtual Machines?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ah sorry, in your previous reply as you mentioned config.template on Datastore view, I got confused.
Now the script looks ok (shared on the other thread, kindly take a look over there), generating data without error, but will take sometime to validate if there are any templates reporting still.
Also, as mentioned in the beginning of this thread, if there are any VMDKs of powered off VMs are reporting then that would be of big concern for me. So, i will validate this too by looking at the latest reported data.
Thank you for your inputs so far on this thread. Will consider this as pending as still while i validate the latest data.