Wednesday Tidbit: Protect a VM using SRM and PowerCLI

20150713 - PowerCLIWhen it was released last year, PowerCLI 5.5 brought with it the ability to do basic management of Site Recovery Manager. Whilst by no means trivial to configure, the functionality is there for when needed.

In this tidbit I will show how to connect to SRM, list the protection groups and add a VM to one.

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

Whilst the API is exposed, the cmdlets are still limited to connecting and disconnecting. Therefore we have to go through a process of setting variables and getting methods:

$SrmConnection = Connect-SrmServer $srm -Credential $credential

Define the API:

$SrmApi = $SrmConnection.ExtensionData

List the protection groups:

$srmApi.Protection.ListProtectionGroups()

Whilst this gives us the groups, listing them by their managed object reference (MoRef) isn’t very useful. Lets query it further:

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

20150722 - 1
This gives us the names of the protection groups and their type (in our case “VR” for vSphere Replication).

Lets add a VM to the protection group London. Define the VM:

$vmToAdd = Get-VM "exchange-mbx5.mail.mdb-lab.com"

Define the protection group:

$protectionGroups = $srmApi.Protection.ListProtectionGroups()
$targetProtectionGroup = $protectionGroups | where {$_.GetInfo().Name -eq "London" }

Reconfgure the protection group to include the VM:

$targetProtectionGroup.AssociateVms(@($vmToAdd.ExtensionData.MoRef))

Protect the VM:

$protectionSpec = New-Object VMware.VimAutomation.Srm.Views.SrmProtectionGroupVmProtectionSpec
$protectionSpec.Vm = $vmToAdd.ExtensionData.MoRef
$protectTask = $targetProtectionGroup.ProtectVms($protectionSpec)
while(-not $protectTask.IsComplete()) { sleep -Seconds 1 }

The VM is now added to the protection group London and is protected.
Disconnect from the vCenter and SRM server:

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

The full PowerCLI script is srm_basic.ps1:

# Variables

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

Connect-ViServer $vc -Credential $credential

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

$vmToAdd = Get-VM "exchange-mbx5.mail.mdb-lab.com"
$protectionGroups = $srmApi.Protection.ListProtectionGroups()
$targetProtectionGroup = $protectionGroups | where {$_.GetInfo().Name -eq "London" }

$targetProtectionGroup.AssociateVms(@($vmToAdd.ExtensionData.MoRef))
$protectionSpec = New-Object VMware.VimAutomation.Srm.Views.SrmProtectionGroupVmProtectionSpec
$protectionSpec.Vm = $vmToAdd.ExtensionData.MoRef
$protectTask = $targetProtectionGroup.ProtectVms($protectionSpec)
while(-not $protectTask.IsComplete()) { sleep -Seconds 1 }

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

One thought on “Wednesday Tidbit: Protect a VM using SRM and PowerCLI

  1. Pingback: Where to find me at VMworld 2016 Europe | virtualhobbit

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