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
LucD
Leadership
Leadership

So do I.
Perhaps add a Write-Host to show which ESXi node the script is connecting to?


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

Reply
0 Kudos
formz
Enthusiast
Enthusiast

I will take a look at that, good idea. The script did run and output a valid list, so I'll have to do some more digging as to what is throwing the error. Thanks!

Reply
0 Kudos
formz
Enthusiast
Enthusiast

That was it. When the host was taken out of MM there were no script errors. Strange.

I'm trying to figure out a way to delete just a specified set of VMs based on naming convention that starts with "tc-gt"

I'm thinking it'll be:

Get-DatastoreCluster -Name 'DataStoreClusterName' | Get-Datastore | Get-VM | Where-Object {$._name -like "tc-gt*"} | Remove-OrphanedData -delete

Is it valid to pass Remove-OrphanedData the names of the VMs like that?

Thanks!

Reply
0 Kudos
LucD
Leadership
Leadership

I'm afraid not, the function requires a Datastore as argument


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

Reply
0 Kudos
formz
Enthusiast
Enthusiast

Ah ok, thanks for the confirmation.

Reply
0 Kudos
formz
Enthusiast
Enthusiast

One more (seemingly dumb) question - does the script take into account name changes of the VM itself? IE: Rename the VM and obviously it doesn't update the name on the datastore.

Reply
0 Kudos
LucD
Leadership
Leadership

Yes, it should.

The script collects the VMDK filenames from the VM itself, so if they do have another name than 'normal', they will still return the actual name


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

Reply
0 Kudos
LucD
Leadership
Leadership

I would be interested to know what the following produces?

That would allow me to further analyse what you suspect might be a flaw in the script.

$vmName = 'VSX'

$vm = Get-VM -Name $vmName

$vm.ExtensionData.LayoutEx.File |

Select Name


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

Reply
0 Kudos
omar13489
Contributor
Contributor

The Output for these lines that give you the whole information about the File Object for VM  :

PowerCLI C:\> $vmName = 'VSX'

PowerCLI C:\> $vm = Get-VM -Name $vmName

PowerCLI C:\> $vm.ExtensionData.LayoutEx.File |

>> Select Name

>>

Name

----

[Local_DataStore_51.19] VSX/VSX.vmx

[Local_DataStore_51.19] VSX/VSX.vmxf

[Local_DataStore_51.19] VSX/VSX.nvram

[Local_DataStore_51.19] VSX/VSX.vmsd

[Local_DataStore_51.19] VSX/VSX.vmdk

[Local_DataStore_51.19] VSX/VSX-flat.vmdk

[Local_DataStore_51.19] VSX/VSX-000001.vmdk

[Local_DataStore_51.19] VSX/VSX-000001-delta.vmdk

[Local_DataStore_51.19] VSX/VSX-000002.vmdk

[Local_DataStore_51.19] VSX/VSX-000002-delta.vmdk

[Local_DataStore_51.19] VSX/VSX-Snapshot1.vmsn

[Local_DataStore_51.19] VSX/VSX-Snapshot2.vmsn

[Local_DataStore_51.19] VSX/vmware-2.log

[Local_DataStore_51.19] VSX/vmware-1.log

[Local_DataStore_51.19] VSX/vmware.log

This means that the File Related to VM, how the script considered it as Orphaned ?

Also the machine is not listed when I check the orphaned machine by :

Get-View -ViewType VirtualMachine -Filter @{'RunTime.ConnectionState'='disconnected|inaccessible|invalid|orphaned'}

Reply
0 Kudos
LucD
Leadership
Leadership

Excuse me, but the file that was mentioned by the function that you gave as an example, is in a different folder (root) and has a different name.

[Local_DataStore_51.19] VSXVSX.vmdk


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

Reply
0 Kudos
LucD
Leadership
Leadership

As a matter of fact, you seem to have a bunch of crap files in the root of that datastore.

[Local_DataStore_51.19] VSXVSX.nvram

[Local_DataStore_51.19] VSXVSX.vmdk

[Local_DataStore_51.19] VSXVSX-Snapshot1.vmsn

[Local_DataStore_51.19] VSXVSX-000001.vmdk

[Local_DataStore_51.19] VSXVSX-000002.vmdk

[Local_DataStore_51.19] VSXVSX-Snapshot2.vmsn

[Local_DataStore_51.19] VSXVSX.vmx

Unless you knowingly tried to create a VM in the root folder of the datastore, these all look like orphaned files.

Did you at some point forget to put the slash between the VSX VSX?


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

Reply
0 Kudos
omar13489
Contributor
Contributor

how it is located on different folder ?

The File located under this directory :

/vmfs/volumes/56ee729e-9af0d55f-3ffa-1402ec42a460/VSX/

-rw-------    1 root     root         491 Jan 10 09:31 VSX.vmdk

and it has the same name

but ls -lh show the above line!

and the slash was not shown with the the output i attached, the output came from the script

maybe you thought it is located inside root directory because I used "ls -lh" on the ESX ?

without ls -lh , only ls :

ls /vmfs/volumes/56ee729e-9af0d55f-3ffa-1402ec42a460/VSX/ | egrep VSX.vmdk

VSX.vmdk

Reply
0 Kudos
LucD
Leadership
Leadership

Open the File browser in the Web client on that datastore.


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

Reply
0 Kudos
omar13489
Contributor
Contributor

Here is the Datastore Browser from vSphere Client

pastedImage_0.png

Reply
0 Kudos
LucD
Leadership
Leadership

But what is in the root folder?


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

Reply
0 Kudos
LucD
Leadership
Leadership

Are there besides folders any files in the root?

In any case, can you run the following, and attach the report.txt file?

There must be something special with that VM, I can't reproduce what you are seeing.

And which vSphere and PowerCLI version are you using?

$vmName = 'VSX'

$vm = Get-VM -Name $vmName

$vm.ExtensionData.Layout | Out-File C:\Temp\report.txt

$vm.ExtensionData.Layout.Disk | Out-File C:\Temp\report.txt -Append

$vm.ExtensionData.Layout.Snapshot | Out-File C:\Temp\report.txt -Append

$vm.ExtensionData.LayoutEx.File | Out-File C:\Temp\report.txt -Append

$vm.ExtensionData.LayoutEx.Disk | Out-File C:\Temp\report.txt -Append


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

Reply
0 Kudos
omar13489
Contributor
Contributor

No Files inside the datatstore root directory . all of them folders

after running the code segment, attached the report.txt output

Reply
0 Kudos
omar13489
Contributor
Contributor

btw , this issue related to All VMs and not this specific VM

Reply
0 Kudos
LucD
Leadership
Leadership

I understand, but let's concentrate on one VM to determine the cause.

The problem is in the missing slash in the filepath

Name         : VSX.vmdk

Folder       : [Local_DataStore_51.19] VSXVSX.vmdk

Size         : 0

CapacityKB   : 31457280

Modification : 1/10/2018 9:31:35 AM

Owner        : root

Thin         : True

Extents      : [Local_DataStore_51.19] VSX/VSX-flat.vmdk

DiskType     : VirtualDiskFlatVer2BackingInfo

HWVersion    : 11

At the moment I have no idea why this would happen.

Which vSphere version and PowerCLI version are you using?

And in which regional settings are you running? What does Get-Culture say?

To eliminate any copy/paste issues, can you check if the attached copy of the functions comes back with the same results?


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

Reply
0 Kudos
omar13489
Contributor
Contributor

Hi LucD,

VSphere Client Version : 6.0.0, Build : 5112508

PowerCLI Version :

PowerCLI C:\> Get-PowerCLIVersion

PowerCLI Version

----------------

VMware vSphere PowerCLI 6.3 Release 1 build 3737840

---------------

PowerCLI C:\> Get-Culture

LCID             Name             DisplayName

----             ----             -----------

1033             en-US            English (United States)

and execute the attached function with the same results

Reply
0 Kudos