-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCody.PureStorage.FlashArray.VMware.psm1
More file actions
2739 lines (2579 loc) · 107 KB
/
Cody.PureStorage.FlashArray.VMware.psm1
File metadata and controls
2739 lines (2579 loc) · 107 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function get-faVolumeNameFromVvolUuid{
<#
.SYNOPSIS
Connects to vCenter and FlashArray to return the FA volume that is a VVol virtual disk.
.DESCRIPTION
Takes in a VVol UUID to identify what volume it is on the FlashArray. If a VVol UUID is not specified it will ask you for a VM and then a VMDK and will find the UUID for you.
.INPUTS
FlashArray connection and VVol UUID.
.OUTPUTS
Returns volume name.
.NOTES
Version: 1.1
Author: Cody Hosterman https://codyhosterman.com
Creation Date: 01/31/2019
Purpose/Change: Initial script development
*******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.
************************************************************************
#>
#Import PowerCLI. Requires PowerCLI version 6.3 or later. Will fail here if PowerCLI cannot be installed
#Will try to install PowerCLI with PowerShellGet if PowerCLI is not present.
[CmdletBinding()]
Param(
[Parameter(Position=0)]
[string]$purevip,
[Parameter(Position=1,ValueFromPipeline=$True)]
[Microsoft.PowerShell.Commands.WebRequestSession]$faSession,
[Parameter(Position=2,mandatory=$true)]
[string]$vvolUUID,
[Parameter(Position=3,ValueFromPipeline=$True)]
[PurePowerShell.PureArray]$flasharray
)
if ($vvolUUID -eq "")
{
throw "You must enter a VVol UUID"
}
if ($purevip -ne "")
{
Write-Warning -Message "purevip will be deprecated in a future version. Please use new-pfaarray and pass in only the flasharray parameter."
}
if ($null -ne $faSession)
{
Write-Warning -Message "faSession will be deprecated in a future version. Please use new-pfaarray and pass in only the flasharray parameter."
}
if (($null -eq $flasharray) -and ($purevip -eq ""))
{
throw "Please use new-pfaarray and pass in a FlashArray connection."
}
$ErrorActionPreference = "stop"
if ($null -eq $faSession)
{
if ($null -eq $flasharray)
{
if ($global:faRestSession -ne "")
{
$faSession = $global:faRestSession
}
else {
throw "Please pass in a FlashArray connection using new-pfaarray"
}
}
else {
$faSession = new-pureflasharrayRestSession -flasharray $flasharray
$purevip = $flasharray.EndPoint
}
}
else {
if ($null -ne $flasharray)
{
$purevip = $flasharray.EndPoint
}
}
#Pull tags that match the volume with that UUID
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
$volumeTags = Invoke-RestMethod -Method Get -Uri "https://$($purevip)/api/1.14/volume?tags=true&filter=value='${vvolUUID}'" -WebSession $faSession -ErrorAction Stop
$volumeName = $volumeTags |where-object {$_.key -eq "PURE_VVOL_ID"}
if ($null -ne $volumeName)
{
write-host
return $volumeName.name
}
else {
throw "The VVol was not found on this FlashArray"
}
}
function new-pureflasharrayRestSession {
<#
.SYNOPSIS
Connects to FlashArray and creates a REST connection.
.DESCRIPTION
For operations that are in the FlashArray REST, but not in the Pure Storage PowerShell SDK yet, this provides a connection for invoke-restmethod to use.
.INPUTS
FlashArray connection or FlashArray IP/FQDN and credentials
.OUTPUTS
Returns REST session
.NOTES
Version: 1.1
Author: Cody Hosterman https://codyhosterman.com
Creation Date: 01/31/2019
Purpose/Change: Initial script development
*******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)]
[string]$purevip,
[Parameter(Position=1,ValueFromPipeline=$True)]
[System.Management.Automation.PSCredential]$faCreds,
[Parameter(Position=2,ValueFromPipeline=$True)]
[PurePowerShell.PureArray]$flasharray
)
if ($purevip -ne "")
{
Write-Warning -Message "purevip will be deprecated in a future version. Please pass in only the flasharray parameter."
}
if ($null -ne $faCreds)
{
Write-Warning -Message "faCreds will be deprecated in a future version. Please use new-pfaarray and pass in only the flasharray parameter."
}
if ($null -eq $flasharray)
{
if (($purevip -eq "") -or ($null -eq $faCreds))
{
throw "Please use new-pfaarray and pass in the flasharray parameter. "
}
}
#Connect to FlashArray
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
#Create FA session to obtain token
if ($null -eq $flasharray)
{
$flasharray = New-PfaArray -EndPoint $purevip -Credentials $faCreds -IgnoreCertificateError
}
#Create FA REST session
$SessionAction = @{
api_token = $flasharray.ApiToken
}
Invoke-RestMethod -Method Post -Uri "https://$($flasharray.Endpoint)/api/$($flasharray.apiversion)/auth/session" -Body $SessionAction -SessionVariable Session -ErrorAction Stop |Out-Null
$global:faRestSession = $Session
return $global:faRestSession
}
function remove-pureflasharrayRestSession {
<#
.SYNOPSIS
Disconnects a FlashArray REST session
.DESCRIPTION
Takes in a FlashArray Connection or session and disconnects on the FlashArray.
.INPUTS
FlashArray connection or session
.OUTPUTS
Returns success or failure.
.NOTES
Version: 1.1
Author: Cody Hosterman https://codyhosterman.com
Creation Date: 01/31/2019
Purpose/Change: Initial script development
*******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)]
[string]$purevip,
[Parameter(Position=1,ValueFromPipeline=$True)]
[Microsoft.PowerShell.Commands.WebRequestSession]$faSession,
[Parameter(Position=2,ValueFromPipeline=$True)]
[PurePowerShell.PureArray]$flasharray
)
if ($purevip -ne "")
{
Write-Warning -Message "purevip will be deprecated in a future version. Please pass in only the flasharray parameter."
}
if (($purevip -eq "") -and ($null -eq $flasharray))
{
throw "Please pass in the flasharray parameter."
}
if ($null -ne $flasharray)
{
$purevip = $flasharray.endpoint
$apiVersion = $flasharray.ApiVersion
}
else {
$apiVersion = 1.12
}
if ($null -eq $faSession)
{
$faSession = $global:faRestSession
if ($null -eq $faSession)
{
throw "No REST session found. You must enter in a REST session to disconnect."
}
}
#Delete FA session
Invoke-RestMethod -Method Delete -Uri "https://${purevip}/api/${apiVersion}/auth/session" -WebSession $faSession -ErrorAction Stop |Out-Null
}
function get-vmdkFromWindowsDisk {
<#
.SYNOPSIS
Returns the VM disk object that corresponds to a given Windows file system
.DESCRIPTION
Takes in a drive letter and a VM object and returns a matching VMDK object
.INPUTS
VM, Drive Letter
.OUTPUTS
Returns VMDK object
.NOTES
Version: 1.0
Author: Cody Hosterman https://codyhosterman.com
Creation Date: 08/24/2018
Purpose/Change: Initial script development
*******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,mandatory=$false,ValueFromPipeline=$True)]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine]$vm,
[Parameter(Position=1,mandatory=$false)]
[string]$driveLetter
)
if ($null -eq $global:defaultviserver)
{
throw "There is no PowerCLI connection to a vCenter, please connect first with connect-viserver."
}
if ($null -eq $vm)
{
try {
$vmName = Read-Host "Please enter in the name of your VM"
$vm = get-vm -name $vmName -ErrorAction Stop
}
catch {
throw $Global:Error[0]
}
}
try {
$guest = $vm |Get-VMGuest
}
catch {
throw $Error[0]
}
if ($guest.State -ne "running")
{
throw "This VM does not have VM tools running"
}
if ($guest.GuestFamily -ne "windowsGuest")
{
throw "This is not a Windows VM--it is $($guest.OSFullName)"
}
try {
$advSetting = Get-AdvancedSetting -Entity $vm -Name Disk.EnableUUID -ErrorAction Stop
}
catch {
throw $Error[0]
}
if ($advSetting.value -eq "FALSE")
{
throw "The VM $($vm.name) has the advanced setting Disk.EnableUUID set to FALSE. This must be set to TRUE for this cmdlet to work."
}
if (($null -eq $driveLetter) -or ($driveLetter -eq ""))
{
try {
$driveLetter = Read-Host "Please enter in a drive letter"
if (($null -eq $driveLetter) -or ($driveLetter -eq ""))
{
throw "No drive letter entered"
}
}
catch {
throw $Global:Error[0]
}
}
try {
$VMdiskSerialNumber = $vm |Invoke-VMScript -ScriptText "get-partition -driveletter $($driveLetter) | get-disk | ConvertTo-CSV -NoTypeInformation" -WarningAction silentlyContinue -ErrorAction Stop |ConvertFrom-Csv
}
catch {
throw $Error[0]
}
if ()
{
throw ($VMdiskSerialNumber |Out-String)
}
try {
$vmDisk = $vm | Get-HardDisk |Where-Object {$_.ExtensionData.backing.uuid.replace("-","") -eq $VMdiskSerialNumber.SerialNumber}
}
catch {
throw $Global:Error[0]
}
if ($null -ne $vmDisk)
{
return $vmDisk
}
else {
throw "Could not match the VM disk to a VMware virtual disk"
}
}
function new-faHostFromVmHost {
<#
.SYNOPSIS
Create a FlashArray host from an ESXi vmhost object
.DESCRIPTION
Takes in a vCenter ESXi host and creates a FlashArray host
.INPUTS
FlashArray connection, a vCenter ESXi vmHost, and iSCSI/FC option
.OUTPUTS
Returns new FlashArray host object.
.NOTES
Version: 1.0
Author: Cody Hosterman https://codyhosterman.com
Creation Date: 09/09/2018
Purpose/Change: Initial script development
*******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,mandatory=$true,ValueFromPipeline=$True)]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost]$esxi,
[Parameter(Position=1,mandatory=$true)]
[string]$protocolType,
[Parameter(Position=2,mandatory=$true,ValueFromPipeline=$True)]
[PurePowerShell.PureArray]$flasharray
)
if (($protocolType -ne "FC") -and ($protocolType -ne "iSCSI"))
{
throw 'No valid protocol entered. Please make sure $protocolType is set to either "FC" or "iSCSI"'
}
if ($null -eq $global:defaultviserver)
{
throw "There is no PowerCLI connection to a vCenter, please connect first with connect-viserver."
}
if ($protocolType -eq "iSCSI")
{
$iscsiadapter = $esxi | Get-VMHostHBA -Type iscsi | Where-Object {$_.Model -eq "iSCSI Software Adapter"}
if ($null -eq $iscsiadapter)
{
throw "No Software iSCSI adapter found on host $($esxi.NetworkInfo.HostName)."
}
else
{
$iqn = $iscsiadapter.ExtensionData.IScsiName
}
try
{
$newFaHost = New-PfaHost -Array $flasharray -Name $esxi.NetworkInfo.HostName -IqnList $iqn -ErrorAction stop
return $newFaHost
}
catch
{
Write-Error $Global:Error[0]
return
}
}
if ($protocolType -eq "FC")
{
$wwns = $esxi | Get-VMHostHBA -Type FibreChannel | Select-Object VMHost,Device,@{N="WWN";E={"{0:X}" -f $_.PortWorldWideName}} | Format-table -Property WWN -HideTableHeaders |out-string
$wwns = (($wwns.Replace("`n","")).Replace("`r","")).Replace(" ","")
$wwns = &{for ($i = 0;$i -lt $wwns.length;$i += 16)
{
$wwns.substring($i,16)
}}
try
{
$newFaHost = New-PfaHost -Array $flasharray -Name $esxi.NetworkInfo.HostName -WwnList $wwns -ErrorAction stop
return $newFaHost
}
catch
{
Write-Error $Global:Error[0]
return
}
}
}
function get-faHostFromVmHost {
<#
.SYNOPSIS
Gets a FlashArray host object from a ESXi vmhost object
.DESCRIPTION
Takes in a vmhost and returns a matching FA host if found
.INPUTS
FlashArray connection and a vCenter ESXi host
.OUTPUTS
Returns FA host if matching one is found.
.NOTES
Version: 1.0
Author: Cody Hosterman https://codyhosterman.com
Creation Date: 09/09/2018
Purpose/Change: Initial script development
*******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,mandatory=$true,ValueFromPipeline=$True)]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost]$esxi,
[Parameter(Position=1,mandatory=$true,ValueFromPipeline=$True)]
[PurePowerShell.PureArray]$flasharray
)
if ($null -eq $global:defaultviserver)
{
throw "There is no PowerCLI connection to a vCenter, please connect first with connect-viserver."
}
$iscsiadapter = $esxi | Get-VMHostHBA -Type iscsi | Where-Object {$_.Model -eq "iSCSI Software Adapter"}
$wwns = $esxi | Get-VMHostHBA -Type FibreChannel | Select-Object VMHost,Device,@{N="WWN";E={"{0:X}" -f $_.PortWorldWideName}} | Format-table -Property WWN -HideTableHeaders |out-string
$wwns = (($wwns.Replace("`n","")).Replace("`r","")).Replace(" ","")
$wwns = &{for ($i = 0;$i -lt $wwns.length;$i += 16)
{
$wwns.substring($i,16)
}}
$fahosts = Get-PFAHosts -array $flasharray -ErrorAction Stop
if ($null -ne $iscsiadapter)
{
$iqn = $iscsiadapter.ExtensionData.IScsiName
foreach ($fahost in $fahosts)
{
if ($fahost.iqn.count -ge 1)
{
foreach ($fahostiqn in $fahost.iqn)
{
if ($iqn.ToLower() -eq $fahostiqn.ToLower())
{
return $fahost
}
}
}
}
}
if ($null -ne $wwns)
{
foreach ($wwn in $wwns)
{
foreach ($fahost in $fahosts)
{
if ($fahost.wwn.count -ge 1)
{
foreach($fahostwwn in $fahost.wwn)
{
if ($wwn.ToLower() -eq $fahostwwn.ToLower())
{
return $fahost
}
}
}
}
}
}
else {
throw "No matching host could be found on the FlashArray"
}
}
function get-faHostGroupfromVcCluster {
<#
.SYNOPSIS
Retrieves a FA host group from an ESXi cluster
.DESCRIPTION
Takes in a vCenter Cluster and retrieves corresonding host group
.INPUTS
FlashArray connection and a vCenter cluster
.OUTPUTS
Returns success or failure.
.NOTES
Version: 1.0
Author: Cody Hosterman https://codyhosterman.com
Creation Date: 09/09/2018
Purpose/Change: Initial script development
*******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,mandatory=$true,ValueFromPipeline=$True)]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.Cluster]$cluster,
[Parameter(Position=1,mandatory=$true,ValueFromPipeline=$True)]
[PurePowerShell.PureArray]$flasharray
)
if ($null -eq $global:defaultviserver)
{
throw "There is no PowerCLI connection to a vCenter, please connect first with connect-viserver."
}
$esxiHosts = $cluster |Get-VMHost
$faHostGroups = @()
$faHostGroupNames = @()
foreach ($esxiHost in $esxiHosts)
{
try {
$faHost = $esxiHost | get-faHostFromVmHost -flasharray $flasharray
if ($null -ne $faHost.hgroup)
{
if ($faHostGroupNames.contains($faHost.hgroup))
{
continue
}
else {
$faHostGroupNames += $faHost.hgroup
$faHostGroup = Get-PfaHostGroup -Array $flasharray -Name $faHost.hgroup
$faHostGroups += $faHostGroup
}
}
}
catch{
continue
}
}
if ($null -eq $faHostGroup)
{
throw "No host group found for this cluster"
}
if ($faHostGroups.count -gt 1)
{
Write-Warning -Message "This cluster spans more than one host group. The recommendation is to have only one host group per cluster"
}
return $faHostGroups
}
function new-faHostGroupfromVcCluster {
<#
.SYNOPSIS
Create a host group from an ESXi cluster
.DESCRIPTION
Takes in a vCenter Cluster and creates hosts (if needed) and host group
.INPUTS
FlashArray connection, a vCenter cluster, and iSCSI/FC option
.OUTPUTS
Returns success or failure.
.NOTES
Version: 1.0
Author: Cody Hosterman https://codyhosterman.com
Creation Date: 08/06/2018
Purpose/Change: Initial script development
*******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,mandatory=$true,ValueFromPipeline=$True)]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.Cluster]$cluster,
[Parameter(Position=1,mandatory=$true)]
[string]$protocolType,
[Parameter(Position=2,mandatory=$true,ValueFromPipeline=$True)]
[PurePowerShell.PureArray]$flasharray
)
if ($null -eq $global:defaultviserver)
{
throw "There is no PowerCLI connection to a vCenter, please connect first with connect-viserver."
}
if (($protocolType -ne "FC") -and ($protocolType -ne "iSCSI"))
{
throw 'No valid protocol entered. Please make sure $protocolType is set to either "FC" or "iSCSI"'
}
$hostGroup = $cluster |get-faHostGroupfromVcCluster -flasharray $flasharray -ErrorAction SilentlyContinue
if ($hostGroup.count -gt 1)
{
throw "The cluster already is configured on the FlashArray and spans more than one host group. This cmdlet does not support a multi-hostgroup configuration."
}
if ($hostGroup.count -eq 1)
{
$clustername = $hostGroup.name
}
$esxiHosts = $cluster |Get-VMHost
$faHosts = @()
foreach ($esxiHost in $esxiHosts)
{
$faHost = $null
try {
$faHost = $esxiHost |get-faHostFromVmHost -flasharray $flasharray
}
catch {}
if ($null -eq $faHost)
{
try {
$faHost = $esxiHost |new-faHostFromVmHost -flasharray $flasharray -protocolType $protocolType -ErrorAction Stop
$faHosts += $faHost
}
catch {
Write-Error $Global:Error[0]
throw "Could not create host. Cannot create host group."
}
}
else {
$faHosts += $faHost
}
}
#FlashArray only supports Alphanumeric or the dash - character in host group names. Checking for VMware cluster name compliance and removing invalid characters.
if ($null -eq $hostGroup)
{
if ($cluster.Name -match "^[a-zA-Z0-9\-]+$")
{
$clustername = $cluster.Name
}
else
{
$clustername = $cluster.Name -replace "[^\w\-]", ""
$clustername = $clustername -replace "[_]", ""
$clustername = $clustername -replace " ", ""
}
$hg = Get-PfaHostGroup -Array $flasharray -Name $clustername -ErrorAction SilentlyContinue
if ($null -ne $hg)
{
if ($hg.hosts.count -ne 0)
{
#if host group name is already in use and has only unexpected hosts i will create a new one with a random number at the end
$nameRandom = get-random -Minimum 1000 -Maximum 9999
$hostGroup = New-PfaHostGroup -Array $flasharray -Name "$($clustername)-$($nameRandom)" -ErrorAction stop
$clustername = "$($clustername)-$($nameRandom)"
}
}
else {
#if there is no host group, it will be created
$hostGroup = New-PfaHostGroup -Array $flasharray -Name $clustername -ErrorAction stop
}
}
$faHostNames = @()
foreach ($faHost in $faHosts)
{
if ($null -eq $faHost.hgroup)
{
$faHostNames += $faHost.name
}
}
#any hosts that are not already in the host group will be added
Add-PfaHosts -Array $flasharray -Name $clustername -HostsToAdd $faHostNames -ErrorAction Stop |Out-Null
$fahostGroup = Get-PfaHostGroup -Array $flasharray -Name $clustername
return $fahostGroup
}
function set-vmHostPureFaiSCSI{
<#
.SYNOPSIS
Configure FlashArray iSCSI target information on ESXi host
.DESCRIPTION
Takes in an ESXi hosts and configures FlashArray iSCSI target info
.INPUTS
FlashArray connection and an ESXi host
.OUTPUTS
Returns ESXi iSCSI targets.
.NOTES
Version: 1.0
Author: Cody Hosterman https://codyhosterman.com
Creation Date: 09/09/2018
Purpose/Change: Initial script development
*******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,mandatory=$true,ValueFromPipeline=$True)]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost]$esxi,
[Parameter(Position=1,mandatory=$true,ValueFromPipeline=$True)]
[PurePowerShell.PureArray]$flasharray
)
if ($esxi.ExtensionData.Runtime.ConnectionState -ne "connected")
{
Write-Warning "Host $($esxi.NetworkInfo.HostName) is not in a connected state and cannot be configured."
return
}
$ESXitargets = @()
$faiSCSItargets = Get-PfaNetworkInterfaces -Array $flasharray |Where-Object {$_.services -eq "iscsi"}
if ($null -eq $faiSCSItargets)
{
throw "The target FlashArray does not currently have any iSCSI targets configured."
}
$iscsi = $esxi |Get-VMHostStorage
if ($iscsi.SoftwareIScsiEnabled -ne $true)
{
$esxi | get-vmhoststorage |Set-VMHostStorage -SoftwareIScsiEnabled $True |out-null
}
foreach ($faiSCSItarget in $faiSCSItargets)
{
$iscsiadapter = $esxi | Get-VMHostHba -Type iScsi | Where-Object {$_.Model -eq "iSCSI Software Adapter"}
if (!(Get-IScsiHbaTarget -IScsiHba $iscsiadapter -Type Send -ErrorAction stop | Where-Object {$_.Address -cmatch $faiSCSItarget.address}))
{
New-IScsiHbaTarget -IScsiHba $iscsiadapter -Address $faiSCSItarget.address -ErrorAction stop
}
$esxcli = $esxi |Get-esxcli -v2
$iscsiargs = $esxcli.iscsi.adapter.discovery.sendtarget.param.get.CreateArgs()
$iscsiargs.adapter = $iscsiadapter.Device
$iscsiargs.address = $faiSCSItarget.address
$delayedAck = $esxcli.iscsi.adapter.discovery.sendtarget.param.get.invoke($iscsiargs) |where-object {$_.name -eq "DelayedAck"}
$loginTimeout = $esxcli.iscsi.adapter.discovery.sendtarget.param.get.invoke($iscsiargs) |where-object {$_.name -eq "LoginTimeout"}
if ($delayedAck.Current -eq "true")
{
$iscsiargs = $esxcli.iscsi.adapter.discovery.sendtarget.param.set.CreateArgs()
$iscsiargs.adapter = $iscsiadapter.Device
$iscsiargs.address = $faiSCSItarget.address
$iscsiargs.value = "false"
$iscsiargs.key = "DelayedAck"
$esxcli.iscsi.adapter.discovery.sendtarget.param.set.invoke($iscsiargs) |out-null
}
if ($loginTimeout.Current -ne "30")
{
$iscsiargs = $esxcli.iscsi.adapter.discovery.sendtarget.param.set.CreateArgs()
$iscsiargs.adapter = $iscsiadapter.Device
$iscsiargs.address = $faiSCSItarget.address
$iscsiargs.value = "30"
$iscsiargs.key = "LoginTimeout"
$esxcli.iscsi.adapter.discovery.sendtarget.param.set.invoke($iscsiargs) |out-null
}
$ESXitargets += Get-IScsiHbaTarget -IScsiHba $iscsiadapter -Type Send -ErrorAction stop | Where-Object {$_.Address -cmatch $faiSCSItarget.address}
}
return $ESXitargets
}
function set-clusterPureFAiSCSI {
<#
.SYNOPSIS
Configure an ESXi cluster with FlashArray iSCSI information
.DESCRIPTION
Takes in a vCenter Cluster and configures iSCSI on each host.
.INPUTS
FlashArray connection and a vCenter cluster.
.OUTPUTS
Returns iSCSI targets.
.NOTES
Version: 1.0
Author: Cody Hosterman https://codyhosterman.com
Creation Date: 09/09/2018
Purpose/Change: Initial script development
*******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,mandatory=$true,ValueFromPipeline=$True)]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.Cluster]$cluster,
[Parameter(Position=1,mandatory=$true,ValueFromPipeline=$True)]
[PurePowerShell.PureArray]$flasharray
)
$esxihosts = $cluster |Get-VMHost
$esxiiSCSItargets = @()
$hostCount = 0
foreach ($esxihost in $esxihosts)
{
if ($hostCount -eq 0)
{
Write-Progress -Activity "Configuring iSCSI" -status "Host: $esxihost" -percentComplete 0
}
else {
Write-Progress -Activity "Configuring iSCSI" -status "Host: $esxihost" -percentComplete (($hostCount / $esxihosts.count) *100)
}
$esxiiSCSItargets += $esxihost | set-vmHostPureFaiSCSI -flasharray $flasharray
$hostCount++
}
return $esxiiSCSItargets
}
function get-faVolfromVMFS {
<#
.SYNOPSIS
Retrieves the FlashArray volume that hosts a VMFS datastore.
.DESCRIPTION
Takes in a VMFS datastore and one or more FlashArrays and returns the volume if found.
.INPUTS
FlashArray connection(s) and a VMFS datastore.
.OUTPUTS
Returns FlashArray volume or null if not found.
.NOTES
Version: 1.0
Author: Cody Hosterman https://codyhosterman.com
Creation Date: 09/10/2018
Purpose/Change: Initial script development
*******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,mandatory=$true,ValueFromPipeline=$True)]
[VMware.VimAutomation.ViCore.Types.V1.DatastoreManagement.Datastore]$datastore,
[Parameter(Position=1,mandatory=$true,ValueFromPipeline=$True)]
[PurePowerShell.PureArray]$flasharray
)
if ($datastore.Type -ne 'VMFS')
{
throw "This is not a VMFS datastore."
}
$lun = $datastore.ExtensionData.Info.Vmfs.Extent.DiskName |select-object -unique
if ($lun -like 'naa.624a9370*')
{
$pureVolumes = Get-PfaVolumes -Array $flasharray
$volserial = ($lun.ToUpper()).substring(12)
$purevol = $purevolumes | where-object { $_.serial -eq $volserial }
if ($null -ne $purevol.name)
{
return $purevol
}
else {
return $null
}
}
else {
throw "This VMFS is not hosted on FlashArray storage."
}
}
function new-faVolVmfs {
<#
.SYNOPSIS
Create a new VMFS on a new FlashArray volume
.DESCRIPTION
Creates a new FlashArray-based VMFS and presents it to a cluster.
.INPUTS
FlashArray connection, a vCenter cluster, a volume size, and name.
.OUTPUTS
Returns a VMFS object.
.NOTES
Version: 1.0
Author: Cody Hosterman https://codyhosterman.com
Creation Date: 09/10/2018
Purpose/Change: Initial script development
*******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,mandatory=$true,ValueFromPipeline=$True)]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.Cluster]$cluster,
[Parameter(Position=1,mandatory=$true,ValueFromPipeline=$True)]
[PurePowerShell.PureArray]$flasharray,
[Parameter(Position=2,mandatory=$true)]
[string]$volName,
[Parameter(Position=3)]
[int]$sizeInGB,
[Parameter(Position=4)]
[int]$sizeInTB
)
if (($sizeInGB -eq 0) -and ($sizeInTB -eq 0))
{
throw "Please enter a size in GB or TB"
}
elseif (($sizeInGB -ne 0) -and ($sizeInTB -ne 0)) {
throw "Please only enter a size in TB or GB, not both."
}
elseif ($sizeInGB -ne 0) {
$volSize = $sizeInGB * 1024 *1024 *1024
}
else {
$volSize = $sizeInTB * 1024 *1024 *1024 * 1024
}
try {
$hostGroup = $cluster | get-faHostGroupfromVcCluster -flasharray $flasharray -ErrorAction Stop
}
catch {
throw $Global:Error[0]
}
$newVol = New-PfaVolume -Array $flasharray -Size $volSize -VolumeName $volName -ErrorAction Stop
New-PfaHostGroupVolumeConnection -Array $flasharray -VolumeName $newVol.name -HostGroupName $hostGroup.name |Out-Null
$esxi = $cluster | get-vmhost | where-object {($_.version -like '5.5.*') -or ($_.version -like '6.*')}| where-object {($_.ConnectionState -eq 'Connected')} |Select-Object -last 1
$cluster| Get-VMHost | Get-VMHostStorage -RescanAllHba |Out-Null
$newNAA = "naa.624a9370" + $newVol.serial.toLower()
$ESXiApiVersion = $esxi.ExtensionData.Summary.Config.Product.ApiVersion
try {
if (($ESXiApiVersion -eq "5.5") -or ($ESXiApiVersion -eq "6.0") -or ($ESXiApiVersion -eq "5.1"))
{
$newVMFS = $esxi |new-datastore -name $newVol.name -vmfs -Path $newNAA -FileSystemVersion 5 -ErrorAction Stop
}
else
{
$newVMFS = $esxi |new-datastore -name $newVol.name -vmfs -Path $newNAA -FileSystemVersion 6 -ErrorAction Stop
}
return $newVMFS
}
catch {
Write-Error $Global:Error[0]
Remove-PfaHostGroupVolumeConnection -Array $flasharray -VolumeName $newVol.name -HostGroupName $hostGroup.name
Remove-PfaVolumeOrSnapshot -Array $flasharray -Name $newVol.name
Remove-PfaVolumeOrSnapshot -Array $flasharray -Name $newVol.name -Eradicate
return $null
}
}
function add-faVolVmfsToCluster {
<#
.SYNOPSIS
Add an existing FlashArray-based VMFS to another VMware cluster.