I found the below useful script for finding orphaned vmdk files as seen below:
Purpose : List all orphaned vmdk on all datastores in all VC's
Version: 1.1
Author : HJA van Bokhoven
Modifications: LucD
$report = @()
$arrUsedDisks = Get-View -ViewType VirtualMachine | % {$_.Layout} | % {$_.Disk} | % {$_.DiskFile}
$arrDS = Get-Datastore | Sort-Object -property Name
foreach ($strDatastore in $arrDS) {
Write-Host $strDatastore.Name
$ds = Get-Datastore -Name $strDatastore.Name | % {Get-View $_.Id}
$fileQueryFlags = New-Object VMware.Vim.FileQueryFlags
$fileQueryFlags.FileSize = $true
$fileQueryFlags.FileType = $true
$fileQueryFlags.Modification = $true
$searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
$searchSpec.details = $fileQueryFlags
$searchSpec.matchPattern = "*.vmdk"
$searchSpec.sortFoldersFirst = $true
$dsBrowser = Get-View $ds.browser
$rootPath = ""
$searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)
foreach ($folder in $searchResult)
{
foreach ($fileResult in $folder.File)
{
if ($fileResult.Path)
{
if (-not ($arrUsedDisks -contains ($folder.FolderPath + $fileResult.Path))){
$row = "" | Select DS, Path, File, Size, ModDate
$row.DS = $strDatastore.Name
$row.Path = $folder.FolderPath
$row.File = $fileResult.Path
$row.Size = $fileResult.FileSize
$row.ModDate = $fileResult.Modification
$report += $row
}
}
}
}
}
When I run this script, it lists my datastores, but then after each datastore it brings up the following error message:
Exception calling "SeachDatastoresubfolders" with "2" argument (s): Invalid datastore path..........
Is there any reason why this script would bring this error message, I am using the lastest version of powercli client, or would it mean I have no orphaned vmdk files. Any advise.
These files are created and used by Changed Block Tracking.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Can someone please explain how to run this script?
xTravirt has a good Beginners Guide, it includes information on how to run scripts.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
how to export the results to a excel sheet. Can some one help me @@
Hi LucD,
When i executed the script its shows the modified date Oct 6th - 13th for the vmdk files, so does that mean that the vmdk file is still in use ? or its orphaned file. I am afraid to delete.
Also, can we get the size in GB ?
VMps602 | Path | File | Size | ModDate |
DC-DL580g5-01-LO-022-PL | [DC-DL580g5-01-LO-022-PL] VMps602/ | VMps602_2-000002.vmdk | 17825792 | 10/6/2015 22:17 |
DC-DL580g5-01-LO-022-PL | [DC-DL580g5-01-LO-022-PL] VMps602/ | VMps602_1-000002.vmdk | 17825792 | 10/6/2015 22:17 |
DC-DL580g5-01-LO-022-PL | [DC-DL580g5-01-LO-022-PL] VMps602/ | VMps602-000002.vmdk | 17825792 | 10/6/2015 22:17 |
DC-DL580g5-01-LO-022-PL | [DC-DL580g5-01-LO-022-PL] VMps602/ | VMps602_2-000003.vmdk | 17825792 | 10/13/2015 22:19 |
DC-DL580g5-01-LO-022-PL | [DC-DL580g5-01-LO-022-PL] VMps602/ | VMps602_1-000003.vmdk | 17825792 | 10/13/2015 22:19 |
DC-DL580g5-01-LO-022-PL | [DC-DL580g5-01-LO-022-PL] VMps602/ | VMps602-000003.vmdk | 17825792 | 10/13/2015 22:19 |
DC-DL580g7-02-VAX-15-PL | DC-DL580g7-02-MAX-15-PL/VM570 | VM570.vmdk | 37580963840 | 5/23/2014 20:31 |
DC-DL580g7-02-VAX-15-PL | DC-DL580g7-02-MAX-15-PL/VM570 | VM893_1.vmdk | 5368709120 | 11/13/2014 20:43 |
DC-DL580g7-02-VAX-15-PL | DC-DL580g7-02-MAX-15-PL/VM570 | VMi892_2.vmdk | 16106127360 | 11/13/2014 20:45 |
DC-DL580g8-01-XO-003-PL | DC-DL580g7-02-MAX-15-PL/VM570 | VMmc002_2.vmdk | 5368709120 | 6/17/2015 22:09 |
Thanks
vm2014
LucD,
Is it possible to get the size of VMDK files in GB ?
Thanks
vm2014
LucD,
Could you,please let me know how to get the size of VMDK in GB's. Also, want to confirm the output generated for vmdk file size in KB is right ?
Size |
17825792 |
17825792 |
17825792 |
17825792 |
17825792 |
17825792 |
37580963840 |
5368709120 |
16106127360 |
5368709120 |
0 |
1.61061E+11 |
16106127360 |
16106127360 |
16106127360 |
16106127360 |
21474836480 |
42949672960 |
8323072 |
Thanks
vm2014
The Size property is expressed in bytes.
To get the value in GB you replace this line
Size = $_.FileSize
with this line
Size = [math]::Round($_.FileSize/1GB,1)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi,
Regarding the “modified date”
It indicates the last time a file has been “modified” by VMware.
For example with a vmdk, it could be linked to the latest storage vmotion or snapshot consolidation.
It means that it is possible to have a very old “modified date” while the VM is still running in production as long as there were no “tasks” on the VM.
In other words a very recent date means that the file is very likely not orphaned.
The opposite is not true, an old date doesn’t mean that the file is orphaned.
Regards,
The example of LucD is good.
In the attached script you just need to replace the following line:
$row.Size = $fileResult.FileSize
By this line
$row.Size = [math]::Round($fileResult.FileSize/1GB,1)
LucD,
Did you get the chance to check the script. ?
thanks
vm2014
The answer is just above this post.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference