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

These files are created and used by Changed Block Tracking.


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

Reply
0 Kudos
feders
Contributor
Contributor

Can someone please explain how to run this script?

Reply
0 Kudos
LucD
Leadership
Leadership

xTravirt has a good Beginners Guide, it includes information on how to run scripts.


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

Reply
0 Kudos
rama2027
Contributor
Contributor

how to export the results to a excel sheet. Can some one help me @@

Reply
0 Kudos
vmk2014
Expert
Expert

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 ?

 

VMps602PathFileSizeModDate
DC-DL580g5-01-LO-022-PL[DC-DL580g5-01-LO-022-PL] VMps602/VMps602_2-000002.vmdk1782579210/6/2015 22:17
DC-DL580g5-01-LO-022-PL[DC-DL580g5-01-LO-022-PL] VMps602/VMps602_1-000002.vmdk1782579210/6/2015 22:17
DC-DL580g5-01-LO-022-PL[DC-DL580g5-01-LO-022-PL] VMps602/VMps602-000002.vmdk1782579210/6/2015 22:17
DC-DL580g5-01-LO-022-PL[DC-DL580g5-01-LO-022-PL] VMps602/VMps602_2-000003.vmdk1782579210/13/2015 22:19
DC-DL580g5-01-LO-022-PL[DC-DL580g5-01-LO-022-PL] VMps602/VMps602_1-000003.vmdk1782579210/13/2015 22:19
DC-DL580g5-01-LO-022-PL[DC-DL580g5-01-LO-022-PL] VMps602/VMps602-000003.vmdk1782579210/13/2015 22:19
DC-DL580g7-02-VAX-15-PLDC-DL580g7-02-MAX-15-PL/VM570VM570.vmdk375809638405/23/2014 20:31
DC-DL580g7-02-VAX-15-PLDC-DL580g7-02-MAX-15-PL/VM570VM893_1.vmdk536870912011/13/2014 20:43
DC-DL580g7-02-VAX-15-PLDC-DL580g7-02-MAX-15-PL/VM570VMi892_2.vmdk1610612736011/13/2014 20:45
DC-DL580g8-01-XO-003-PLDC-DL580g7-02-MAX-15-PL/VM570VMmc002_2.vmdk53687091206/17/2015 22:09

Thanks

vm2014

Reply
0 Kudos
vmk2014
Expert
Expert

LucD,

    Is it possible to get the size of VMDK files in GB ?

Thanks

vm2014

Reply
0 Kudos
vmk2014
Expert
Expert

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

Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos
vmk2014
Expert
Expert

LucD,

    PFA script which i am using. I am unable to find the  parameter "Size = $_.FileSize" in the attached script


Kindly help me.


thanks

vm2014

Reply
0 Kudos
ccalvetTCC
Enthusiast
Enthusiast

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,

Blog: http://thecrazyconsultant.com/ | Twitter: @ccalvetTCC
Reply
0 Kudos
ccalvetTCC
Enthusiast
Enthusiast

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)

Blog: http://thecrazyconsultant.com/ | Twitter: @ccalvetTCC
Reply
0 Kudos
vmk2014
Expert
Expert

LucD,

    Did you get the chance to check the script. ?

thanks

vm2014

Reply
0 Kudos
LucD
Leadership
Leadership

The answer is just above this post.


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

Reply
0 Kudos