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
   #>
 
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
                        $MergeVHDDestinationPath += ((ls $VMInfo.Path | ? {($_.Name -like "*.vhdx") -and ($_.Name -match ($VMDifferencialDiskConfigured).SubString(0,10))})).FullName
 
                        #Write-Host "$MergeVHDDestinationPath" -ForegroundColor DarkMagenta -> Debugging
 
                        Merge-VHD -Path ($VMDifferencialDisk.Path) -DestinationPath ((ls $VMInfo.Path | ? {($_.Name -like "*.vhdx") -and ($_.Name -match ($VMDifferencialDiskConfigured).SubString(0,10))})).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
}