Determine VMs in a SRM recovery plan using PowerShell

Here is some PowerShell code to determine the VMs that are in a VMware Site Recovery Manager recovery plan.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#requires -version 2
 
# Description: List the VMs in a SRM recovery plan.
# Usage: .\ListVMsInRecoveryPlan.ps1 -recoveryPlan <Recovery plan name> -dbServer <DB server name> -dbName <SRM db name>
 
param
(
    [String]$recoveryPlan = $(throw "A -recoveryPlan must be specified."),
    [String]$dbServer = $(throw "A -dbServer must be specified."),
    [String]$dbName = $(throw "A -dbName must be specified.")
)
 
$vmList = @()
 
# Wrap the specific .Net errors with our own user friendly message.
try
{
    # Setup SQL connection using trusted authentication
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
    $SqlConnection.ConnectionString = "Server=$dbServer; Database=$dbName; Integrated Security=True"
    $SqlConnection.Open()
}
catch
{
    throw ("Error connecting to the DB using '{0}'." -f $SqlConnection.ConnectionString)
}
 
# Query the database
try
{
 
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    $SqlCmd.Connection = $SqlConnection
 
    $query = @"
        SELECT sv.shadowvmname AS shadowvm_name
        FROM pdsr_shadowvm sv,
        	(SELECT sg.mo_id AS groupmoid, convert(varchar(255), g.string_val) AS shadowvmmoid
        		FROM pdsr_shadowgroup sg
        		LEFT OUTER JOIN g_string_array g
        		ON sg.vmmoids = g.seq_id) sg,
        	(SELECT rp.name AS plan_name, convert(varchar(255), g.string_val) AS shadowgroupmoid
        		FROM pdsr_recoveryprofile rp
        		LEFT OUTER JOIN g_string_array g
        		ON rp.shadowgroupmoids = g.seq_id) rp
        WHERE sg.shadowvmmoid = sv.mo_id
        	AND rp.shadowgroupmoid = sg.groupmoid
            AND rp.plan_name LIKE '{0}'
"@ -f $recoveryPlan
 
    $SqlCmd.CommandText = $query
    # Execute the query
    $data = $SqlCmd.ExecuteReader()
 
    while ($data.Read())
    {
        $vmList += $data.GetValue(0)
    }
}
catch
{
    throw "Error reading data from the database."
}
finally
{
    # Clean up the DB connections.
    $data.Close()
    $SqlConnection.Close()
}
 
# Return the results.
$vmList

Comments (3)

  1. abu obaid

    is there a way to start SRM recovery plan using PowerShell or powercli?

  2. Jake

    Whats the difference between the pdsr_ and the pdr_ tables? My pdsr_ tables are empty but the pdr_ has data….

  3. Eric (Post author)

    @abu I would check out https://blogs.vmware.com/vsphere/2014/03/powercli-for-srm.html and the SRM API.

    @Jake Depending on what you need, the API might be a better fit than the DB nowadays. The last time I looked at the tables I think one was for the protected site inventory and one was for the recovery site inventory. If you are in a failover only scenario, the tables may be essentially inverted between the two sites.

Leave a Comment

Your email address will not be published.