PureStorage.FlashArray.VMware.VVol.Replication.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
function Get-PfaVvolReplicationGroup {
    <#
    .SYNOPSIS
      Returns FlashArray Replication Groups
    .DESCRIPTION
      Takes in storage policy, a vVol datastore, a VM, or no inputs, and returns source and/or target replication groups.
    .INPUTS
      Storage Policy, vVol datastore, source or target
    .OUTPUTS
      Replication groups
    .NOTES
      Version: 1.0
      Author: Cody Hosterman https://codyhosterman.com
      Creation Date: 12/31/2020
      Purpose/Change: Function creation
    .EXAMPLE
      PS C:\ Get-PfaVvolReplicationGroup
 
      Returns all FlashArray vVol Replication Groups from all FlashArrays which have vVol datastores that are mounted in the connected vCenters
    .EXAMPLE
      PS C:\ $fd = Get-SpbmFaultDomain -Name flasharray-m50-1
      PS C:\ Get-PfaVvolReplicationGroup -faultDomain $fd
 
      Returns all FlashArray vVol Replication Groups from the specified fault domain (FlashArray) which has a vVol datastore that is mounted in the connected vCenters
    .EXAMPLE
      PS C:\ Get-PfaVvolReplicationGroup -source
 
      Returns all source FlashArray vVol Replication Groups from all FlashArrays which have vVol datastores that are mounted in the connected vCenters
    .EXAMPLE
      PS C:\ $fd = Get-SpbmFaultDomain -Name flasharray-m50-1
      PS C:\ Get-PfaVvolReplicationGroup -faultDomain $fd -source
 
      Returns all FlashArray vVol source Replication Groups from the specified fault domain (FlashArray) which has a vVol datastore that is mounted in the connected vCenters
    .EXAMPLE
      PS C:\ Get-PfaVvolReplicationGroup -target
 
      Returns all target FlashArray vVol Replication Groups from all FlashArrays which have vVol datastores that are mounted in the connected vCenters
    .EXAMPLE
      PS C:\ Get-PfaVvolReplicationGroup -testFailover
 
      Returns all FlashArray vVol Replication Groups that are in the middle of a test failover from all FlashArrays which have vVol datastores that are mounted in the connected vCenters
    .EXAMPLE
      PS C:\ Get-PfaVvolReplicationGroup -failedOver
 
      Returns all FlashArray vVol Replication Groups that have been failed over from all FlashArrays which have vVol datastores that are mounted in the connected vCenters
    .EXAMPLE
      PS C:\ Get-PfaVvolReplicationGroup -VM (get-vm vVolVM-01)
 
      Returns the FlashArray vVol replication group for the virtual machine named vVolVM-01
    .EXAMPLE
      PS C:\ Get-PfaVvolReplicationGroup -policy (Get-SpbmStoragePolicy vVolStoragePolicy)
 
      Returns all the FlashArray vVol replication groups that are valid for the storage policy from all FlashArrays which have vVol datastores that are mounted in the connected vCenters
     
    *******Disclaimer:******************************************************
    This scripts are offered "as is" with no warranty. While this
    scripts is tested and working in my environment, it is recommended that you test
    this script in a test lab before using in a production environment. Everyone can
    use the scripts/commands provided here without any written permission but I
    will not be liable for any damage or loss to the system.
    ************************************************************************
    #>


    [CmdletBinding(DefaultParameterSetName='DefaultSet')]
    Param(
            [Parameter(ParameterSetName='VM',Position=0,ValueFromPipeline=$True,mandatory=$true)]
            [ValidateScript({
                $ds = $_ |get-datastore |where-object {$_.Type -eq 'VVOL'} | where-object {$_.ExtensionData.Info.VvolDS.StorageArray[0].VendorId -eq "PURE"}
                if ($null -eq $ds)
                {
                    throw "This VM is not using a Pure Storage vVol datastore."
                }
                else {
                  $true
                }
              })]
            [VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine]$VM,

            [Parameter(ParameterSetName='Datastore',Position=1,ValueFromPipeline=$True)]
            [ValidateScript({
              if ($_.Type -ne 'VVOL')
              {
                  throw "The entered datastore is not a vVol datastore. It is type $($_.Type). Please only enter a vVol datastore"
              }
              elseif ($_.ExtensionData.Info.VvolDS.StorageArray[0].VendorId -ne "PURE") 
              {
                throw "This is not a Pure Storage vVol datastore"
              }
              else {
                $true
              }
            })]
            [VMware.VimAutomation.ViCore.Types.V1.DatastoreManagement.Datastore]$Datastore,

            [Parameter(ParameterSetName='Datastore',Position=2,ValueFromPipeline=$True,mandatory=$true)]
            [ValidateScript({
                $rules = $_.AnyOfRuleSets.Allofrules.capability |Where-Object {$_.Name -like "*com.purestorage.storage.replication*"}
                if ($null -eq $rules)
                {
                    throw "This is not a Pure Storage replication-based policy."
                }
                else {
                  $True
                }
            })]
            [VMware.VimAutomation.ViCore.Types.V1.Storage.StoragePolicy]$Policy,

            [Parameter(ParameterSetName='DefaultSet',Position=3)]
            [Switch]$source,

            [Parameter(ParameterSetName='DefaultSet',Position=4)]
            [Switch]$target,

            [Parameter(ParameterSetName='DefaultSet',Position=5)]
            [Switch]$testFailover,

            [Parameter(ParameterSetName='DefaultSet',Position=6)]
            [Switch]$failedOver,

            [Parameter(ParameterSetName='DefaultSet',Position=7)]
            [ValidateScript({
              if ($_.StorageArray.VendorId -ne "PURE")
              {
                  throw "This is not a Pure Storage fault domain (FlashArray)."
              }
              else {
                $True
              }
            })]
            [VMware.VimAutomation.Storage.Types.V1.Spbm.Replication.SpbmFaultDomain]$FaultDomain
    )
    if ($null -ne $datastore)
    {
      $pureReplicationGroups = Get-SpbmReplicationGroup -Datastore $datastore -StoragePolicy $policy
    }
    elseif ($null -ne $policy) {
      $pureReplicationGroups = Get-SpbmReplicationGroup -StoragePolicy $policy
    }
    elseif ($null -ne $vm) {
      $pureReplicationGroups = Get-SpbmReplicationGroup -VM $vm
    }
    else {
      if ($null -ne $faultDomain)
      {
        $pureReplicationGroups = Get-SpbmReplicationGroup -FaultDomain $faultDomain
      }
      else 
      {
        $vp = Get-VasaProvider |Where-Object {$_.Namespace -eq "com.purestorage"}
        $pureReplicationGroups = Get-SpbmReplicationGroup -VasaProvider $vp -ErrorAction SilentlyContinue
      }
      $groupfilter = @()
      if ($source -eq $True)
      {
        $groupfilter += "Source"
      }
      if ($target -eq $True)
      {
        $groupfilter += "Target"
      }
      if ($testFailover -eq $True)
      {
        $groupfilter += "InTest"
      }
      if ($failedOver -eq $True)
      {
        $groupfilter += "FailedOver"
      }
      if ($groupfilter.count -ge 1)
      {
        $pureReplicationGroups = $pureReplicationGroups  | Where-Object {$_}| Where-Object {$groupfilter.contains($_.State.ToString())}
      }
    }
    Write-Debug ($pureReplicationGroups |format-list * |Out-String)
    return $pureReplicationGroups
}
function Get-PfaVvolReplicationGroupPartner {
  <#
  .SYNOPSIS
    Returns any partner FlashArray Replication Groups for a specified group.
  .DESCRIPTION
    Takes in a replication group and some optional filters
  .INPUTS
    Storage Policy, vVol datastore, source or target
  .OUTPUTS
    Replication groups
  .NOTES
    Version: 1.0
    Author: Cody Hosterman https://codyhosterman.com
    Creation Date: 05/14/2020
    Purpose/Change: Function creation
  .EXAMPLE
    PS C:\ $vm = get-vm srmvm
    PS C:\ $group = $vm |Get-PfaVvolReplicationGroup
    PS C:\ Get-PfaVvolReplicationGroupPartner -replicationGroup $group
     
    Finds the replication group a VM's storage is assigned to and returns the target replication group(s).
  .EXAMPLE
    PS C:\ $vm = get-vm srmvm
    PS C:\ $group = $vm |Get-PfaVvolReplicationGroup
    PS C:\ $fd = Get-SpbmFaultDomain -Name flasharray-m50-1
    PS C:\ Get-PfaVvolReplicationGroupPartner -replicationGroup $group -faultDomain $fd
     
    Finds the replication group a VM's storage is assigned to and returns the target replication group for the specified fault domain (FlashArray).
  .EXAMPLE
    PS C:\ $fd = Get-SpbmFaultDomain -Name flasharray-m50-1
    PS C:\ $targetGroup = Get-PfaVvolReplicationGroup -target -faultDomain $fd
    PS C:\ Get-PfaVvolReplicationGroupPartner -replicationGroup $targetGroup
     
    Finds the source replication group of the specified target replication group.
 
    *******Disclaimer:******************************************************
    This scripts are offered "as is" with no warranty. While this
    scripts is tested and working in my environment, it is recommended that you test
    this script in a test lab before using in a production environment. Everyone can
    use the scripts/commands provided here without any written permission but I
    will not be liable for any damage or loss to the system.
    ************************************************************************
    #>


  [CmdletBinding()]
  Param(
          [Parameter(Position=0,ValueFromPipeline=$True,mandatory=$true)]
          [VMware.VimAutomation.ViCore.Types.V1.Storage.ReplicationGroup]$replicationGroup,
          
          [Parameter(Position=2,ValueFromPipeline=$True)]
          [ValidateScript({
            if ($_.StorageArray.VendorId -ne "PURE")
            {
                throw "This is not a Pure Storage fault domain (FlashArray)."
            }
            else {
              $True
            }
          })]
          [VMware.VimAutomation.Storage.Types.V1.Spbm.Replication.SpbmFaultDomain]$FaultDomain
  )
  if ($replicationGroup.state.ToString() -eq "Source")
  {
    $pureReplicationGroups = Get-PfaVvolReplicationGroup -testFailover -target -failedOver
    $groupID = $replicationGroup.ExtensionData.groupId
    $groupPartners = @()
    foreach ($pureReplicationGroup in $pureReplicationGroups)
    {
      $partnerGroupID = $pureReplicationGroup.ExtensionData.sourceInfo.sourceGroupId
      if ($partnerGroupID.deviceGroupId.id -eq $groupID.deviceGroupId.id)
      {
        if ($partnerGroupID.faultDomainId.id -eq $groupID.faultDomainId.id)
        {
          if (($null -ne $faultDomain) -and ($pureReplicationGroup.faultDomain.id -eq $faultDomain.Id))
          {
            write-host "fart"
            $groupPartners += $pureReplicationGroup
          }
          if ($null -eq $faultDomain)
          {
            $groupPartners += $pureReplicationGroup
          }
        }
      }
    }
    return $groupPartners
  }
  else{
    $pureReplicationGroups = Get-PfaVvolReplicationGroup -source
    $groupID = $replicationGroup.ExtensionData.sourceInfo.sourceGroupId
    foreach ($pureReplicationGroup in $pureReplicationGroups)
    {
      $partnerGroupID = $pureReplicationGroup.ExtensionData.groupId
      if ($partnerGroupID.deviceGroupId.id -eq $groupID.deviceGroupId.id)
      {
        if ($partnerGroupID.faultDomainId.id -eq $groupID.faultDomainId.id)
        {
          if (($null -ne $faultDomain) -and ($pureReplicationGroup.faultDomain.id -ne $faultDomain.Id))
          {
            throw "The source replication group does not exist on the specified Fault Domain: $($faultDomain.name) ($($faultDomain.id)). It resides on a FlashArray with ID of $($pureReplicationGroup.faultDomain.id)"
          }
          return $pureReplicationGroup
        }
      }
    }
  }
}
function Get-PfaVvolFaultDomain {
  <#
  .SYNOPSIS
    Returns all or specified FlashArray fault domains
  .DESCRIPTION
    Takes in a name, serial number, connection or nothing and returns the corresponding FlashArray fault domains
  .INPUTS
    Takes in a name, serial number, connection or nothing.
  .OUTPUTS
    Fault domains
  .NOTES
    Version: 1.0
    Author: Cody Hosterman https://codyhosterman.com
    Creation Date: 12/31/2020
    Purpose/Change: Function creation
  .EXAMPLE
    PS C:\ Get-PfaVvolFaultDomain
 
    Returns all Pure Storage FlashArray fault domains
  .EXAMPLE
    PS C:\ Get-PfaVvolFaultDomain -ArraySerial 7e914d96-c90a-31e0-a495-75e8b3c300cc
 
    Returns the FlashArray fault domain for the specified array serial number.
  .EXAMPLE
    PS C:\ Get-PfaVvolFaultDomain -ArrayName flasharray-m50-1
 
    Returns the FlashArray fault domain for the specified array name.
  .EXAMPLE
    PS C:\ $fa = new-pfaConnection -endpoint flasharray-m50-1 -ignoreCertificateError -DefaultArray
    PS C:\ Get-PfaVvolFaultDomain -FlashArray $fa
 
    Returns the FlashArray fault domain for the specified FlashArray connection.
 
    *******Disclaimer:******************************************************
    This scripts are offered "as is" with no warranty. While this
    scripts is tested and working in my environment, it is recommended that you test
    this script in a test lab before using in a production environment. Everyone can
    use the scripts/commands provided here without any written permission but I
    will not be liable for any damage or loss to the system.
    ************************************************************************
    #>


  [CmdletBinding(DefaultParameterSetName='Name')]
  Param(
        [Parameter(Position=0,ValueFromPipeline=$True,ParameterSetName='Connection')]
        [PurePowerShell.PureArray]$Flasharray,

        [Parameter(Position=1,ParameterSetName='Name')]
        [string]$ArrayName,

        [Parameter(Position=2,ParameterSetName='Serial')]
        [string]$ArraySerial
  )
    if (![string]::IsNullOrEmpty($arrayName))
    {
      $faultDomain = Get-SpbmFaultDomain -Name $arrayName -ErrorAction Stop
    }
    elseif (![string]::IsNullOrEmpty($arraySerial))
    {
      $faultDomain = Get-SpbmFaultDomain | Where-Object {$_.StorageArray.Id -eq "com.purestorage:$($arraySerial)"} 
      if ($null -eq $faultDomain)
      {
        throw "Could not find a fault domain for specified serial number: $($arraySerial)"
      }
    }
    elseif ($null -ne $flasharray) 
    {
      $arraySerial = (Get-PfaArrayAttributes -array $flasharray).id
      $faultDomain = Get-SpbmFaultDomain -ErrorAction Stop | Where-Object {$_.StorageArray.Id -eq "com.purestorage:$($arraySerial)"} 
      if ($null -eq $faultDomain)
      {
        throw "Could not find a fault domain for specified FlashArray with the serial number: $($arraySerial)"
      }
    }
    else {
      $faultDomain = Get-SpbmFaultDomain | Where-Object {$_.StorageArray.VendorId -eq "PURE"}
    }
    return $faultDomain
}