daily pastebin goal
11%
SHARE
TWEET

Shrink a VMware VMDK from command line

anonit Jun 8th, 2014 315 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # This will run a VMWare defrag, prepare, shrink, and MS Contig defrag on a given list of VMDK files
  2.  
  3. # requires:
  4.  
  5. # Run as administrator
  6. # vmware-mount.exe, found at vmware: https://my.vmware.com/web/vmware/details?downloadGroup=VSP510-VDDK-510&productId=268
  7. # contig.exe, found at Microsoft: http://technet.microsoft.com/en-au/sysinternals/bb897428
  8.  
  9. # Location of vmware-vdiskmanager.exe
  10. $vdiskmanager='C:\Program Files (x86)\VMware\VMware Workstation\vmware-vdiskmanager.exe'
  11.  
  12. # Location of vmware-mount.exe
  13. $vmount='C:\Program Files (x86)\VMware\VMware Virtual Disk Development Kit\bin\vmware-mount.exe'
  14.  
  15. # Location of contig.exe
  16. # Run it once first by double clicking it to agree with the EULA
  17. $contig='c:\program files (x86)\sysinternals\contig.exe'
  18.  
  19. # Text file containing the full path of the VMDK files to shrink, one per line
  20. $sourcefile="d:\vmdkmaintenance\vmdklist.txt"
  21.  
  22. # Test file containing the file size before and after shrinking
  23. $resultsfile="d:\vmdkmaintenance\maint.txt"
  24.  
  25. # Drive letter to mount the VMDK as
  26. $mount_DriveLetter="m:"
  27.  
  28.  
  29.  
  30.  
  31. # ---------------------------------------------------------------------------#
  32.  
  33.  
  34. $vdisk_Defrag="-d"
  35. $vdisk_Prepare="-p"
  36. $vdisk_shrink="-k"
  37. $vmount_Volume="/v:"
  38. $vmount_Partitions="/p"
  39. $vmount_Dismount="/d"
  40. $contig_verbose="-v"
  41.  
  42.  
  43. # get the list of VMDK files and list the size
  44. get-content $sourcefile | foreach-object {get-childitem $_} | select fullname,length >> $resultsfile
  45.  
  46.  
  47.  
  48. get-content $sourcefile | ForEach-Object {
  49.     $activeMachine=$_;
  50.  
  51.     # Use the VMWare Disk Manager defrag on the active VMDK
  52.     write-host "Defrag $activeMachine"
  53.     & $vdiskmanager $vdisk_defrag $activeMachine
  54.    
  55.     # VMDK files may have multiple partitions, get the number of partitions for the active VMDK
  56.     write-host "List Partitions $activeMachine"
  57.     & $vmount $vmount_partitions "$activeMachine" > "$ENV:temp\VMDKPartition.txt"
  58.     $TotalPartitions=get-content "$ENV:temp\VMDKPartition.txt" | measure-object -line
  59.  
  60.     # For each partition in the active VMDK, mount the partition to the drive letter,
  61.     # prepare for shrinking and dismount    
  62.     for($activepartition=1; $activepartition -le $Totalpartitions.Lines; $activepartition++){
  63.        write-host "Preparing $activeMachine partition $activepartition"
  64.        & $vmount $mount_DriveLetter $activeMachine $vmount_Volume$activepartition
  65.        & $vdiskmanager $vdisk_prepare $mount_DriveLetter
  66.        & $vmount $mount_DriveLetter $vmount_Dismount
  67.     }
  68.        
  69.     # Shrink the active VMDK
  70.     write-host "Shrink $activeMachine"
  71.     & $vdiskmanager $vdisk_shrink $activeMachine
  72.  
  73.     # perform contig on the active VMDK
  74.     & $contig $contig_verbose $activeMachine    
  75. }
  76.  
  77. # get the list of VMDK files and list the size
  78. get-content $sourcefile | foreach-object {get-childitem $_} | select fullname,length >> $resultsfile
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top