RajuVCP
Hot Shot
Hot Shot

Script to get Orphaned VM files across vCenter

Hi All,

Is there a script around, which can give us Orphaned VM file across vCenter, along with its Datastore names , orphaned VM file size, in Excel or CSV output file.

Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com
Tags (1)
0 Kudos
11 Replies
LucD
Leadership
Leadership

Have a look at Orphaned Files Revisited


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

0 Kudos
RajuVCP
Hot Shot
Hot Shot

Thanks LucD,

Just want to confirm before I use, this script will not delete orphaned VM files rite,

it will just give us the detailed information on Orphaned VMs.

Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com
0 Kudos
LucD
Leadership
Leadership

Correct, no files will be deleted


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

0 Kudos
RajuVCP
Hot Shot
Hot Shot

Thank LucD,

The page got 2 scripts. One is main script and other is to get report output. do I need to merger both main script and this one.

Or is it possible you paste the final script including the output file script lines here.

Thanks in Advance.

$reportName = 'C:\orphan-report.xlsx'

foreach($ds in (Get-Cluster -Name MyCluster | Get-Datastore | Get-VmwOrphan |

Group-Object -Property {$_.Folder.Split(']')[0].TrimStart('['))){

$ds.Group | Export-Excel -Path $reportName -WorkSheetname $ds.Name -AutoSize -AutoFilter -FreezeTopRow

}

Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com
0 Kudos
LucD
Leadership
Leadership

The 1st script on that page is the actual function.

The 2nd series of scripts (under Sample Usage), are examples of how you can call the function.

I attach a sample .ps1 file.
You have to adapt the lines at the bottom to reflect your environment.


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

0 Kudos
RajuVCP
Hot Shot
Hot Shot

LucD thanks for quick response.

I see  few lines as , will this lines are meant to remove Orphaned vmdk files?

If yes, I do not want to delete them now itself.  please correct me.

# Remove system files & folders from list

        $systemFiles = $fileTab.Keys | where{$_ -match "] \.|vmkdump"}

        $systemFiles | %{

          $fileTab.Remove($_)

        }

Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com
0 Kudos
LucD
Leadership
Leadership

No, these lines remove entries of a hash table.

I use a hash table to keep track of the files, and in this part of the code I remove the system files from the hash table.


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

0 Kudos
RajuVCP
Hot Shot
Hot Shot

Thanks LucD,

Am getting this error , am I missing something here...?

Missing closing '}' in statement block.

At C:\Users\rgunnal\Desktop\Orphaned_VM_6.ps1:143 char:79

+                 Group-Object -Property {$_.Folder.Split(']')[0].TrimStart('['

) <<<< )){

    + CategoryInfo          : ParserError: (CloseBraceToken:TokenId) [], Parse

   Exception

    + FullyQualifiedErrorId : MissingEndCurlyBrace

Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com
0 Kudos
LucD
Leadership
Leadership

Yes, there is a curly brace missing.

Replace those last lines with

$reportName = 'C:\orphan-report.xlsx'

foreach($ds in (Get-Cluster -Name MyCluster | Get-Datastore | Get-VmwOrphan |

    Group-Object -Property {$_.Folder.Split(']')[0].TrimStart('[')})){

    $ds.Group | Export-Excel -Path $reportName -WorkSheetname $ds.Name -AutoSize -AutoFilter -FreezeTopRow

}


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

0 Kudos
RajuVCP
Hot Shot
Hot Shot

Hi LucD,

do I need to add mycluster details in line (Get-Cluster -Name MyCluster | .....

am getting error as

Get-Cluster : 10/30/2018 6:23:35 AM    Get-Cluster        Cluster with name 'My

Cluster' was not found using the specified filter(s).

At C:\Users\ITSME\Desktop\Orphaned_VM_6.ps1:144 char:28

+ foreach($ds in (Get-Cluster <<<<  -Name MyCluster | Get-Datastore | Get-VmwOr

phan |

    + CategoryInfo          : ObjectNotFound: (:) [Get-Cluster], VimException

    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimA

   utomation.ViCore.Cmdlets.Commands.GetCluster

Then when I add my cluster names( is it should be host cluster or storage cluster name) there I added both, getting error as.

Get-Cluster : 10/30/2018 3:49:15 AM    Get-Cluster        Cluster with name 'Cluster001" was not found using the specified filter(s).

At C:\Users\ITSME\Desktop\Orphaned_VM_6.ps1:144 char:28

+ foreach($ds in (Get-Cluster <<<<  -Name "Cluster001" | Get-Datastore

| Get-VmwOrphan |

    + CategoryInfo          : ObjectNotFound: (:) [Get-Cluster], VimException

    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimA

   utomation.ViCore.Cmdlets.Commands.GetCluster

Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com
0 Kudos
LucD
Leadership
Leadership

Yes, you should put the name of your cluster in there (a regular cluster, not a datastorecluster).

You can check which clusters are visible with your current connection by doing a Get-Cluster.

You can check your connections by displaying the content of $global:defaultviservers.


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

0 Kudos