inconsistentFoldersAndVMDK script

###########################################################################################
    
###########################################################################################
# Description: check vm folder or orphline folder
# output document: inconsistentFoldersAndVMDK.csv
###########################################################################################

##################
# Add VI-toolkit #
##################
& {
$ErrorActionPreference = "silentlycontinue"
$vmwareSnap = get-pssnapin -name "VMware.VimAutomation.Core"
if (! $?)
{
 Add-PSsnapin VMware.VimAutomation.Core
 Initialize-VIToolkitEnvironment.ps1
}
}

##################
# Connexion to VC #
##################
$vcserver = "vcentername here"
connect-VIServer $vcserver
$filelocation1="c:\tmp\InconsistentFoldersAndVMDK.csv"
function InconsistentFoldersAndVMDK
{
 Write-host "Processing: InconsistentFoldersAndVMDK"

 $exclusion1 = "SWAP"
 $exclusion2 = "LOCAL"
 $exclusion3 = "NFS"

 $datastores = Get-Datastore | where {$exclusion1 -notcontains $_.name -and $exclusion2 -notcontains $_.name -and $exclusion3 -notcontains $_.name} | Sort Name
 $vms = Get-VM
 $templates = Get-Template
 $usedDisk = @()
 $orphanedVMDKList = @()

 foreach ($vm in $vms)
 {
  $vmView = Get-View $vm
  foreach($disk in $vmView.Layout.Disk)
  {
   $usedDisk += $disk.DiskFile
  }
 
 }

 foreach($template in $templates)
 {
  foreach($device in $template.ExtensionData.Config.Hardware.Device)
  {
  
   if(($device).tostring() -eq "VMware.Vim.VirtualDisk")
   {
    $usedDisk += $device.Backing.Filename
   }
  }
 }

 $uniqueUsedDisk = @()
 foreach ($datastore in $datastores)
 {
   $dsName = $datastore.Name
   $dsv = $datastore | Get-View
  
   $fqFlags = New-Object VMware.Vim.FileQueryFlags
   $fqFlags.FileSize = $true
   $fqFlags.FileType = $true
   $fqFlags.Modification = $true
   $searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
   $searchSpec.details = $fqFlags
   $searchSpec.matchPattern = "*.vmdk"
   $dsBrowser = Get-View -Server $managedHost $dsv.browser
   $rootPath = "["+$dsv.summary.Name+"]"
  
   ## Special thanks to Yasen Kalchev @ VMware for a vSphere fix
   if ($dsBrowser.Client.Version -eq "Vim4") {
    $searchSpec = [VMware.Vim.VIConvert]::ToVim4($searchSpec)
    $searchSpec.details.fileOwnerSpecified = $true
    $dsBrowserMoRef = [VMware.Vim.VIConvert]::ToVim4($dsBrowser.MoRef);
    $searchTaskMoRef = $dsBrowser.Client.VimService.SearchDatastoreSubFolders_Task($dsBrowserMoRef, $rootPath, $searchSpec)
    $searchResult = [VMware.Vim.VIConvert]::ToVim($dsBrowser.WaitForTask([VMware.Vim.VIConvert]::ToVim($searchTaskMoRef)))
   } else {
    trap {
     if ($_.Exception.MethodFault -is [VMware.Vim.FileNotFound]) {
      continue
     } elseif ($_.Exception.MethodFault -is [VMware.Vim.NoPermission]) {
      Write-Warning $_
      continue
     }
     else {
      Write-Warning $_
      continue
     }
    }
    $searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)
   }
   foreach ($fPath in $searchResult){
    $folderPath = $fPath.folderPath
    if ($fPath.File){
     $fPath.File | ForEach-Object {
      $fileSize = $_.FileSize/1024
      $lastModified = $_.Modification
      $_.Path | ForEach-Object {
       $finalFolderPath = ($folderPath + $_)
       if (($usedDisk -notcontains $finalFolderPath) -and ($uniqueUsedDisk -notcontains $finalFolderPath) -and ($finalFolderPath -notlike "*-rdm*"))
       {
        $uniqueUsedDisk += $finalFolderPath
       
        $ligne = "" | Select-Object Datastore, Folder, File, SizeGB, Modified
        $ligne.Datastore = $dsname
        $ligne.Folder = $folderPath
        $ligne.File = $_
        $ligne.SizeGB = $fileSize
        $ligne.Modified = $lastModified
      
        $orphanedVMDKList+=$ligne
       }
      }
     }
    }
   }
 }
 $orphanedVMDKList | Export-CSV $filelocation1 -NoTypeInformation
}
InconsistentFoldersAndVMDK
##############################
# Disconnect session from VC #
##############################
disconnect-viserver -confirm:$false
#######################################################
# end of script                                       #
#######################################################

Commentaires

Messages les plus consultés de ce blogue

VMware Explore 2024 Party list events

VMware Explore 2025 registration is now live and there is some great improvements

Changement dans les licences VMware à compter du 10 avril 2025