Hyper-V Festplatten verkleinern

Schritt 1: Code kopieren und in der Datei "vhd-shrink.ps1" speichern
Evtl. muss die Variable "$VHDPATH" angepasst werden
  1. ################################################################################
  2. # Copyright (c) 2017, Manuel Strauch (manuel.strauch@outlook.com)
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are met:
  7. #
  8. #  -Redistributions of source code must retain the above copyright notice,
  9. #   this list of conditions and the following disclaimer.
  10. #  -Redistributions in binary form must reproduce the above copyright notice,
  11. #   this list of conditions and the following disclaimer in the documentation
  12. #   and/or other materials provided with the distribution.
  13. #
  14. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  18. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. # POSSIBILITY OF SUCH DAMAGE.
  25. ################################################################################
  26.  
  27. <#
  28. .SYNOPSIS
  29.     Verkleinert VHD und VHDX Images
  30.   .LINK
  31.   http://www.anukis.de
  32. .PARAMETER VHDFile
  33.         Einzelne VHD oder VHDX Datein verkleinern
  34.   .EXAMPLE
  35.   Verkleinern eines Image
  36.   .\vhd-shrink.ps1 -VHDFile WindowsXP.vhdx
  37.  
  38.   Verkleinern aller Images in den Verzeichnissen:
  39.   - C:\Virtual_Machines\Harddisks
  40.   - D:\Virtual_Machines\Harddisks
  41.   - E:\Virtual_Machines\Harddisks
  42.   .\vhd-shrink.ps1
  43. .NOTES
  44.         Author: Manuel Strauch
  45.         Date: 2017.01.28
  46. #>
  47.  
  48. ### Config
  49. Param(
  50.         [string]$VHDFile=""
  51. )
  52. $VHDDRIVE="CDE"
  53. $VHDPATH=":\Virtual_Machines\Harddisks"
  54. $MESSAGE="Keine VHD/VHDX Container gefunden!"
  55. $CURRENTDIR=pwd
  56.      
  57. ################################################################################
  58. ### Functions
  59. function vhdshrink(){
  60.         if ( $VHDFile -ne "" ){
  61.                 $files = $VHDFile
  62.         } else {
  63.                 $files = Get-ChildItem
  64.         }
  65.      
  66.         foreach($file in $files){
  67.         if ( $file -match ".vhd" ){
  68.             Write-Host ("  --> " + $file)
  69.             $len=0; gci $file | foreach{$len += $_.length};
  70.             Write-Host ("    - Anfangsgröße: " + [math]::Round($len/1GB, 2) + " GB" )
  71.             Get-VHD $file | where {$_.VhdType -eq "Dynamic"} | Optimize-VHD -mode full
  72.             $elen=0; gci $file | foreach{$elen += $_.length};
  73.             Write-Host ("    - Endgröße: " + [math]::Round($elen/1GB, 2) + " GB")
  74.             Write-Host ("    - Eingespart: " +  [math]::Round(($len - $elen)/1GB, 2) + " GB") -foreground "green"
  75.         }
  76.     }
  77. }
  78.      
  79. ################################################################################
  80. ### Program
  81.  
  82. if ( $VHDFile -ne "" ){
  83.         if ( Test-Path $VHDFile ){
  84.                 vhdshrink
  85.                 $MESSAGE="Container wurden verkleinert!"
  86.         }
  87. } else {
  88.         foreach($drive in $VHDDRIVE.toCharArray()){
  89.                 if ( Test-Path $drive$VHDPATH ){
  90.                         cd $drive$VHDPATH
  91.                         Write-Host ($drive + $VHDPATH + ":")
  92.                         vhdshrink
  93.                         $MESSAGE="Alle gefunden Container wurden verkleinert!"
  94.                 }
  95.         }
  96. }
  97.  
  98. ################################################################################
  99. # END
  100. cd $CURRENTDIR
  101. Write-Host ($MESSAGE)
  102. Read-Host -Prompt "Press Enter to exit"
Schritt 2: Script mit der rechten Maustaste "Mit Powershell starten" ausführen