PureStorage.CBS.AVS.Replication.ps1

function Start-PCBSVvolReplicationGroupFailover {
  <#
  .SYNOPSIS
    Issue test failover or failover operation against group
  .DESCRIPTION
    Issue test failover or failover operation against group
  .INPUTS
    replication group ID, cluster name, power on (boolean), failover (boolean)
  .OUTPUTS
    N.A.
  .NOTES
    Version: 1.0
  .EXAMPLE
    Start-PCBSVvolReplicationGroupFailover -ReplicationGroupID myGroupId -ClusterName myclustername
    #>


  [CmdletBinding()]
  Param(
    [Parameter(Position=0,mandatory=$true)]
    [String]$ReplicationGroupID,
    [Parameter(Position=1,mandatory=$true)]
    [String]$ClusterName,
    [Parameter(Position=2,mandatory=$false)]
    [bool]$PowerOn,
    [Parameter(Position=3,mandatory=$false)]
    [bool]$Failover
  )


  ## check that the cluster exists ##
  $cluster = Get-Cluster -Name $ClusterName -ErrorAction Stop

  #creates groupid tag to tag new VMs
  $cat = Get-TagCategory -Name "PCBS-Rep-Group-ID" -ErrorAction SilentlyContinue
  if (-not $cat) {
    $cat = New-TagCategory -Name "PCBS-Rep-Group-ID" -EntityType "VirtualMachine"
  }
  $tag = Get-Tag -Name $ReplicationGroupID -Category $cat -ErrorAction SilentlyContinue
  if (-not $tag) {
    $tag = New-Tag -Name $ReplicationGroupID -Category $cat
  }

  ## Setting the Replication Group Variable ##
  $repGroup = Get-SpbmReplicationGroup -ID $ReplicationGroupID

  ## Failing over the replication group for the VMs and setting the operations to a variable ##
  if($Failover) {
    # does actual failover, rep group status become FailOver
    $testVms = Start-SpbmReplicationFailover -ReplicationGroup $repGroup -Confirm:$false
  }
  else {
    # does test failover, rep group status become InTest
    $testVms = Start-SpbmReplicationTestFailover -ReplicationGroup $repGroup
  }

  $VMNames = @()
  foreach ($vm in $testVms) {
    # create VMs on the failover vcenter
    $newvm = New-VM -VMFilePath $vm -ResourcePool $ClusterName
    # assign rep group tag to new vm
    New-TagAssignment -Tag $tag -Entity $newvm | Out-Null

    $VMNames += $newvm.Name
    ## Starting the VM if PowerOn param is true ##
    if($PowerOn) {
      $newvm | Start-VM -ErrorAction SilentlyContinue -ErrorVariable hasQuestion | out-null
      if ($hasQuestion) {
        ## there is a question that is asked if the VM is copied or moved. This question must be answered ##
        $newvm | Get-VMQuestion | Set-VMQuestion -Option '*copied*' -Confirm:$false
      }
    }
  }
}

function Start-PCBSVvolCleanupSourceReplicationGroupForFailover {
  [CmdletBinding()]
  Param(
    [Parameter(Position=0,mandatory=$true)]
    [String]$ReplicationGroupID,
    [Parameter(Position=1,mandatory=$false)]
    [bool]$RemoveFromDisk
  )

  ## Setting the Replication Group Variable ##
  $repGroup = Get-SpbmReplicationGroup -ID $ReplicationGroupID

  ## Get VMs related to replication Group ##
  $relatedVMs = Get-VM -RelatedObject $repGroup

  foreach ($vm in $relatedVMs) {
    # stop VMs
    # if we fail to stop it's probably powered off already so we can ignore, if it's not it will fail on the next step
    $vm | Stop-VM -Confirm:$false -ErrorAction SilentlyContinue

    # unregister VMs
    # if VM exists but we cannot remove it, we should not continue
    if ($RemoveFromDisk) {
      Write-Verbose "Removing VM files from disk for VM $($vm.Name)"
      $vm | Remove-VM -Confirm:$false -DeletePermanently -ErrorAction Stop
    }
    else {
      Write-Verbose "Unregister VM $($vm.Name) from inventory"
      $vm | Remove-VM -Confirm:$false -ErrorAction Stop
    }
  }

  # remove tag category created on previous failovers
  $cat = Get-TagCategory -Name "PCBS-Rep-Group-ID" -ErrorAction SilentlyContinue
  if ($cat) {
    Remove-TagCategory -Category $cat -Confirm:$false
  }
}

function Remove-PCBSTags {
  [CmdletBinding()]
  Param(
    [Parameter(Position=0,mandatory=$true)]
    [String]$ReplicationGroupID
  )

  $cat = Get-TagCategory -Name "PCBS-Rep-Group-ID" -ErrorAction SilentlyContinue
  if ($cat) {
    $tag = Get-Tag -Name $ReplicationGroupID -Category $cat -ErrorAction SilentlyContinue
    if ($tag) {
      # cleanup tags created by previous failovers
      Remove-Tag -Tag $tag -Confirm:$false
    }
  }
}

