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

The $rootPath variable was incorrect.

Attached a corrected version.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
Guv
Enthusiast
Enthusiast

thanks for that lucd. The script is running now. Its listing all datastores very slowly, so I am assuming that there are no orphaned vmdk files if it just lists the datatstores. otherwise it would show it up under the datastore. is that correct ?

Reply
0 Kudos
LucD
Leadership
Leadership

That is correct.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
vwman
Contributor
Contributor

Is there a way to have the script ignore local datastores? All of my local datastores contain the word "local".

Thanks

Scott

Reply
0 Kudos
LucD
Leadership
Leadership

Yes, if you change the 3th line in my script into this

...
$arrDS = Get-Datastore | where {$_.Name -notlike "*local*"} | Sort-Object -property Name
...

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
vwman
Contributor
Contributor

Perfect.

Thanks!

Reply
0 Kudos
karlgag
Contributor
Contributor

Hi LucD,

What about vm's with snapshot ? The vm property point on the last snapshot (myVmName-00000X.vmdk) and your script will show an orphaned vmdk for myVmName.vmdk. I challenge you because my script look like yours but I can't find a property who can show me the disk root parent.

I work with something like this => if ( $disk.Filename -like "00000?" )...

So, If you have another suggestion, I will be happy :smileyshocked:)

Thanks

Karl

Reply
0 Kudos
LucD
Leadership
Leadership

I would suggest to use the layoutEx property in the VirtualMachine object to find the snapshot root.

Kinda like I did in my yadr – A vdisk reporter post.

In my opinion you can't rely on the filename alone.

I have seen VMs where this doesn't follow anymore what you would expect.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
KSemea
Contributor
Contributor

Hi there,

I have run your script which completes sucsessfully.

But no output is generated. Im sure I have orphaned files does it writh the results to a textfile anywhere..?

Kr

Paul 

Reply
0 Kudos
LucD
Leadership
Leadership

The yadr script stores the result in a CSV file called "C:\Yadr-report.csv"


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

Reply
0 Kudos
Cy5
Contributor
Contributor

Hi, I get errors when running against vSphere 4.x (ESXi 4.x): does this script work in that environment?

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:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Get-orphaned-VMDK-vSphere.ps1:24 char:24
+         $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 C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Get-orphaned-VMDK-vSphere.ps1:35 char:56
+             $searchResult = $dsBrowser.SearchDatastoreSubFolders <<<< ($rootPath, $searchSpec)
+ CategoryInfo          : InvalidOperation: (SearchDatastoreSubFolders:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Reply
0 Kudos
LucD
Leadership
Leadership

Are you connected to the ESXi server or to a vCenter ?


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

Reply
0 Kudos
Cy5
Contributor
Contributor

Vcenter server, not directly to ESXi

Reply
0 Kudos
LucD
Leadership
Leadership

What type of datastore was being handled at the time of the error ?

The name should have been printed to the console.


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

Reply
0 Kudos
Cy5
Contributor
Contributor

Happens against every datastore. HP eva

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership

Does this return a Name and a Browser for each datastore ?

Get-Datastore | %{Get-View $_.Id} | Select Name,Browser


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

Reply
0 Kudos
Cy5
Contributor
Contributor

Yeah, that's where the problem lies. .Browser is empty

I only get at list of the DataStore names:

xxx_xxx_vmfs_data9

xxx_vmfs_templates

xxx_xxx_vmfs_data4

Reply
0 Kudos
Cy5
Contributor
Contributor

Hey LucD, any chance you've digged into this? Any direction you can provide would be appreciated.

Reply
0 Kudos
LucD
Leadership
Leadership

I did, but I have no clue why the datastore browser property is empty.

What PowerCLI version are you using ? Can you do a

Get-PowerCLIVersion

and check if this returns

PowerCLI Version
----------------
   VMware vSphere PowerCLI 4.1 U1 build 332441

You ran this against ESX 4.1 I understood, which vCenter version are you using ?

And finally, from which engine are you rnning the script ?

The PowerCLI prompt, the PowerShell IDE, PowerGui....


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

Reply
0 Kudos