VMware Cloud Community
Guv
Enthusiast
Enthusiast

orphaned vmdk files script

I found the below useful script for finding orphaned vmdk files as seen below:

  1. Purpose : List all orphaned vmdk on all datastores in all VC's

  2. Version: 1.1

  3. Author : HJA van Bokhoven

  4. 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.

72 Replies
Cy5
Contributor
Contributor

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

Reply
0 Kudos
russjar
Enthusiast
Enthusiast

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

VCP,MCSE NT4/W2k/W2k3, MCSA W2k3
Reply
0 Kudos
Matt_B1
Enthusiast
Enthusiast

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

Reply
0 Kudos
godbucket
Enthusiast
Enthusiast

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! Smiley Happy

Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos
godbucket
Enthusiast
Enthusiast

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!!!!

Reply
0 Kudos
drivera01
Enthusiast
Enthusiast

is there a way to limit the check to specific clusters?

Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos
godbucket
Enthusiast
Enthusiast

bump?

Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos
godbucket
Enthusiast
Enthusiast

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!!! Smiley Happy

Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos
godbucket
Enthusiast
Enthusiast

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 Smiley Happy

Reply
0 Kudos
LucD
Leadership
Leadership

Just replace that last line

$report

into

$report | Export-Csv "C:\orphans.csv" -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
godbucket
Enthusiast
Enthusiast

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! Smiley Happy

Reply
0 Kudos
RenerS
Contributor
Contributor

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.

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
godbucket
Enthusiast
Enthusiast

Beautiful. Works great! THANKS! Thanks to you and Luc. I appreciate you guys big time.

Reply
0 Kudos
drivera01
Enthusiast
Enthusiast

how about if we want to only report  out of inventory vms on particular clusters only?

Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos