[PSH] Raiders of the Lost VM files – MAJ
Posted by NiTRo | Filed under VMware, ZFS
MAJ 23/05/2015 : Mise à jours des scripts pour exclure que les fichiers “.lck” (VSAN).
MAJ 30/10/2012 : Mise à jours des scripts pour n’exclure que les vm “invalid” ou “orphaned”.
MAJ 06/07/2012 : Mise à jour des scripts pour exclure “*/hostCache/*” pour ne pas remonter les fichiers vswp de la fonction Host Cache d’ESXi 5.
MAJ 05/07/2012 : Mise à jour des scripts pour intégrer les fichiers vswp.xxxxxx, merci à Larus Hjartarson pour l’idée.
MAJ 21/03/2012 : Mise à jour des scripts pour exclure “var/tmp/cache” dans le cas d’une redirection de la partition scratch sur un datastore.
MAJ 06/03/2012 : Mise à jour des scripts pour le support des multi-datacenter/multi-vcenter, des snapshot NetApp et des fichiers zdump qui trainent.
MAJ 28/02/2012 : Mise à jour des scripts suite à une erreur dans le “SideIndicator”
MAJ 24/02/2012 : Et pour ceux qui ont *VRAIMENT* confiance, voici le script pour supprimer les fichiers vswp orphelins (et si vraiment vous en avez, vous pouvez changer le filtrage au niveau du |?{$_.lostfile -match “vswp”}) :
foreach ($lost in (compare -passthru -property path $(foreach ($ds in ((get-view -ViewType datastore|sort name -unique|?{$_.summary.accessible}|%{(get-view $_.browser).SearchDatastoreSubFolders($("[" + $_.Name + "]"),(New-Object VMware.Vim.HostDatastoreBrowserSearchSpec -property @{matchPattern=("*zdump*","*.xml","*.vmsn","*.vmsd","*.vswp*", "*.vmx","*.vmdk","*.vmss","*.nvram","*.vmxf"); details=(New-Object VMware.Vim.FileQueryFlags -property @{filesize=$true;modification=$true})}))})|?{$_.file})){foreach ($files in $ds.file){$files|?{$_.Path -notmatch "-ctk.vmdk|esxconsole.vmdk|esxconsole-flat.vmdk"}|select @{n="path";e={$ds.FolderPath + $files.path|%{$_ -replace " ",""}}},FileSize,Modification|?{$_.path -notmatch ".zfs|.snapshot|var/tmp/cache|/hostCache/|.lck"}}}) $(get-view -ViewType virtualmachine|?{$_.Runtime.ConnectionState -notmatch "invalid|orphaned"}|%{$_.layoutex.file}|?{$_.type -ne "log"}|%{$_.name}|%{$_ -replace " ",""}|sort -unique|select @{n="path";e={$_}})|?{$_.SideIndicator -eq "<="}|select @{n="LostFile";e={$_.Path -replace "]","] "}}, @{n="FileSize";e={"{0:N0}" -f ($_.FileSize/1MB)}}, Modification|?{$_.lostfile -match "vswp"})){(get-view (Get-View ServiceInstance|select -first 1).content.filemanager|select -first 1).DeleteDatastoreFile($lost.LostFile,(Get-View -ViewType datacenter|?{$_.Datastore -match (get-datastore $lost.LostFile.split("[]")[1]|get-view).moref}|select -first 1).moref)}
MAJ 24/02/2012 : Mise à jour du script pour afficher la taille en MegaBytes et la date de modification des fichiers.
Très fortement inspiré du célèbre Raiders of the Lost VMX (mais aussi du post Orphaned files and folders – Spring cleaning) de LucD, nous avons enfin fini de finioler un oneliner PowerCLI qui va vous lister les fichiers .xml, .vmsn, .vmsd, .vswp, .vmx, .vmdk, .vmss, .nvram et .vmxf qui traînent sur vos datastores et qui ne sont pas rattachés à une quelconque vm du vcenter depuis lequel vous avez exécuté le script.
compare -passthru -property path $(foreach ($ds in ((get-view -ViewType datastore|sort name -unique|?{$_.summary.accessible}|%{(get-view $_.browser).SearchDatastoreSubFolders($("[" + $_.Name + "]"),(New-Object VMware.Vim.HostDatastoreBrowserSearchSpec -property @{matchPattern=("*zdump*","*.xml","*.vmsn","*.vmsd","*.vswp*", "*.vmx","*.vmdk","*.vmss","*.nvram","*.vmxf"); details=(New-Object VMware.Vim.FileQueryFlags -property @{filesize=$true;modification=$true})}))})|?{$_.file})){foreach ($files in $ds.file){$files|?{$_.Path -notmatch "-ctk.vmdk|esxconsole.vmdk|esxconsole-flat.vmdk"}|select @{n="path";e={$ds.FolderPath + $files.path|%{$_ -replace " ",""}}},FileSize,Modification|?{$_.path -notmatch ".zfs|.snapshot|var/tmp/cache|/hostCache/|.lck"}}}) $(get-view -ViewType virtualmachine|?{$_.Runtime.ConnectionState -notmatch "invalid|orphaned"}|%{$_.layoutex.file}|?{$_.type -ne "log"}|%{$_.name}|%{$_ -replace " ",""}|sort -unique|select @{n="path";e={$_}})|?{$_.SideIndicator -eq "<="}|select @{n="LostFile";e={$_.Path -replace "]","] "}}, @{n="FileSize";e={"{0:N0}" -f ($_.FileSize/1MB)}}, Modification|ft -autosize
Les snapshots ZFS (.zfs/snapshot), les fichiers -ctk.vmdk (fichiers CBT) et les vmdk des COS d’ESX classic (esxconsole-) sont filtrés, n’hésitez pas à nous remonter d’autres faux positifs que vous pourriez rencontrer. Prochaine étape : ménage automatique…
Tags: CBT, oneliner, PowerCLI, powershell, scripting, snapshot, vmdk, VMFS, vmx
July 5th, 2012 at 12:26
[...] is in french so here is a link to his post through Google translate. And if you can read french, Allez. Share this:TwitterFacebookEmailLike this:LikeBe the first to like [...]
August 27th, 2014 at 15:35
Hi,
The compare script works fine, and gave me this:
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> compare -passthru -property path $(foreach ($ds in ((get-v
iew -ViewType datastore|sort name -unique|?{$_.summary.accessible}|%{(get-view $_.browser).SearchDatastoreSubFolders($(“[" + $_.Na
me + "]“),(New-Object VMware.Vim.HostDatastoreBrowserSearchSpec -property @{matchPattern=(“*zdump*”,”*.xml”,”*.vmsn”,”*.vmsd”,”*.v
swp*”, “*.vmx”,”*.vmdk”,”*.vmss”,”*.nvram”,”*.vmxf”); details=(New-Object VMware.Vim.FileQueryFlags -property @{filesize=$true;mod
ification=$true})}))})|?{$_.file})){foreach ($files in $ds.file){$files|?{$_.Path -notmatch “-ctk.vmdk|esxconsole.vmdk|esxconsole-
flat.vmdk”}|select @{n=”path”;e={$ds.FolderPath + $files.path|%{$_ -replace ” “,”"}}},FileSize,Modification|?{$_.path -notmatch “.
zfs|.snapshot|var/tmp/cache|/hostCache/”}}}) $(get-view -ViewType virtualmachine|?{$_.Runtime.ConnectionState -notmatch “invalid|o
rphaned”}|%{$_.layoutex.file}|?{$_.type -ne “log”}|%{$_.name}|%{$_ -replace ” “,”"}|sort -unique|select @{n=”path”;e={$_}})|?{$_.S
ideIndicator -eq ” foreach ($lost in (compare -passthru -property path $(fore
ach ($ds in ((get-view -ViewType datastore|sort name -unique|?{$_.summary.accessible}|%{(get-view $_.browser).SearchDatastoreSubFo
lders($(“[" + $_.Name + "]“),(New-Object VMware.Vim.HostDatastoreBrowserSearchSpec -property @{matchPattern=(“*zdump*”,”*.xml”,”*.
vmsn”,”*.vmsd”,”*.vswp*”, “*.vmx”,”*.vmdk”,”*.vmss”,”*.nvram”,”*.vmxf”); details=(New-Object VMware.Vim.FileQueryFlags -property @
{filesize=$true;modification=$true})}))})|?{$_.file})){foreach ($files in $ds.file){$files|?{$_.Path -notmatch “-ctk.vmdk|esxconso
le.vmdk|esxconsole-flat.vmdk”}|select @{n=”path”;e={$ds.FolderPath + $files.path|%{$_ -replace ” “,”"}}},FileSize,Modification|?{$
_.path -notmatch “.zfs|.snapshot|var/tmp/cache|/hostCache/”}}}) $(get-view -ViewType virtualmachine|?{$_.Runtime.ConnectionState -
notmatch “invalid|orphaned”}|%{$_.layoutex.file}|?{$_.type -ne “log”}|%{$_.name}|%{$_ -replace ” “,”"}|sort -unique|select @{n=”pa
th”;e={$_}})|?{$_.SideIndicator -eq “<="}|select @{n="LostFile";e={$_.Path -replace "]","] "}}, @{n="FileSize";e={"{0:N0}" -f ($_.
FileSize/1MB)}}, Modification|?{$_.lostfile -match "vswp"})){(get-view (Get-View ServiceInstance|select -first 1).content.filemana
ger|select -first 1).DeleteDatastoreFile($lost.LostFile,(Get-View -ViewType datacenter|?{$_.Datastore -match (get-datastore $lost.
LostFile.split("[]")[1]|get-view).moref}|select -first 1).moref)}
get-datastore : 8/27/2014 8:22:57 AM Get-Datastore Datastore with name 'iSCSI-C_Drive' was not found using the
specified filter(s).
At line:1 char:1339
+ … astore -match (get-datastore $lost.LostFile.split("[]")[1]|get-view).moref}|sele …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-Datastore], VimException
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetDatastore
Exception calling "DeleteDatastoreFile" with "2" argument(s): "Invalid datastore path '[iSCSI-C_Drive]
vCenterCBM/vmx-vCenterCBM-2622869734-2.vswp'."
At line:1 char:1160
+ foreach ($lost in (compare -passthru -property path $(foreach ($ds in ((get-view …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException
Some kind of problem with the argument passed to the DeleteDatastoreFile.
Thanks,
Jim
August 27th, 2014 at 15:38
Sorry…the output from the compare was:
LostFile FileSize Modification
——– ——– ————
[iSCSI-C_Drive] vCenterCBM/vmx-vCenterCBM-2622869734-2.vswp 126 9/23/2013 7:56:42 PM
[iSCSI-C_Drive] Win2K8R2/ViewSecurity-flat.vmdk 40,960 11/17/2012 7:12:51 PM
[iSCSI-C_Drive] Win2K8R2/ViewSecurity.vmdk 0 11/17/2012 7:23:15 PM
[iSCSI-C_Drive] Win2K8R2/ViewSecurity.nvram 0 11/17/2012 7:23:15 PM
[iSCSI-C_Drive] Win2K8R2/ViewSecurity.vmxf 0 11/17/2012 7:23:17 PM
[iSCSI-C_Drive] Win2K8R2/ViewSecurity.vmsd 0 11/17/2012 7:23:17 PM
August 27th, 2014 at 16:19
Hi Jim, so what was the issue?
August 27th, 2014 at 16:24
I don’t know…I am hoping the author can fix it
The problem has something to do with the parameters being passed to the DeleteDatastoreFile – the combination of the datastore and filename are not valid – wrong syntax or something.
Jim
August 27th, 2014 at 16:25
The compare script works fine.
It’s just the delete script that doesn’t work.
Jim
August 27th, 2014 at 16:26
ok sorry i didn’t see the previous message, i’ll have a look on the errors. Thanks for your feedback by the way!