Wednesday Tidbit: Test an SRM Recovery Plan using PowerCLI

20150713 - PowerCLIIn the last Wednesday Tidbit I showed how to protect a VM using SRM and PowerCLI.

In this one I show how to test a failover plan so that in the event of a disaster we know what to expect. Once the failover has been performed and recorded as successful, I will back it out.

Connect to SRM:

# Variables

$vc = "vc.nl.mdb-lab.com"
$srm = "vc3.uk.mdb-lab.com"
$credential = Get-Credential

Connect-ViServer $vc -Credential $credential
Connect-SrmServer $srm -Credential $credential

As before – set the variable, define the API and list the protection groups:

$SrmConnection = Connect-SrmServer $srm -Credential $credential
$SrmApi = $SrmConnection.ExtensionData
$SrmApi.Protection.ListProtectionGroups().GetInfo() | Format-Table Name,Type

This returns the following Protection Groups:

20150728 - 4

Now we need to list the recovery plans, but as mentioned previously, there are no cmdlets for this.  So we need to call the API’s member properties to see what is available:

$srmapi | Get-Member

This returns:

20150728 - 1

An easier way to visualise this is:

20150728 - 2

As we can see, there is a property for Recovery.  To list recovery plans, use:

$SrmApi.Recovery.ListPlans().GetInfo() | Format-Table Name,State

This gives us:

20150728 - 5

So we have a Protection Group called Exchange and a Recovery Plan called Test Exchange failover. Assign a variable so we can investigate further:

$RecoveryPlans=$SrmApi.Recovery.ListPlans()

List available members:

$RecoveryPlans | Get-Member

This returns:

20150728 - 3
From this point on I just want to work with the Test Exchange failover plan, so I set a variable for it (as Failover London and Exchange is first in the list, that would be assigned a zero):

$RPmoref = $SrmApi.Recovery.ListPlans()[1]

When dealing with a plan there are five actions we can take:

  • 1 – Test
  • 2 – Cleanup
  • 3 – Failover
  • 4 – Reprotect
  • 5 – Revert

To set the recovery action, we need to create an object for it then assign a value (see the SRM API developer’s guide for more information):

$RPmode = New-Object VMware.VimAutomation.Srm.Views.SrmRecoveryPlanRecoveryMode

Set the action to Test:

$RPmode.Value__ = 1

Now the plan is set to test, we just need to know how to start it. If you recall the Get-Member, there was an method called Start. Invoke this using:

$RPmoref.Start($RPmode)

This will test the Recovery Plan Test Exchange Failover. Once you are happy the test concluded successfully, set the plan to cleanup:

$RPmode.Value__ = 2

Begin the cleanup:

$RPmoref.Start($RPmode)

Disconnect from the VC and SRM server:

Disconnect-ViServer $vc -Confirm:$false
Disconnect-SrmServer $srm -Confirm:$false

The full PowerCLI script is srm_test_failover.ps1:

# Author:	@virtualhobbit
# Website:	http://virtualhobbit.com
# Ref:		Wednesday Tidbit: Test an SRM Recovery Plan using PowerCLI

# Variables
 
$vc = "vc.nl.mdb-lab.com"
$srm = "vc3.uk.mdb-lab.com"
$credential = Get-Credential
 

# Connect to VC and SRM server
Connect-ViServer $vc -Credential $credential
Connect-SrmServer $srm -Credential $credential

# Define API variable
$SrmConnection = Connect-SrmServer $srm -Credential $credential
$SrmApi = $SrmConnection.ExtensionData

# List Protection Groups
$SrmApi.Protection.ListProtectionGroups().GetInfo() | Format-Table Name,Type

# List Recovery Plans
$SrmApi.Recovery.ListPlans().GetInfo() | Format-Table Name,State

# Set variable
$RecoveryPlans=$SrmApi.Recovery.ListPlans()

# Begin work on Test Exchange failover Recovery Plan
$RPmoref = $SrmApi.Recovery.ListPlans()[1]

# Set recovery mode to Test
$RPmode = New-Object VMware.VimAutomation.Srm.Views.SrmRecoveryPlanRecoveryMode
$RPmode.Value__ = 1

# Begin test
$RPmoref.Start($RPmode)

# Pause script and wait for user to return
Write-Host "The script will now pause.  When you are satisfied the test has been successful, press enter to continue"
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

# Set recovery mode to Cleanup
$RPmode.Value__ = 2

# Begin Cleanup
$RPmoref.Start($RPmode)

# Disconnect from VC and SRM server
Disconnect-ViServer $vc -Confirm:$false
Disconnect-SrmServer $srm -Confirm:$false

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s