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

This example shows how to replicate a single VM from a datastore on the local vCenter Server system to a datastore on a remote vCenter Server system.

Prerequisites

  • Verify that the vSphere Replication appliance is deployed on the source and target sites.

  • Verify that your VrServerConnection is authenticated against the source and target vCenter Server systems. This example uses the $vr2 connection variable from Connect to a Local and to a Remote vSphere Replication Server.

  • You must have the required privileges to configure replications and manage datastores on the source and target sites.

Procedure

  1. Get the local VM that you want to replicate.
    $pairing = $vr2.ConnectedPairings["<remote_vc_server_name>"].Pairing
    $replicatedVm = Invoke-VrGetLocalVms -PairingId $pairing.PairingId `
        -VcenterId $pairing1.LocalVcServer.Id `
        -FilterProperty "Name" `
        -Filter "<my_vm_name>" `
        -Server $vr2
    
  2. Get the VM hard disks that you want to replicate.
    $replicatedHdds = Invoke-VrGetLocalVmDisks -PairingId $pairing.PairingId `
        -VmId $replicatedVm.List[0].Id `
        -VcenterId $pairing.LocalVcServer.Id `
        -Server $vr2
    
  3. Get the target datastore on the remote site.
    $targetDatastore = Invoke-VrGetVrCapableTargetDatastores  -PairingId $pairing.PairingId `
        -VcenterId $pairing.RemoteVcServer.Id `
        -FilterProperty "Name" -Filter "<remote_datastore_name>" -Server $vr2
    
  4. Create the configuration for disks replication.
       $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 $vr2
    

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

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

    Eventually, the task status must change to SUCCESS.

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