You can use the vSphere Replication SDK for PowerShell to replicate a VM from a source to a target datastore on the local vCenter Server system.

This example shows how to replicate a single VM from a source to a target datastore on the same vCenter Server system.

Prerequisites

  • Verify that the vSphere Replication appliance is deployed on the vCenter Server system.

  • Verify that you are connected to the vCenter Server system. This example uses the $vr1 connection variable from Connect to a Local vSphere Replication Server.

  • You must have the required privileges to configure replications and manage datastores on the vCenter Server system.

Procedure

  1. Get the local VM that you want to replicate.
    $pairing = $vr1.ConnectedPairings["<local_vc_address_or_fqdn>"].Pairing
    $replicatedVm = Invoke-VrGetLocalVms -PairingId $pairing.PairingId `
        -VcenterId $pairing.LocalVcServer.Id `
        -FilterProperty "Name" `
        -Filter "<my_vm_name>" `
        -Server $vr1
  2. Get the VM hard disks that you want to replicate.
    $replicatedHdds = Invoke-VrGetLocalVmDisks -PairingId $pairing.PairingId `
        -VcenterId $pairing.LocalVcServer.Id `
        -VmId $replicatedVm.List[0].Id `
        -Server $vr1
  3. Get the target datastore for the replication.
    $targetDatastore = Invoke-VrGetVrCapableTargetDatastores -PairingId $pairing.PairingId `
        -VcenterId  $pairing.PairingId.RemoteVcServer.Id `
        -FilterProperty "Name" `
        -Filter "<target_datastore_name>" `
        -Server $vr1
  4. Create the configuration for disks replication.
    $replicationVmDisks = @()
    $replicatedHdds.List | ForEach-Object {
       $replicationVmDisks += Initialize-VrConfigureReplicationVmDisk -VmDisk $_ `
         -EnabledForReplication:$true `
         -DestinationDatastoreId $targetDatastore.List[0].Id `
         -DestinationDiskFormat 'SAMEASSOURCE'
  5. Create the replication spec.
    $replicationSpec = Initialize-VrConfigureReplicationSpec `
        -Rpo 60 -NetworkCompressionEnabled:$true `
        -MpitEnabled:$true -AutoReplicateNewDisks:$true `
        -LwdEncryptionEnabled:$false `
        -Disks $replicationVmDisks `
        -MpitInstances 1 `
        -MpitDays 1 `
        -TargetVcId $pairing.RemoteVcServer.Id `
        -VmId $replicatedVm.List[0].Id 
  6. Invoke the ConfigureReplication operation.
    $task = Invoke-VrConfigureReplication -PairingId $pairing.PairingId `
        -ConfigureReplicationSpec $replicationSpec -Server $vr1
    

    vSphere Replication starts replicating the virtual machine files to the target datastore.

  7. Monitor the task progress.
    $t = Invoke-VrGetTaskInfo -TaskId $task.List[0].Id -Server $vr1
    $t
     

    Eventually, the task status changes to SUCCESS.

  8. (Optional) Get the replication on the target datastore.
    $rep1 = Invoke-VrGetAllReplications -PairingId $pairing.PairingId `
    -FilterProperty "Name" -Filter <my_vm_name> -Server $vr1
  9. Sync your virtual machine to the configured replication on the target datastore.
    Invoke-VrSyncReplication -PairingId $pairing.PairingId `
        -ReplicationId $rep1.List[0].Id -Server $vr1