Schritt 1: Code kopieren und in der Datei "vhd-shrink.ps1" speichern
Evtl. muss die Variable "$VHDPATH" angepasst werden
- ################################################################################
- # Copyright (c) 2017, Manuel Strauch (manuel.strauch@outlook.com)
- # All rights reserved.
- #
- # Redistribution and use in source and binary forms, with or without
- # modification, are permitted provided that the following conditions are met:
- #
- # -Redistributions of source code must retain the above copyright notice,
- # this list of conditions and the following disclaimer.
- # -Redistributions in binary form must reproduce the above copyright notice,
- # this list of conditions and the following disclaimer in the documentation
- # and/or other materials provided with the distribution.
- #
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- # POSSIBILITY OF SUCH DAMAGE.
- ################################################################################
- <#
- .SYNOPSIS
- Verkleinert VHD und VHDX Images
- .LINK
- http://www.anukis.de
- .PARAMETER VHDFile
- Einzelne VHD oder VHDX Datein verkleinern
- .EXAMPLE
- Verkleinern eines Image
- .\vhd-shrink.ps1 -VHDFile WindowsXP.vhdx
- Verkleinern aller Images in den Verzeichnissen:
- - C:\Virtual_Machines\Harddisks
- - D:\Virtual_Machines\Harddisks
- - E:\Virtual_Machines\Harddisks
- .\vhd-shrink.ps1
- .NOTES
- Author: Manuel Strauch
- Date: 2017.01.28
- #>
- ### Config
- Param(
- [string]$VHDFile=""
- )
- $VHDDRIVE="CDE"
- $VHDPATH=":\Virtual_Machines\Harddisks"
- $MESSAGE="Keine VHD/VHDX Container gefunden!"
- $CURRENTDIR=pwd
- ################################################################################
- ### Functions
- function vhdshrink(){
- if ( $VHDFile -ne "" ){
- $files = $VHDFile
- } else {
- $files = Get-ChildItem
- }
- foreach($file in $files){
- if ( $file -match ".vhd" ){
- Write-Host (" --> " + $file)
- Write-Host (" - Anfangsgröße: " + [math]::Round($len/1GB, 2) + " GB" )
- Write-Host (" - Endgröße: " + [math]::Round($elen/1GB, 2) + " GB")
- Write-Host (" - Eingespart: " + [math]::Round(($len - $elen)/1GB, 2) + " GB") -foreground "green"
- }
- }
- }
- ################################################################################
- ### Program
- if ( $VHDFile -ne "" ){
- if ( Test-Path $VHDFile ){
- vhdshrink
- $MESSAGE="Container wurden verkleinert!"
- }
- } else {
- foreach($drive in $VHDDRIVE.toCharArray()){
- if ( Test-Path $drive$VHDPATH ){
- cd $drive$VHDPATH
- Write-Host ($drive + $VHDPATH + ":")
- vhdshrink
- $MESSAGE="Alle gefunden Container wurden verkleinert!"
- }
- }
- }
- ################################################################################
- # END
- cd $CURRENTDIR
- Write-Host ($MESSAGE)
- Read-Host -Prompt "Press Enter to exit"