Home > DataONTAP, ESX, NetApp, PowerCLI, Powershell, VMware > SRM for free with PowerCLI & DataONTAP– Part 2: iSCSI

SRM for free with PowerCLI & DataONTAP– Part 2: iSCSI

September 14th, 2011

My updated previous NFS post talked about implementing part of the functionality of SRM in Powershell using PowerCLI and NetApp’s DataONTAP for NFS datastores.

This post is an update to my previous iSCSI post in which I had only looked at the VMware side of the automation and so it’s time for an update to include the important part of handling the underlying storage. I’m going to use NetApp storage as an example but any storage vendor that exposes their API to Powershell can be used if you can find the relevant commands.

You will obviously need both PowerCLI and Netapp’s DataONTAP installed. See my previous post: Pimping your Powershell Profile for getting everything installed.

For the storage automation, all you really have to do is connect to the filer and then quiesce and break the storage mirror so it is writeable at the BR site. After you’ve connected this is a single line:

Get-NaSnapmirror "FilerName" | Invoke-NaSnapmirrorQuiesce | Invoke-NaSnapmirrorBreak -Confirm:$false

When using iSCSI storage there are a few more steps required on the VM side as you can’t use one of the benefits of NFS in presenting a read only datastore to ESX hosts in your BR site and having your VMs pre-populated in the inventory and ready to just power on.

The manual steps would be:

  • Break the storage mirror.
  • Rescan the storage on the ESX(i) hosts in the recovery site to pick up the now-visible LUN(s).
  • Search the LUN(s) for all .vmx files and add them into the inventory.
  • Power on the VMs and answer the UUID question that they have moved.

Adding the .VMX files to the inventory has been updated with the new procedure I found in my previous post, Adding .VMX Files to vCenter Inventory with PowerCLI gets even easier and searching the datastores for the .VMX files has been updated with PowerCLI guru Luc Dekens’ new procedure as detailed in VMX Raiders Revisited.

Title:        VM-iSCSIBRv2.ps1
Description:  Connects to vCenter.
              Connects to Filer
              Breaks Snapmirror
              Rescans HBAs
              Browse datastore and all all .vmx files to inventory in a specified folder.
              Folder name must be unique in vCenter
              Power on VMs
              Answers UUID question to say VM was Moved.
              Can be Run as Scheduled Task

$Cluster = "NYC_PROD1"             # Business Recovery site Cluster "
$DatastoreNames = "nyc_lonvm1"     # Datastores to search to add .vmx files from
$VMFolder = "BR-LondonAppServers"  # Folder where BR VMs are to be placed - Must be unique in VC
$VCServer  = "vcenter.local"       # vCenter Server
$FilerName = "nycfiler.local"      # BR Filer Name
$FilerVol = "v_lon_brservers"      # BR Filer Volume

# Connect to VC
Connect-VIServer -Server $vcserver

# Connect to Filer
Connect-NaController $FilerName

# Break Storage Mirror
Get-NaSnapmirror $FilerVol | Invoke-NaSnapmirrorQuiesce | Invoke-NaSnapmirrorBreak -Confirm:$false

# Rescan all HBAs in BR site
Get-Cluster $Cluster | Get-VMHost | Get-VMHostStorage -RescanAllHba

# Get discovered datastores
$Datastores = get-datastore snap*$DatastoreNames*

$ESXHost = Get-Cluster $Cluster | Get-VMHost | select -First 1
$Folder = Get-Folder $VMFolder

foreach($Datastore in Get-Datastore $Datastores) {
    # Collect .vmx paths of registered VMs on the datastore
    $registered = @{}
    Get-VM -Datastore $Datastore | %{$_.Extensiondata.LayoutEx.File | where {$_.Name -like "*.vmx"} | %{$registered.Add($_.Name,$true)}}

   # Set up Search for .VMX Files in Datastore
    New-PSDrive -Name TgtDS -Location $Datastore -PSProvider VimDatastore -Root '\' | Out-Null
    $unregistered = @(Get-ChildItem -Path TgtDS: -Recurse | where {$_.FolderPath -notmatch ".snapshot" -and $_.Name -like "*.vmx" -and !$registered.ContainsKey($_.Name)})
    Remove-PSDrive -Name TgtDS

   #Register all .vmx Files as VMs on the datastore
   foreach($VMXFile in $unregistered) {
      New-VM -VMFilePath $VMXFile.DatastoreFullPath -VMHost $ESXHost -Location $VMFolder -RunAsync

# All VMs are now added to folder
$VMs = Get-VM -Location $Folder

# Disconnect Networking FOR TESTING ONLY
#$vms | get-networkadapter | Set-NetworkAdapter -StartConnected:$false -Confirm:$false

# Power on VMs even with questions pending
$vms | Start-VM -RunAsync

# Answer Question to Keep UUID in Loop until Question is answered
$qMsgUuidMoved = "msg.uuid.altered:"
$choiceMove = 2

ForEach ($vm in $vms)
      $vmview = $vm | Get-View
      if($vmview.Runtime.Question -ne $null -and $vmview.Runtime.Question.Text.StartsWith($qMsgUuidMoved))
         $vmview.AnswerVM($vmview.Runtime.Question.Id, $choiceMove)
   until ($vmview.Runtime.PowerState -ne "poweredoff")

# Disconnect from VC Server
Disconnect-VIServer -Confirm:$False
  1. Tolga
    December 8th, 2011 at 18:31 | #1

    first of all thanks this great script,
    what i want to ask is how can we add multiple luns from 2 controllers to break snapmirror and make the script search for multiple datastores and how is rdm handled?

    Many thanks again

  2. Tiho
    September 3rd, 2015 at 14:15 | #2

    Can you please explain why did you choose -Confirm:$false in
    Get-NaSnapmirror $FilerVol | Invoke-NaSnapmirrorQuiesce | Invoke-NaSnapmirrorBreak -Confirm:$false
    (from http://www.wooditwork.com/2011/09/14/srm-for-free-with-powercli-dataontap%E2%80%93-part-2-iscsi/)

    Particularly if you have more than one volume in the same script. Wouldn’t be better option to have -Confirm:$true
    If Quiesce is not completed Break will not work and script will error.
    Thank you for your time – great help – much appreciated

  1. No trackbacks yet.
Comments are closed.