PowerCLI查询SRM中未受保护VM所属保护组及关联集群
Core Approach
Combine vSphere API data (for VM storage context) with SRM REST API calls to dynamically map VMs to their associated protection groups and paired primary/recovery clusters. This eliminates hardcoding by pulling live relationships directly from your infrastructure.
Step 1: Link VM's Datastore to SRM Replication Group
First, get the datastore your VM resides on via vSphere, then use SRM API to find the replication group tied to that datastore (array-based SRM replication groups are built around replicated datastore pairs).
PowerCLI Example:
# Fetch target VM and its datastore $vm = Get-VM -Name "MyTargetVM" $datastore = $vm | Get-Datastore # Authenticate to SRM REST API (pre-generate a valid token first) $srmBaseUrl = "https://your-srm-server/api/v1" $authHeaders = @{ "Authorization" = "Bearer $yourSrmAuthToken" } # Get all SRM replication groups $allReplicationGroups = Invoke-RestMethod -Uri "$srmBaseUrl/replication-groups" -Headers $authHeaders -Method Get # Filter for the replication group containing the VM's datastore $targetReplicationGroup = $allReplicationGroups.items | Where-Object { $_.primary_datastore.id -eq $datastore.Id -or $_.recovery_datastore.id -eq $datastore.Id }
Step 2: Map Replication Group to Protection Group
Array-based SRM protection groups are linked to replication groups. Query SRM to find which protection group is associated with the replication group from Step 1.
PowerCLI Example:
# Get all SRM protection groups $allProtectionGroups = Invoke-RestMethod -Uri "$srmBaseUrl/protection-groups" -Headers $authHeaders -Method Get # Find the protection group tied to the target replication group $vmProtectionGroup = $allProtectionGroups.items | Where-Object { $_.replication_group_ids -contains $targetReplicationGroup.id }
Step 3: Retrieve Paired Clusters from Protection Group
Each protection group is tied to a primary and recovery site. Use SRM API to get site details, then map those sites to their corresponding vCenter clusters.
PowerCLI Example:
# Extract site IDs from the protection group $primarySiteId = $vmProtectionGroup.primary_site_id $recoverySiteId = $vmProtectionGroup.recovery_site_id # Fetch site metadata $primarySite = Invoke-RestMethod -Uri "$srmBaseUrl/sites/$primarySiteId" -Headers $authHeaders -Method Get $recoverySite = Invoke-RestMethod -Uri "$srmBaseUrl/sites/$recoverySiteId" -Headers $authHeaders -Method Get # Map sites to clusters (parse cluster ID from SRM's resource pool reference) $primaryCluster = Get-Cluster -Id ($primarySite.resource_pool.id.Split(":")[1]) $recoveryCluster = Get-Cluster -Id ($recoverySite.resource_pool.id.Split(":")[1]) # Output results Write-Host "VM: $($vm.Name)" Write-Host "Protection Group: $($vmProtectionGroup.name)" Write-Host "Primary Cluster: $($primaryCluster.Name)" Write-Host "Recovery Cluster: $($recoveryCluster.Name)"
Key Notes
- Authentication: Ensure valid sessions/tokens for both vSphere (PowerCLI) and SRM REST API (use SRM's OAuth endpoint to generate tokens).
- Multi-Datastore VMs: If a VM spans multiple datastores, add logic to check all associated datastores and identify a common protection group.
- API Version: The examples use SRM 8.3+ REST API; adjust endpoints if using older SRM versions (e.g., SOAP API for pre-8.0 deployments).
内容的提问来源于stack exchange,提问作者Scott Birch-Horn

