Zum Inhalt springen

Hyper-V Replica Script

  • Allgemein
  • von
#======================================================================== # Date: 2014.11.20  # Author: Guido Jeuken #======================================================================== #The Hyper-V Replica is great, but I don't like the way the replicated VMs are stored. # #I also like the possibility to exclude VHDX-Files from replication, to avoid unnecessary traffic and storage consumption. Think of the page-file... #But I want to include the excluded VHDX-Files in the replicated VM's to be prepared for the Disaster Recovery. # #The script part to include excluded VHDX-files in the replica VM is from Aashish Ramdas, many Thanks for it!! # http://blogs.technet.com/b/virtualization/archive/2014/05/11/excluding-virtual-disks-in-hyper-v-replica.aspx  # # I use the script after the creation of a new replica VM, while having initial replication scheduled. # The script assumes, that the original VM runs in a Hyper-V Cluster.  # The script moves the complete VM to a new folder defined as $destination\$VMNAME.  # Afterwards the original VM is scanned for excluded disks. If so, the excluded disk #are created, formatted and attached to the replica VM. # ---------------------------------------- #  $VMNAME = "YourVmName" $destination = "c:\Replica\" $Clustername = "YourClusterName"  #create the destinationfor VM $destVM = Join-Path $destination $VMNAME  #pass the VM name to Move-VMStorage and move the VHD Write-Host "Moving $VMNAME to $destVM" -foregroundcolor Cyan Move-VMStorage -VMName $VMNAME $destVM  # get VMs Primaryserver out of all Clusternodes $PRIMARYSERVER_ = get-clusternode -Cluster $Clustername | foreach-object {get-vm -computername $_ } | where {$_.replicationstate -notmatch "Disabled" -and $_.name -eq $VMNAME} | get-VMreplication | select primaryserver $PRIMARYSERVER=$PRIMARYSERVER_.primaryserver  ## Get VHD details from primary, replica $excludedDisks = get-clusternode -Cluster $Clustername | foreach-object {get-vm -computername $_ } | where {$_.replicationstate -notmatch "Disabled" -and $_.name -eq $VMNAME} | get-VMreplication |  select ExcludedDisks $includedDisks = Get-VMReplication -VMName $VMNAME | select ReplicatedDisks if( $excludedDisks -eq $null ) { exit }  #Get location of first replica VM disk $replicaPath = $includedDisks.ReplicatedDisks[0].Path | Split-Path -Parent  ## Create and attach each excluded disk foreach( $exDisk in $excludedDisks.ExcludedDisks ) { #Get the actual disk object $pDisk = Get-VHD -Path $exDisk.Path -ComputerName $PRIMARYSERVER $pDisk  #Create a new VHD on the Replica $diskpath = $replicaPath + "\" + ($pDisk.Path | Split-Path -Leaf) $newvhd = New-VHD -Path $diskpath ` -SizeBytes $pDisk.Size ` -Dynamic ` -LogicalSectorSizeBytes $pDisk.LogicalSectorSize ` -PhysicalSectorSizeBytes $pDisk.PhysicalSectorSize ` -BlockSizeBytes $pDisk.BlockSize ` -Verbose if($newvhd -eq $null) { Write-Host "It is assumed that the VHD [" ($pDisk.Path | Split-Path -Leaf) "] already exists and has been added to the Replica VM [" $VMNAME "]" continue; }  #Mount and format the new new VHD $newvhd | Mount-VHD -PassThru -verbose ` | Initialize-Disk -Passthru -verbose ` | New-Partition -AssignDriveLetter -UseMaximumSize -Verbose ` | Format-Volume -FileSystem NTFS -Confirm:$false -Force -verbose `  #Unmount the disk $newvhd | Dismount-VHD -Passthru -Verbose  #Attach disk to Replica VM Add-VMHardDiskDrive -VMName $VMNAME ` -ControllerType $exDisk.ControllerType ` -ControllerNumber $exDisk.ControllerNumber ` -ControllerLocation $exDisk.ControllerLocation ` -Path $newvhd.Path ` -Verbose } 

 

 

 

Schreibe einen Kommentar