Merge-AVHDXwithnoCheckpoints

Aus Wiki-WebPerfect
Wechseln zu: Navigation, Suche

IMPORTANT: Use this script only if you have AVHDX-Files but no Checkpoints on the Hyper-V Node -> Get-VM | Get-VMCheckpoint


   <#
   .Synopsis
      Merge all AVHDX of a VM with the ParentDisk (.vhdx)
   .DESCRIPTION
      IMPORTANT: Use this script only if you have AVHDX-Files but no Checkpoints on the Hyper-V Node -> Get-VM | Get-VMCheckpoint
   .EXAMPLE
      Merge-AVHDXwithnoCheckpoint -VMName <VMName> -VMMServer <VMMServer>
      and configure the original .VHDX in Failover Cluster Manager (Select the ParentDisk (.vhdx) for each Disk)
   .AUTHOR
      v1.0 - 27.11.18: Robin Hermann
      v2.0 - 14.05.19: Robin Hermann
   #>
 
Function Merge-AVHDXwithnoCheckpoint {
 
    param(
    [Parameter(Position=0,mandatory=$true)]
    [string] $VMName,
    [Parameter(Position=1,mandatory=$true)]
    [string] $VMMServer
    )
 
    $VMVMMObject = Get-SCVirtualMachine -Name $VMName -VMMServer $VMMServer
    $VMHost = ($VMVMMObject).VMHost.ComputerName
 
    $RemoteVariables = Invoke-Command -ComputerName $VMHost -ScriptBlock {
        $VMInfo = Get-VM $Using:VMName
        $VMDisks = Get-VM -Name $Using:VMName | Get-VMHardDiskDrive
        $VMDifferencialDisks = $VMDisks | ? {$_.Path -like "*.avhdx"}
 
        $MergeVHDDestinationPath = @()
 
        If ($VMInfo) {
            # Check VM State -> The VM must PowerOff
            If ($VMInfo.State -eq "Off") {
                Write-Verbose "VERBOSE: The VM ($VMInfo.Name) is on the Hyper-V Node ($Using:VMHost)" 
 
                #Check has the VM DifferencialDisks
                If ($VMDifferencialDisks) {
                    Write-Verbose "Info: The VM ($VMInfo.Name) on the Hyper-V Node ($Using:VMHost) has DifferencialDisks"
 
                    Foreach ($VMDifferencialDisk in $VMDifferencialDisks) {
                        Write-Verbose "Info: Running Merge-Process for Disk $($VMDifferencialDisk.Path)"
 
                        $VMDifferencialDisksIdentifier = $VMDifferencialDisk | Select ControllerNumber, ControllerLocation
                        $VMDifferencialDiskConfigured = $VMDifferencialDisk.Path -split "\\" | select -Last 1
                        $VMDifferencialDiskConfiguredLocation = (($VMDifferencialDisk.Path).Replace("$VMDifferencialDiskConfigured","")).Trim("\") #If the VM Disks are not all in the same location
                        $StringwithoutAVHDX = ($VMDifferencialDiskConfigured.Trim(".avhdx")) #String: Remove .AVHDX at the end
                        $StringwithoutGUID = $StringwithoutAVHDX.Substring(0, $StringwithoutAVHDX.Length-37) #String: Remove GUID and the "_" at the end
 
                        $MergeVHDDestinationPath += ((ls $VMDifferencialDiskConfiguredLocation | ? {($_.Name -like "*.vhdx") -and ($_.Name -match $StringwithoutGUID)})).FullName
 
                        #Write-Host "$MergeVHDDestinationPath" -ForegroundColor DarkMagenta #-> Debugging
                        #Write-host ((ls $VMDifferencialDiskConfiguredLocation | ? {($_.Name -like "*.vhdx") -and ($_.Name -match $StringwithoutGUID)})).FullName #-> Debugging
 
                        Merge-VHD -Path ($VMDifferencialDisk.Path) -DestinationPath ((ls $VMDifferencialDiskConfiguredLocation | ? {($_.Name -like "*.vhdx") -and ($_.Name -match $StringwithoutGUID)})).FullName
                        Write-Host "SUCCESS: Merge-Process for Disk $($VMDifferencialDisk.Path) completed" -ForegroundColor Green
                        Remove-Item -Path "$($VMDifferencialDisk.Path).mrt" -Force -ErrorAction SilentlyContinue
                        Remove-Item -Path "$($VMDifferencialDisk.Path).rct" -Force -ErrorAction SilentlyContinue
 
                    }
                    Write-Host "IMPORTANT: ---> Please configure the original .VHDX in Failover Cluster Manager (Select the ParentDisk (.vhdx) for each Disk) -> It is not possible with VMM" -ForegroundColor Magenta
 
                } Else {
                    Write-Verbose "Info: The VM ($VMInfo.Name) has no AVHDX-Disks"
                }
            } Else {
                Write-Host "Error: The VM ($VMInfo.Name) is not in a supported State" -ForegroundColor Red
            }
        } Else {
            Write-Host "Error: Cannot find the selected VM ($VMInfo.Name) on the Hyper-V Node ($Using:VMHost)" -ForegroundColor Red
        }
    Write-Output $MergeVHDDestinationPath
    }
    #Set VM-Configuration to the ParentDisk -> Not possible because you can't configure an existing VHDX with VMM (only VHDX in VMM Library)
    #Write-Host $RemoteVariables -ForegroundColor DarkGray
}