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

3 thoughts on “Wednesday Tidbit: Test an SRM Recovery Plan using PowerCLI

  1. Hi Mark,

    Hope you are well.

    Hoping this is a quick one. I have trying to use the script above but I am unable to login to the SRM 8.2 appliance using PowerShell. I can login ot the management page of the SRM Appliance but through Powershell\powercli, I get a username and password error:

    Connect-SrmServer : Cannot complete login due to an incorrect user name or password

    I have even changed the script to include the password so that I know it is correct

    $vcpassword = ConvertTo-SecureString ‘my-vc-password’ -AsPlainText -Force
    $vcCred = New-Object System.Management.Automation.PSCredential (“username”, $vcpassword)
    $srmpassword = ConvertTo-SecureString ‘my-srm-password’ -AsPlainText -Force
    $srmCred = New-Object System.Management.Automation.PSCredential (“admin”, $srmpassword)

    I have tried the admin and the root accounts but still get it kicked back. The VC connection works fine:

    Connect-ViServer $vc -Credential $vcCred
    Connect-SrmServer $srm -port 443 -Credential $srmCred

    Any ideas?

    Thanks

    Pete

    Like

      • Its the actural username.,.

        But the issue is that I cannot even connect to the SRM appliance over PowerCLI\PowerShell outside of the script as it gives me a username and password error but I can login to the appliance management page with that account fine

        Like

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.