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.
Hey LucD,
running the script from PowerCLI prompt (VISERVER is vcenter server W2K8), same version of PowerCLI you listed
a mix of ESX 4.0 and ESXi 4.0
vcenter 4.0.0 build 208111
Thanks
I have the same error when running the script. The error is below.
Get-View : Cannot validate argument on parameter 'VIObject'. The argument is null or empty. Supply an argument that is not null or empty and then try t
he command again.
At line:15 char:23
+ $dsBrowser = Get-View <<<< $ds.browser
+ CategoryInfo : InvalidData: (:) [Get-View], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView
You cannot call a method on a null-valued expression.
At line:17 char:54
+ $searchResult = $dsBrowser.SearchDatastoreSubFolders <<<< ($rootPath, $searchSpec)
+ CategoryInfo : InvalidOperation: (SearchDatastoreSubFolders:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
In case you were referring to the original script in this post, it does not output the orphaned VMDKs by default. I added the following lines to get a CSV with the list.
$filename = "orphanedVMDKs.csv"
...
$report | Export-Csv $filename -NoTypeInformation
Invoke-Item $filename
Man, strangest thing...
Sorry in advance for the noob question, but the script just isn't working for me.
When I run the script, it doesn't generate any errors at all, and it goes through all my ESXi hosts and Datastores and lists them all in order in a single row. Then the script just stops/ends.
I know this sounds like it works, but I know for hard fact that I have orphaned vmdk's out there on some LUN's. I purposefully placed some on a few datastores just to see if the script was indeed returning true information.
Is there something I'm missing? The last post in this thread said something about exporting it to CSV format but I tried that with same results.
Please help! Thanks in advance!
There have been several variations on this script.
Could you perhaps attach the script you are actually using ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Oop! Duh... sorry I didnt specify before.
The script I'm using is the second post in this thread made by you actually...
http://communities.vmware.com/servlet/JiveServlet/download/1527048-37989/VMDK-orphaned.ps1
Its a great script and seems to work for everyone else just fine, and it technically runs correctly for me too, however its not displaying any true info in its output, ie. I have orphaned vmdk's and its not reporting them.
Thank you for your help!!!!
is there a way to limit the check to specific clusters?
With the script from the Orphaned files and folders – Spring cleaning post, you can do
$ds = Get-Cluster -Name MyCluster | Get-VMHost | Get-Datastore
Remove-OrphanedData -Datastore $ds
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
bump?
The results should be in the $report array.
Did you display or export that at the end of the script ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I'm not sure, I'm not a scripting guy per se, but I dont think so. I just ran the script that you posted in the beginning of this thread:
http://communities.vmware.com/servlet/JiveServlet/download/1527048-37989/VMDK-orphaned.ps1
Again, it returns the list of all my hosts and datastores, very slowly, but doesnt display whether I have any orphaned vmdk's or not. And I know for fact that I do because I placed some out there as a test.
What am I doing wrong? Thanks again for your help!!!
That script doesn't display the $report array either.
Can you try the script I attached in my previous reply, it has an extra line at the end.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Oh... duh! I am so sorry, I didnt even see that you had attached that. I wasnt looking.
Ok! Wow! Now it runs! Thanks!!!!
However now I see the need to export to a spreadsheet, because it logs tons of servername-ctk.vmdk files, which I know are change-block-tracking files.
So again, please excuse my idiocy, but is there a way to exclude CBT files and export everything into an Excel format?
Man, seriously, thanks again
Just replace that last line
$report
into
$report | Export-Csv "C:\orphans.csv" -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ah, now I understand. I guess I was looking at the script wrong; like I said, I'm not much of a scripting guy.
Thanks so much!!! This worked great!!! I appreciate all the help!
Hey LucD. Did you ever find a resolution to this? I am getting the same error
####
Get-View : Cannot validate argument on parameter 'VIObject'. The argument is null or empty. Supply an argument that is not null or empty and
then try the command again.
At C:\apps\VMWare\Scripts\PowerShell\VMDK-orphaned-2.ps1:15 char:23
+ $dsBrowser = Get-View <<<< $ds.Browser
+ CategoryInfo : InvalidData: (:) [Get-View], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView
#######
Line 15 = $dsBrowser = Get-View $ds.Browser
I too am connecting to vCenter from PowerGUI and from PowerCLI
PowerCLI Version
----------------
VMware vSphere PowerCLI 5.0 build 435427
---------------
Snapin Versions
---------------
VMWare AutoDeploy PowerCLI 5.0 build 575
VMWare ImageBuilder PowerCLI 5.0 build 575
VMware License PowerCLI 5.0 build 395016
VMWare vSphere PowerCLI 5.0 build 435427
When I run Get-Datastore | %{Get-View $_.Id} | Select Name,Browser
I get valid responses back. Sample output..
Name Browser
---- -------
LD9001-01 HostDatastoreBrowser-datastoreBrowser-datastore-3229
LD9001-01 HostDatastoreBrowser-datastoreBrowser-datastore-3229
LD9000-01 HostDatastoreBrowser-datastoreBrowser-datastore-2777
I am running the VMDK-orphaned-2.ps1 script you provided later in this thread (nov 4, 2011).
Any thoughts? I have 200 + hosts with over 500+ datastores accross 3 vCenter servers. It has proven impossible to keep up with orphans manually and I can’t seem to get a script running to find them.
Thanks a million in advance for your response.
I used Luc's VMDK-orphaned-2.ps1 script to create a new vCheck plugin. But while running the script in our environment, I noticed that it reports Changed Block Tracking *-ctk.vmdk files as orphaned files. So I modified the script to not report the *-ctk.vmdk files, by changing the line:
if ($fileResult.Path)
into:
if ($fileResult.Path -and $fileResult.Path -notlike "*-ctk.vmdk")
Hopefully this is also useful for someone else.
For more information about Changed Block Tracking see:
Changed Block Tracking (CBT) on virtual machines.
Message was edited by: RvdNieuwendijk
Beautiful. Works great! THANKS! Thanks to you and Luc. I appreciate you guys big time.
how about if we want to only report out of inventory vms on particular clusters only?
The original script from Raiders of the Lost VMX allows clusters on the entityName parameter and with the WhatIf switch it will only list the VMX files of unregistered VMs it finds.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference