Search code examples
vmwarepowercli

powercli site recovery manager: determine which protection group an unprotected VM will be in. SRM uses array based replication


I use automation to deply VM's to various vcenter clusters. I then confgiure SRM network mapping to create a network map between the cluster that the VM is in and the cluster which is used for DR purposes, in the protection group for those two clusters.

SRM is set up for array based replication, so as long as the VM is placed on replicated storage in the right cluster it will appear in SRM under the protection group, if a network mapping is in place then the VM will be auto protected by SRM or via my SRM config script.

I currently have the primary cluster, DR cluster and protection group hard coded, but would like to determine the protection group a VM is in and the name of the two clusters which the protection group is set up for, that way any changes to cluster configuration is automatically picked up and doesn't require manual changes to the SRM config script.

I've looked in the SRM API docs but it's not something I have worked out yet!


Solution

  • I have solved the issue:

    $credential = Get-Credential 
    $server_name = "test-server" 
      
    Connect-VIServer -Server $primaryDC -Credential $credential 
    $srmConnection = Connect-SrmServer -Credential $credential -RemoteCredential $credential 
    Connect-VIServer -Server $secondaryDC -Credential $credential 
     
    $srmApi = $srmConnection.ExtensionData 
    $protectionGroups = $srmApi.Protection.ListProtectionGroups() 
            
    foreach ($protectionGroup in $protectionGroups){ 
          $associatedVms = $protectionGroup.ListProtectedDatastores() | Get-VIObjectByVIView | Get-VM | Where-Object {($_.name -eq $server_name) -and($_.ExtensionData.Config.ManagedBy.ExtensionKey -ne 'com.vmware.vcDr' )} 
          foreach ($vm in $associatedVms) { 
    
              if ($vm.Name -eq $server_name) { 
                $protection_group_name = $protectionGroup.GetInfo().Name 
                $primary_cluster = get-vm -name $server_name | get-cluster 
                $primary_cluster_res_group = $primary_cluster.ExtensionData.ResourcePool 
                $srm_resource_groups = $srmApi.inventoryMapping.getResourcePoolMappings() 
    
                foreach ($resource_group in $srm_resource_groups){ 
                  if ($resource_group.PrimaryObject -eq $primary_cluster_res_group){ 
                    $secondary_res_group = $resource_group.SecondaryObject 
                  }             
                } 
              } 
           } 
        } 
      
    $secondary_cluster = Get-Cluster | Where-Object {$_.ExtensionData.ResourcePool -eq $secondary_res_group} 
    
    Write-Host "VM: $vm  -  Protection Group: $protection_group_name  -  Primary cluster: $primary_cluster  -  Secondary cluster: $secondary_cluster  -  Primary ResGrp: $primary_cluster_res_group  -  Secondary ResGrp: $secondary_res_group"