VMware Cloud Community
niblet
Contributor
Contributor
Jump to solution

PowerCLI / SRM - determine which protection group an unprotected VM is in

I can use PowerCLI to find which protection group that a VM is in, when it has SRM protection.
I would like to determine which group it is in, if it waiting to be configured for protection.
I use ABR for replication.

Secondly, is it possible to display which cluster resources are associated with a protection group, so that network mapping can be created between the two clusters?

 

0 Kudos
1 Solution

Accepted Solutions
niblet
Contributor
Contributor
Jump to solution

Solved it:
 
 
$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"

View solution in original post

2 Replies
niblet
Contributor
Contributor
Jump to solution

I can get the information of which protection group a specific server is in, using:

$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 {$_.name -eq $server_name}
      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

          }
       }
    }
   

Write-Host "VM: $vm   -   Protection Group: $protection_group_name   -   Primary cluster: $primary_cluster"
 
 
I can get the resource mappings, using $srmApi.inventoryMapping.getResourcePoolMappings() but they are in the ResourcePool-resgroup-123456  format and not a cluster name.  So I need to try and get the cluster and resource group to match...
0 Kudos
niblet
Contributor
Contributor
Jump to solution

Solved it:
 
 
$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"