VMware Cloud Community
Kumargtk
Contributor
Contributor

How to find Orphaned VMDK files

How can we find out Orphaned VMDK files in all the Data-stores.

119 Replies
formz
Enthusiast
Enthusiast

Amazing coincidence, I'm about to modify the script for the EXACT same reason.

rstack
Contributor
Contributor

As I am still learning here, Smiley Happy

Would it look something like this?

foreach($folder in $searchResult | Where {$folder.FolderPath -ne ".snapshot/*"})

I am guessing not I still them LOL

Reply
0 Kudos
LucD
Leadership
Leadership

I don't have access to my lab right now, but I would suggest a match operator.

Something like: foreach($folder in ($searchResult | Where {$folder.FolderPath -notmatch "snapshot"})

But be aware that this might cause skipped folders if you have some that have 'snapshot' in their name


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

Reply
0 Kudos
rstack
Contributor
Contributor

Thanks Luc.

Tried that Still getting snapshot folders.

Tried

foreach($folder in $searchResult | Where {$folder.FolderPath -notmatch "snapshot"})

foreach($folder in $searchResult | Where {$folder.FolderPath -notlike "*snapshot*"})

Extents: [DS] .snapshot/daily.2017-10-24_2100/<ServerName>/<ServerName>-flat.vmdk
Name   : <ServerName>.vmdk
Thin   : True

CapacityKB : 52428800

Folder : [DS] .snapshot/daily.2017-10-24_2100/<ServerName>/
Size   : 18209038336
Reply
0 Kudos
LucD
Leadership
Leadership

I'll have to check in my lab.
Hold on


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

formz
Enthusiast
Enthusiast

I tried this:

foreach($folder in ($searchResult | Where {$folder.FolderPath -notmatch "*.snapshot*"})){

As the snapshot folder is actually called .snapshot. But received a lot of errors:

At line:77 char:52

+ ... $searchResult | Where {$folder.FolderPath -notmatch "*.snapshot*"})){

+                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : OperationStopped: (:) [], ArgumentException

    + FullyQualifiedErrorId : System.ArgumentException

parsing "*.snapshot*" - Quantifier {x,y} following nothing.

At line:77 char:52

+ ... $searchResult | Where {$folder.FolderPath -notmatch "*.snapshot*"})){

Reply
0 Kudos
LucD
Leadership
Leadership

The dot and the asterisk are special characters in RegEx expressions.

And the (not)match operator expects a valid RegEx expression.

Try with the -notlike instead of -notmatch


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

Reply
0 Kudos
rstack
Contributor
Contributor

I had tried the -notlike and was receiving the same output as I posted before.

I tried

foreach($folder in $searchResult | Where {$folder.FolderPath -notlike "*snapshot*"})

I have even tried

foreach($folder in $searchResult | Where {$folder.FolderPath -notlike ".snapshot*"})

Reply
0 Kudos
rstack
Contributor
Contributor

Oh, also tried with the () in it like

foreach($folder in ($searchResult | Where {$folder.FolderPath -notlike "*.snapshot*"}))

Reply
0 Kudos
formz
Enthusiast
Enthusiast

Yeah this is my exact line 77

foreach($folder in ($searchResult | Where {$folder.FolderPath -notlike "*.snapshot*"})){

Still getting them listed.

Reply
0 Kudos
formz
Enthusiast
Enthusiast

Hey LucD​ have you had a chance to check this out in your lab? Thanks!

Reply
0 Kudos
LucD
Leadership
Leadership

My flight landed 3 hours ago, but I had a quick check in my lab.

With the following, I don't see any of the .snapshot folders popping up in the result

foreach($folder in ($searchResult | where{$_.FolderPath -notmatch "\] \.snapshot/"})){


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

formz
Enthusiast
Enthusiast

That did it LucD​ !!! Thank you so much for the help. Can you give a brief explanation as to why that worked and what I was doing wrong previously?

Reply
0 Kudos
LucD
Leadership
Leadership

We're taking all the results (in $searchResult) and we 'pipe' them through a Where-clause.

An object in the pipeline is addressed by the $_ variable, not the $folder variable.

That's also why I explicitly used the parenthesis, to show what order everything is done.


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

Reply
0 Kudos
formz
Enthusiast
Enthusiast

Perfect thank you!

Reply
0 Kudos
formz
Enthusiast
Enthusiast

Hey LucD​ do you have any advice in changing the script to run against a datastore cluster?

Reply
0 Kudos
LucD
Leadership
Leadership

This should work

Get-DatastoreCluster -Name MyDSC | Get-Datastore | Remove-OrphanedData


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

formz
Enthusiast
Enthusiast

That mostly worked. I say mostly because I don't think there is an error in the code but rather in my vCenter. It worked perfectly fine for one DS cluster but not for another. I got this error:

Exception calling "SearchDatastoreSubFolders" with "2" argument(s): "An error occurred while communicating with the remote host."

At C:\Users\mhenze_sa\Documents\Scripts\Delete Orphan VMDKs.ps1:76 char:9

+         $searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)

+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : VimException

Reply
0 Kudos
LucD
Leadership
Leadership

Is perhaps one of the ESXi nodes in a cluster that is linked to the DSC powered off?


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

Reply
0 Kudos
formz
Enthusiast
Enthusiast

One is in maintenance mode. I thought that would have been okay but maybe that's where the error lies.

Reply
0 Kudos