Merge-AVHDXwithnoCheckpoints: Unterschied zwischen den Versionen

Aus Wiki-WebPerfect
Wechseln zu: Navigation, Suche
Zeile 13: Zeile 13:
 
   .AUTHOR
 
   .AUTHOR
 
       v1.0 - 27.11.18: Robin Hermann
 
       v1.0 - 27.11.18: Robin Hermann
 +
      v2.0 - 14.05.19: Robin Hermann
 
   #>
 
   #>
  
 
Function Merge-AVHDXwithnoCheckpoint {
 
Function Merge-AVHDXwithnoCheckpoint {
 
+
 
     param(
 
     param(
 
     [Parameter(Position=0,mandatory=$true)]
 
     [Parameter(Position=0,mandatory=$true)]
Zeile 23: Zeile 24:
 
     [string] $VMMServer
 
     [string] $VMMServer
 
     )
 
     )
 
+
 
     $VMVMMObject = Get-SCVirtualMachine -Name $VMName -VMMServer $VMMServer
 
     $VMVMMObject = Get-SCVirtualMachine -Name $VMName -VMMServer $VMMServer
 
     $VMHost = ($VMVMMObject).VMHost.ComputerName
 
     $VMHost = ($VMVMMObject).VMHost.ComputerName
   
+
 
     $RemoteVariables = Invoke-Command -ComputerName $VMHost -ScriptBlock {
 
     $RemoteVariables = Invoke-Command -ComputerName $VMHost -ScriptBlock {
 
         $VMInfo = Get-VM $Using:VMName
 
         $VMInfo = Get-VM $Using:VMName
 
         $VMDisks = Get-VM -Name $Using:VMName | Get-VMHardDiskDrive
 
         $VMDisks = Get-VM -Name $Using:VMName | Get-VMHardDiskDrive
 
         $VMDifferencialDisks = $VMDisks | ? {$_.Path -like "*.avhdx"}
 
         $VMDifferencialDisks = $VMDisks | ? {$_.Path -like "*.avhdx"}
       
+
 
         $MergeVHDDestinationPath = @()
 
         $MergeVHDDestinationPath = @()
   
+
 
         If ($VMInfo) {
 
         If ($VMInfo) {
 
             # Check VM State -> The VM must PowerOff
 
             # Check VM State -> The VM must PowerOff
 
             If ($VMInfo.State -eq "Off") {
 
             If ($VMInfo.State -eq "Off") {
 
                 Write-Verbose "VERBOSE: The VM ($VMInfo.Name) is on the Hyper-V Node ($Using:VMHost)"  
 
                 Write-Verbose "VERBOSE: The VM ($VMInfo.Name) is on the Hyper-V Node ($Using:VMHost)"  
 
+
 
                 #Check has the VM DifferencialDisks
 
                 #Check has the VM DifferencialDisks
 
                 If ($VMDifferencialDisks) {
 
                 If ($VMDifferencialDisks) {
 
                     Write-Verbose "Info: The VM ($VMInfo.Name) on the Hyper-V Node ($Using:VMHost) has DifferencialDisks"
 
                     Write-Verbose "Info: The VM ($VMInfo.Name) on the Hyper-V Node ($Using:VMHost) has DifferencialDisks"
 
+
 
                     Foreach ($VMDifferencialDisk in $VMDifferencialDisks) {
 
                     Foreach ($VMDifferencialDisk in $VMDifferencialDisks) {
 
                         Write-Verbose "Info: Running Merge-Process for Disk $($VMDifferencialDisk.Path)"
 
                         Write-Verbose "Info: Running Merge-Process for Disk $($VMDifferencialDisk.Path)"
 
+
 
                         $VMDifferencialDisksIdentifier = $VMDifferencialDisk | Select ControllerNumber, ControllerLocation
 
                         $VMDifferencialDisksIdentifier = $VMDifferencialDisk | Select ControllerNumber, ControllerLocation
 
                         $VMDifferencialDiskConfigured = $VMDifferencialDisk.Path -split "\\" | select -Last 1
 
                         $VMDifferencialDiskConfigured = $VMDifferencialDisk.Path -split "\\" | select -Last 1
                         $MergeVHDDestinationPath += ((ls $VMInfo.Path | ? {($_.Name -like "*.vhdx") -and ($_.Name -match ($VMDifferencialDiskConfigured).SubString(0,10))})).FullName
+
                        $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 "$MergeVHDDestinationPath" -ForegroundColor DarkMagenta #-> Debugging
                          
+
                         #Write-host ((ls $VMDifferencialDiskConfiguredLocation | ? {($_.Name -like "*.vhdx") -and ($_.Name -match $StringwithoutGUID)})).FullName #-> Debugging
                         Merge-VHD -Path ($VMDifferencialDisk.Path) -DestinationPath ((ls $VMInfo.Path | ? {($_.Name -like "*.vhdx") -and ($_.Name -match ($VMDifferencialDiskConfigured).SubString(0,10))})).FullName
+
 
 +
                         Merge-VHD -Path ($VMDifferencialDisk.Path) -DestinationPath ((ls $VMInfo.Path | ? {($_.Name -like "*.vhdx") -and ($_.Name -match $StringwithoutGUID)})).FullName
 
                         Write-Host "SUCCESS: Merge-Process for Disk $($VMDifferencialDisk.Path) completed" -ForegroundColor Green
 
                         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).mrt" -Force -ErrorAction SilentlyContinue
 
                         Remove-Item -Path "$($VMDifferencialDisk.Path).rct" -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
 
                     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 {
 
                 } Else {
 
                     Write-Verbose "Info: The VM ($VMInfo.Name) has no AVHDX-Disks"
 
                     Write-Verbose "Info: The VM ($VMInfo.Name) has no AVHDX-Disks"

Version vom 14. Mai 2019, 17:52 Uhr

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 $VMInfo.Path | ? {($_.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
}