function Get-VMsByPCBSTags {
  [CmdletBinding()]
  Param(
    [Parameter(Position=0,mandatory=$true)]
    [String]$ReplicationGroupID
  )

  $cat = Get-TagCategory -Name "PCBS-Rep-Group-ID" -ErrorAction SilentlyContinue
  $tag = Get-Tag -Name $ReplicationGroupID -Category $cat -ErrorAction SilentlyContinue
  # get vms list
  $vms = Get-VM -Tag $tag
  return $vms
}

function Stop-PCBSVvolReplicationGroupFailoverTest {
  <#
  .SYNOPSIS
    Stops test failover operation against group
  .DESCRIPTION
    Issue test failover operation against group
  .INPUTS
    replication group ID,
  .OUTPUTS
    N.A.
  .NOTES
    Version: 1.0
  .EXAMPLE
    Stop-PCBSVvolReplicationGroupFailoverTest -RepGroupID myGroupId
    #>


  [CmdletBinding()]
  Param(
    [Parameter(Position=0,mandatory=$true)]
    [String]$ReplicationGroupID
  )

  ## Setting the Replication Group Variable ##
  $repGroup = Get-SpbmReplicationGroup -ID $ReplicationGroupID

  # get vms list
  $vms = Get-VMsByPCBSTags -ReplicationGroupID $ReplicationGroupID

  foreach ($vm in $vms) {
    # stop VMs
    # if we fail to stop it's probably powered off already so we can ignore, if it's not it will fail on the next step
    $vm | Stop-VM -Confirm:$false -ErrorAction SilentlyContinue

    # unregister VMs
    # if VM exists but we cannot remove it, we should not continue
    $vm | Remove-VM -DeletePermanently:$true -Confirm:$false -ErrorAction Stop
  }
  Remove-PCBSTags -ReplicationGroupID $ReplicationGroupID

  $retryCount = 5
  $TimeoutInSecs = 15

  do {
    # wait sometimes for vms to cleanup
    Start-Sleep -Seconds $TimeoutInSecs

    # Issue test failover stop
    # can fail saying that resource is still in use, but works if we retry
    Stop-SpbmReplicationTestFailover -ReplicationGroup $repGroup.Name -ErrorAction SilentlyContinue -ErrorVariable stopError

    $retryCount--
  } while ($stopError -and $retryCount -gt 0)

  if ($stopError) {
    throw 'Stop Failed: ' + $stopError
  }
}

function Start-PCBSVvolReprotectReplicationGroup {
  [CmdletBinding()]
  Param(
    [Parameter(Position=0,mandatory=$true)]
    [String]$ReplicationGroupID,
    [Parameter(Position=2,mandatory=$false)]
    [String]$PolicyName
  )

  ## Setting the Replication Group Variable ##
  $failoverGroup = Get-SpbmReplicationGroup -ID $ReplicationGroupID

  ## Running the reverse replication group operation to reverse the source and target status for the Replication Group ##
  $new_source_group = Start-SpbmReplicationReverse -ReplicationGroup $failoverGroup.Name

  # get vms list
  $vms = Get-VMsByPCBSTags -ReplicationGroupID $ReplicationGroupID

  ## Setting the New VM to a variable on the recovery vCenter Server
  $relevantObjs = @()
  foreach ($newvm in $vms) {
    $relevantObjs +=$newvm
    $HD1 = $newvm | Get-HardDisk
    $relevantObjs += $HD1
  }

  ## get relevant SPBM-related configuration data of Virtual Machine, Hard Disk, and Datastore objects
  $spbmConfig = $relevantObjs | Get-SpbmEntityConfiguration

  ## Resetting the storage policy for the VM and each virtual disk to the "VVol No Requirements Policy" ##
  $noReqPolicy = Get-SpbmStoragePolicy -name 'VVol No Requirements Policy'
  $spbmConfig | Set-SpbmEntityConfiguration -StoragePolicy $noReqPolicy

  if ($PolicyName) {
    ## Setting the Variables for the replication group and storage policy that we want to use to re-protect the VM to the previous source/protected site ##
    $new_policy = Get-SpbmStoragePolicy -name $PolicyName
    $new_rg = $new_policy | Get-SpbmReplicationGroup -Name $new_source_group.Name

    ## Applying the Storage Policy and Replication group to the VMs to complete the Re-protect process ##
    $spbmConfig | Set-SpbmEntityConfiguration -StoragePolicy $new_policy -ReplicationGroup $new_rg
  }
  Remove-PCBSTags -ReplicationGroupID $ReplicationGroupID
}