-
Notifications
You must be signed in to change notification settings - Fork 7
/
VMware.CloudFoundation.CertificateManagement.psm1
2550 lines (2150 loc) · 138 KB
/
VMware.CloudFoundation.CertificateManagement.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
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
# Copyright 2023-2024 Broadcom. All Rights Reserved.
# SPDX-License-Identifier: BSD-2
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# Allow communication with self-signed certificates when using Powershell Core. If you require all communications to be
# secure and do not wish to allow communication with self-signed certificates, remove lines 19-39 before importing the
# module.
if ($PSEdition -eq 'Core') {
$PSDefaultParameterValues.Add("Invoke-RestMethod:SkipCertificateCheck", $true)
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | Out-Null
}
if ($PSEdition -eq 'Desktop') {
# Allow communication with self-signed certificates when using Windows PowerShell
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | Out-Null
if ("TrustAllCertificatePolicy" -as [type]) {} else {
Add-Type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertificatePolicy : ICertificatePolicy {
public TrustAllCertificatePolicy() {}
public bool CheckValidationResult(
ServicePoint sPoint, X509Certificate certificate,
WebRequest wRequest, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertificatePolicy
}
}
##########################################################################
#Region Non Exported Functions ######
Function Get-Password {
param (
[string]$user,
[string]$password
)
if ([string]::IsNullOrEmpty($password)) {
$secureString = Read-Host -Prompt "Enter the password for $user" -AsSecureString
$password = ConvertFrom-SecureString $secureString -AsPlainText
}
return $password
}
Function Get-VcenterService {
<#
.DESCRIPTION
The Get-VcenterService retrieves the service's current status and health from vCenter Server and returns with an
ordered hash object with the service name and health.
.EXAMPLE
Get-VcenterService -serviceName "certificateauthority"
This example retrieves the status and health of the vCenter Server service named "certificateauthority"
.PARAMETER serviceName
The name of the vCenter Server service.
#>
Param (
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $serviceName
)
$serviceExists = Invoke-GetService -Service $serviceName -ErrorAction SilentlyContinue
if ($serviceExists -eq $null) {
Write-Error "Service $serviceName does not exist." -ErrorAction Stop
Exit
} else {
return [ordered]@{
name = $serviceName
status = $serviceExists.state
health = $serviceExists.health
}
}
}
Function Restart-VcenterService {
<#
.DESCRIPTION
The Restart-VcenterService restart the vCenter Server service taken from parameter value and returns a hash object
with the service name, the service status, and the result from restart operation.
.EXAMPLE
Restart-VcenterService -serviceName "certificateauthority"
This example restart the vCenter server service named "certificateauthority"
.PARAMETER serviceName
The name of the vCenter Server service.
#>
param (
[string]$serviceName
)
Invoke-RestartService -Service $serviceName | Out-Null
for ($count = 0; $count -lt 16; $count++) {
$serviceStatus = Get-VcenterService -Service $serviceName
if ($serviceStatus.status -eq "STARTED" -and $serviceStatus.health -eq "HEALTHY") {
return [ordered]@{
name = $serviceName
status = "GREEN"
result = "Successfully restarted service."
}
} elseif ($serviceStatus.status -eq "STARTING" -or $serviceStatus.status -eq "STOPPING") {
# Service is still starting or stopped state; sleep for 20 seconds before retry.
Sleep-Time -Seconds 20
} elseif ($serviceStatus.status -eq "STARTED" -and $serviceStatus.health -eq "HEALTHY_WITH_WARNINGS") {
return [ordered]@{
name = $serviceName
status = "YELLOW"
result = "Issues restarting service: the health state is HEALTHY_WITH_WARNINGS."
}
} elseif (($serviceStatus.status -eq "STARTED") -and ($serviceStatus.health -eq "DEGRADED")) {
return [ordered]@{
name = $serviceName
status = "RED"
result = "Failed restarting service: the health state is DEGRADED."
}
} elseif ($serviceStatus.status -eq "STOPPED") {
return [ordered]@{
name = $serviceName
status = "RED"
result = "Failed restarting service: the service status is STOPPED."
}
} elseif ($count -gt 14) {
# Operation timed out
return [ordered]@{
name = $serviceName
status = "RED"
result = "Failed restarting service: unable to retrieve service status. Operation timed out."
}
}
}
}
#EndRegion Non Exported Functions ######
##########################################################################
#######################################################################################################################
##################################################### FUNCTIONS #####################################################
Function Get-vCenterServer {
<#
.SYNOPSIS
Retrieves the vCenter Server details and connection object from SDDC Manager using either a workload domain
name or ESXi host FQDN.
.DESCRIPTION
The Get-vCenterServer retrieves the vCenter Server details and connection object from SDDC Manager using either
a workload domain name or ESXi host FQDN.
The cmdlet connects to the SDDC Manager using the -server, -user, and -password values.
- Validates that network connectivity and authentication is possible to SDDC Manager.
- Validates that network connectivity and authentication is possible to vCenter Server.
- Validates that the workload domain exists in the SDDC Manager inventory.
- Connects to vCenter Server and returns its details and connection in a single object.
.EXAMPLE
Get-vCenterServer -server sfo-vcf01.sfo.rainpole.io -user [email protected] -pass VMw@re1! -esxiFqdn sfo01-m01-esx01.sfo.rainpole.io
This example retrieves the vCenter Server details and connection object to which the ESXi host with The fully qualified domain name of sfo01-m01-esx01.sfo.rainpole.io belongs.
.EXAMPLE
Get-vCenterServer -server sfo-vcf01.sfo.rainpole.io -user [email protected] -pass VMw@re1! -domain sfo-m01
This example retrieves the vCenter Server details and connection object belonging to the domain sfo-m01.
.PARAMETER server
The fully qualified domain name of the SDDC Manager appliance.
.PARAMETER user
The username to authenticate to the SDDC Manager instance.
.PARAMETER pass
The password to authenticate to the SDDC Manager instance.
.PARAMETER domain
The name of the workload domain to retrieve the vCenter Server details from SDDC Manager for the connection object.
.PARAMETER esxiFqdn
The fully qualified domain name of the ESXi host to validate against the SDDC Manager inventory.
#>
Param (
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $server,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $user,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $pass,
[Parameter (Mandatory = $true, ParameterSetName = "domain")] [String] $domain,
[Parameter (Mandatory = $true, ParameterSetName = "esxifqdn")] [String] $esxiFqdn
)
if (Test-VCFConnection -server $server) {
if (Test-VCFAuthentication -server $server -user $user -pass $pass) {
if ($PsBoundParameters.ContainsKey("domain")) {
$domain = $(Get-VCFWorkloadDomain | Where-Object { $_.name -eq $domain }).name
} else {
$esxiHost = Get-VCFHost -fqdn $esxiFqdn
if (!$esxiHost) {
Throw "ESXi host not found. Please check the provided FQDN: $esxiFqdn."
}
$domain = $(Get-VCFWorkloadDomain -id $($esxiHost.domain.id)).name
}
if ($vcfvCenterDetails = Get-vCenterServerDetail -server $server -user $user -pass $pass -domain $domain) {
if (Test-VsphereConnection -server $($vcfvCenterDetails.fqdn)) {
if ($connection = Connect-VIServer -server $vcfvCenterDetails.fqdn -user $vcfvCenterDetails.ssoAdmin -pass $vcfvCenterDetails.ssoAdminPass) {
$vcfvCenterServerObject = New-Object -TypeName psobject
$vcfvCenterServerObject | Add-Member -NotePropertyName 'details' -NotePropertyValue $vcfvCenterDetails
$vcfvCenterServerObject | Add-Member -NotePropertyName 'connection' -NotePropertyValue $connection
return $vcfvCenterServerObject
}
}
} else {
Throw "Unable to return vCenter Server details: PRE_VALIDATION_FAILED"
}
} else {
Throw "Unable to obtain access token from SDDC Manager ($server), check credentials: PRE_VALIDATION_FAILED"
}
} else {
Throw "Unable to connect to ($server): PRE_VALIDATION_FAILED"
}
}
Function Get-VCFCertificateThumbprint {
<#
.SYNOPSIS
Retrieves certificate thumbprints for ESXi hosts or vCenter Server instances.
.DESCRIPTION
The Get-VCFCertificateThumbprint cmdlet retrieves certificate thumbprints for ESXi hosts or vCenter Server
instances.
.EXAMPLE
Get-VCFCertificateThumbprint -esxi -server sfo-vcf01.sfo.rainpole.io -user [email protected] -pass VMw@re1! -esxiFqdn sfo01-m01-esx01.sfo.rainpole.io
This example retrieves the ESXi host's certificate thumbprint for an ESXi host with The fully qualified domain name of sfo01-m01-esx01.sfo.rainpole.io.
.EXAMPLE
Get-VCFCertificateThumbprint -vcenter -server sfo-vcf01.sfo.rainpole.io -user [email protected] -pass VMw@re1! -domain sfo-m01 -issuer rainpole
This example retrieves the vCenter Server instance's certificate thumbprints for the vCenter Server instance belonging to domain sfo-m01 and a matching issuer "rainpole".
.PARAMETER esxi
Switch to retrieve the certificate thumbprint for an ESXi host.
.PARAMETER vcenter
Switch to retrieve the certificate thumbprints for a vCenter Server instance.
.PARAMETER server
The fully qualified domain name of the SDDC Manager instance.
.PARAMETER user
The username to authenticate to the SDDC Manager instance.
.PARAMETER pass
The password to authenticate to the SDDC Manager instance.
.PARAMETER domain
The name of the workload domain (only required when using the "vCenter" parameter).
.PARAMETER issuer
The name of the issuer to match with the vCenter Server instance's certificate thumbprints (only required when using the "vCenter" parameter).
#>
Param (
[Parameter (Mandatory = $true, ParameterSetName = "ESXi")] [ValidateNotNullOrEmpty()] [Switch] $esxi,
[Parameter (Mandatory = $true, ParameterSetName = "vCenter")] [ValidateNotNullOrEmpty()] [Switch] $vcenter,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $server,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $user,
[Parameter (Mandatory = $false)] [ValidateNotNullOrEmpty()] [String] $pass,
[Parameter (Mandatory = $false, ParameterSetName = "ESXi")] [ValidateNotNullOrEmpty()] [String] $esxiFqdn,
[Parameter (Mandatory = $false, ParameterSetName = "vCenter")] [ValidateNotNullOrEmpty()] [String] $domain,
[Parameter (Mandatory = $false, ParameterSetName = "vCenter")] [ValidateNotNullOrEmpty()] [String] $issuer
)
$pass = Get-Password -User $user -Password $pass
Try {
if ($PsBoundParameters.ContainsKey("esxi")) {
$vCenterServer = Get-vCenterServer -server $server -user $user -pass $pass -esxiFqdn $esxiFqdn
$esxiCertificateThumbprint = $(Get-VIMachineCertificate -Server $($vCenterServer.details.fqdn) -VMHost $esxiFqdn).Certificate.Thumbprint
return $esxiCertificateThumbprint
} else {
$vCenterServer = Get-vCenterServer -server $server -user $user -pass $pass -domain $domain
$vcTrustedCert = Get-VITrustedCertificate -Server $vCenterServer.details.fqdn
if ($vcTrustedCert) {
if ($PsBoundParameters.ContainsKey("issuer")) {
$vcTrustedCert = $vcTrustedCert | Where-Object { $_.issuer -match $issuer }
}
$vcCertificateThumbprint = $vcTrustedCert.Certificate.Thumbprint
return $vcCertificateThumbprint
} else {
Write-Error "Unable to retrieve certificates from vCenter Server instance $($vCenterServer.details.fqdn)." -ErrorAction Stop
}
}
} Catch {
Debug-ExceptionWriter -object $_
} Finally {
if ($vCenterServer) { Disconnect-VIServer -server $vCenterServer.details.fqdn -Confirm:$false -WarningAction SilentlyContinue }
}
}
Function Test-EsxiCertMgmtChecks {
<#
.SYNOPSIS
Run the checks required for ESXi Certificate Management for a given cluster or an ESXi host.
.DESCRIPTION
The Test-EsxiCertMgmtChecks runs the checks required for ESXi Certificate Management for a given cluster or an
ESXi host. The following checks are run:
- Check ESXi Certificate Mode
- Check ESXi Lockdown Mode
- Confirm CA In vCenter Server
- Check vSAN Health Status
.EXAMPLE
Test-EsxiCertMgmtChecks -server sfo-vcf01.sfo.rainpole.io -user [email protected] -pass VMw@re1! -domain sfo-m01 -cluster sfo-m01-cl01 -issuer rainpole -signedCertificate F:\Certificates\Root64.cer
This example runs the checks required for ESXi Certificate Management for the cluster belonging to the domain sfo-m01.
.EXAMPLE
Test-EsxiCertMgmtChecks -server sfo-vcf01.sfo.rainpole.io -user [email protected] -pass VMw@re1! -domain sfo-m01 -cluster sfo-m01-cl01 -esxiFqdn sfo01-m01-esx01.sfo.rainpole.io -issuer rainpole -signedCertificate F:\Certificates\Root64.cer
This example runs the checks required for ESXi Certificate Management for an ESXi host belonging to the domain sfo-m01.
.PARAMETER server
The fully qualified domain name of the SDDC Manager instance.
.PARAMETER user
The username to authenticate to the SDDC Manager instance.
.PARAMETER pass
The password to authenticate to the SDDC Manager instance.
.PARAMETER domain
The name of the workload domain to retrieve the vCenter Server instance's certificate thumbprints from.
.PARAMETER cluster
The name of the cluster in which the ESXi host is located.
.PARAMETER esxiFqdn
The fully qualified domain name of the ESXi host to verify the certificate thumbprint against.
.PARAMETER signedCertificate
The complete path for the signed certificate file.
.PARAMETER issuer
The name of the issuer to match with the vCenter Server instance's certificate thumbprints.
#>
Param (
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $server,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $user,
[Parameter (Mandatory = $false)] [ValidateNotNullOrEmpty()] [String] $pass,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $domain,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $cluster,
[Parameter (Mandatory = $false)] [ValidateNotNullOrEmpty()] [String] $esxiFqdn,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $signedCertificate,
[Parameter (Mandatory = $false)] [ValidateNotNullOrEmpty()] [String] $issuer
)
$pass = Get-Password -User $user -Password $pass
$errorMessage = @()
$warningMessage = @()
$statusMessage = @()
Try {
Write-Output "############## Running Prechecks for ESXi Certificate Management ###############"
$status = "FAILED"
$vCenterServer = Get-vCenterServer -server $server -user $user -pass $pass -domain $domain
$mode = Get-EsxiCertificateMode -server $server -user $user -pass $pass -domain $domain
if ($mode -ne "custom") {
$msg = "Certificate Management Mode is not set to $mode on the vCenter Server instance $($vCenterServer.details.fqdn)."
$errorMessage += $msg
} else {
$msg = "Certificate Management Mode is set to $mode on the vCenter Server instance $($vCenterServer.details.fqdn)."
$statusMessage += $statusMessage
$status = "PASSED"
}
Write-Output "Check ESXi Certificate Mode: $status"
$status = "FAILED"
if ($PsBoundParameters.ContainsKey("esxiFqdn")) {
$lockdownModes = Get-EsxiLockdownMode -server $server -user $user -pass $pass -domain $domain -cluster $cluster -esxiFqdn $esxiFqdn
} else {
$lockdownModes = Get-EsxiLockdownMode -server $server -user $user -pass $pass -domain $domain -cluster $cluster
}
foreach ($lockdownMode in $lockdownModes) {
if ($lockdownMode -like "*lockdownDisabled*") {
$statusMessage += $lockdownMode
$status = "PASSED"
} else {
$errorMessage += $lockdownMode
}
}
Write-Output "Check ESXi Lockdown Mode: $status"
$status = "FAILED"
$caStatus = Confirm-CAInvCenterServer -server $server -user $user -pass $pass -domain $domain -issuer $issuer -signedCertificate $signedCertificate
if ($caStatus -eq $true) {
$msg = "Signed certificate thumbprint matches with the vCenter Server certificate authority thumbprint."
$statusMessage += $msg
$status = "PASSED"
} elseif ($caStatus -eq $false) {
$msg = "Signed certificate thumbprint does not match any of the vCenter Server certificate authority thumbprints."
$errorMessage += $msg
} else {
$msg = "Error: Unable to Confirm CA In vCenter Server."
$msg = $msg + $caStatus
$errorMessage += $msg
}
Write-Output "Confirm CA In vCenter Server: $status"
$status = "FAILED"
$vsanStatus = Get-vSANHealthSummary -server $server -user $user -pass $pass -domain $domain -cluster $cluster -errorAction SilentlyContinue -ErrorVariable errorMsg -WarningAction SilentlyContinue -WarningVariable warnMsg
if ($warnMsg) {
$warningMessage += $warnMsg
$status = "WARNING"
}
if ($errorMsg) {
$errorMessage += $errorMsg
}
if ($vsanStatus -eq 0) {
$status = "PASSED"
$statusMessage += $vsanStatus
}
Write-Output "Check vSAN Health Status: $status"
Write-Output "############## Finished Running Prechecks for ESXi Certificate Management ###############"
if ($statusMessage) {
Write-Debug "############## Status of ESXi Certificate Management Prechecks : ###############"
foreach ($msg in $statusMessage) {
Write-Debug $msg
}
}
if ($warningMessage) {
Write-Output "############## Warnings Raised While Running Prechecks for ESXi Certificate Management : ###############"
foreach ($msg in $warningMessage) {
Write-Warning $msg
}
}
if ($errorMessage) {
Write-Output "############## Issues Found While Running Prechecks for ESXi Certificate Management : ###############"
foreach ($msg in $errorMessage) {
Write-Error $msg
}
}
} Catch {
Debug-ExceptionWriter -object $_
}
}
Function Confirm-EsxiCertificateInstalled {
<#
.SYNOPSIS
Verifies if the provided certificate is already on the ESXi host.
.DESCRIPTION
The Confirm-EsxiCertificateInstalled cmdlet will get the thumbprint from the provided signed certificate and
matches it with the certificate thumbprint from ESXi host. You need to pass in the complete path for the
certificate file. Returns true if certificate is already installed, else returns false.
.EXAMPLE
Confirm-EsxiCertificateInstalled -server sfo-vcf01.sfo.rainpole.io -user [email protected] -pass VMw@re1! -esxiFqdn sfo01-w01-esx01.sfo.rainpole.io -signedCertificate F:\certificates\sfo01-w01-esx01.sfo.rainpole.io.cer
This example checks the thumbprint of the provided signed certificate with the thumbprint on ESXi host.
.PARAMETER server
The fully qualified domain name of the SDDC Manager instance.
.PARAMETER user
The username to authenticate to the SDDC Manager instance.
.PARAMETER pass
The password to authenticate to the SDDC Manager instance.
.PARAMETER esxiFqdn
The fully qualified domain name of the ESXi host to verify the certificate thumbprint against.
.PARAMETER signedCertificate
The complete path for the signed certificate file.
#>
Param (
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $server,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $user,
[Parameter (Mandatory = $false)] [ValidateNotNullOrEmpty()] [String] $pass,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $esxiFqdn,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $signedCertificate
)
$pass = Get-Password -User $user -Password $pass
Try {
if (Test-Path $signedCertificate -PathType Leaf ) {
Write-Debug "Certificate file found - $signedCertificate"
} else {
Write-Error "Could not find certificate in $signedCertificate." -ErrorAction Stop
return
}
$esxiCertificateThumbprint = Get-VCFCertificateThumbprint -esxi -server $server -user $user -pass $pass -esxiFqdn $esxiFqdn
$crt = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2($signedCertificate)
$signedCertThumbprint = $crt.Thumbprint
if ($esxiCertificateThumbprint -eq $signedCertThumbprint) {
Write-Debug "Signed certificate thumbprint matches with the ESXi host certificate thumbprint."
Write-Warning "Certificate is already installed on ESXi host $esxiFqdn : SKIPPED"
return $true
} else {
Write-Debug "ESXi host's certificate thumbprint ($esxiCertificateThumbprint) does not match with the thumbprint of provided certificate ($signedCertThumbprint)"
Write-Debug "Provided certificate is not installed on ESXi host $esxiFqdn."
return $false
}
} Catch {
Debug-ExceptionWriter -object $_
}
}
Function Confirm-CAInvCenterServer {
<#
.SYNOPSIS
Verifies the root certificate thumbprint matches with one of the CA thumbprints from vCenter Server instance.
.DESCRIPTION
The Confirm-CAInvCenterServer cmdlet gets the thumbprint from the root certificate and matches it with the CA
thumbprint from the vCenter Server instance.You need to pass in the complete path for the certificate file.
Returns true if thumbprint matches, else returns false.
.EXAMPLE
Confirm-CAInvCenterServer -server sfo-vcf01.sfo.rainpole.io -user [email protected] -pass VMw@re1! -domain sfo-m01 -issuer rainpole -signedCertificate F:\certificates\Root64.cer
This example matches the thumbprint of provided root certificate file with the thumbprints on the vCenter Server instance matching the issuer "rainpole".
.PARAMETER server
The fully qualified domain name of the SDDC Manager instance.
.PARAMETER user
The username to authenticate to the SDDC Manager instance.
.PARAMETER pass
The password to authenticate to the SDDC Manager instance.
.PARAMETER domain
The name of the workload domain to retrieve the vCenter Server instance's certificate thumbprints from.
.PARAMETER signedCertificate
The complete path for the root certificate file.
.PARAMETER issuer
The name of the issuer to match with the thumbprint.
#>
Param (
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $server,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $user,
[Parameter (Mandatory = $false)] [ValidateNotNullOrEmpty()] [String] $pass,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $domain,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $signedCertificate,
[Parameter (Mandatory = $false)] [ValidateNotNullOrEmpty()] [String] $issuer
)
$pass = Get-Password -User $user -Password $pass
Try {
if ($PsBoundParameters.ContainsKey("issuer")) {
$vcThumbprints = Get-VCFCertificateThumbprint -vcenter -server $server -user $user -pass $pass -domain $domain -issuer $issuer
} else {
$vcThumbprints = Get-VCFCertificateThumbprint -vcenter -server $server -user $user -pass $pass -domain $domain
}
if (Test-Path $signedCertificate -PathType Leaf ) {
Write-Output "Certificate file found - $signedCertificate."
} else {
Write-Error "Could not find certificate in $signedCertificate." -ErrorAction Stop
return
}
$crt = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2($signedCertificate)
$signedCertThumbprint = $crt.Thumbprint
$match = $false
foreach ($vcThumbprint in $vcThumbprints) {
if ($vcThumbprint -eq $signedCertThumbprint) {
Write-Output "Signed certificate thumbprint matches with the vCenter Server certificate authority thumbprint."
$match = $true
break
}
}
if (!$match) {
Write-Error "Signed certificate thumbprint does not match any of the vCenter Server certificate authority thumbprints."
}
return $match
} Catch {
Debug-ExceptionWriter -object $_
}
}
Function Get-EsxiCertificateMode {
<#
.SYNOPSIS
Retrieves the certificate management mode value from the vCenter Server instance for a workload domain.
.DESCRIPTION
The Get-EsxiCertificateMode cmdlet retrieves the certificate management mode value from vCenter Server instance
for a workload domain.
.EXAMPLE
Get-EsxiCertificateMode -server sfo-vcf01.sfo.rainpole.io -user [email protected] -pass VMw@re1! -domain sfo-m01
This example retrieves the certificate management mode value for the vCenter Server instance for the workload domain sfo-m01.
.PARAMETER server
The fully qualified domain name of the SDDC Manager instance.
.PARAMETER user
The username to authenticate to the SDDC Manager instance.
.PARAMETER pass
The password to authenticate to the SDDC Manager instance.
.PARAMETER domain
The name of the workload domain to retrieve the certificate management mode value for.
#>
Param (
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $server,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $user,
[Parameter (Mandatory = $false)] [ValidateNotNullOrEmpty()] [String] $pass,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $domain
)
$pass = Get-Password -User $user -Password $pass
Try {
$vCenterServer = Get-vCenterServer -server $server -user $user -pass $pass -domain $domain
$certModeSetting = Get-AdvancedSetting "vpxd.certmgmt.mode" -Entity $vCenterServer.connection
return $certModeSetting.value
} Catch {
Debug-ExceptionWriter -object $_
} Finally {
if ($vCenterServer) { Disconnect-VIServer -server $vCenterServer.details.fqdn -Confirm:$false -WarningAction SilentlyContinue }
}
}
Function Set-EsxiCertificateMode {
<#
.SYNOPSIS
Sets the certificate management mode in vCenter Server for the ESXi hosts in a workload domain.
.DESCRIPTION
The Set-EsxiCertificateMode cmdlet sets the certificate management mode in vCenter Server for the ESXi hosts in
a workload domain.
.EXAMPLE
Set-EsxiCertificateMode -server sfo-vcf01.sfo.rainpole.io -user [email protected] -pass VMw@re1! -domain sfo-m01 -mode custom
This example sets the certificate management mode to custom in vCenter Server for the ESXi hosts in workload domain sfo-m01.
.PARAMETER server
The fully qualified domain name of the SDDC Manager instance.
.PARAMETER user
The username to authenticate to the SDDC Manager instance.
.PARAMETER pass
The password to authenticate to the SDDC Manager instance.
.PARAMETER domain
The name of the workload domain to set the vCenter Server instance certificate management mode setting for.
.PARAMETER mode
The certificate management mode to set in vCenter Server. One of "custom" or "vmca".
#>
Param (
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $server,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $user,
[Parameter (Mandatory = $false)] [ValidateNotNullOrEmpty()] [String] $pass,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $domain,
[Parameter (Mandatory = $true)] [ValidateSet ("custom", "vmca")] [String] $mode
)
$pass = Get-Password -User $user -Password $pass
Try {
$vCenterServer = Get-vCenterServer -server $server -user $user -pass $pass -domain $domain
$certModeSetting = Get-AdvancedSetting "vpxd.certmgmt.mode" -Entity $vCenterServer.connection
if ($certModeSetting.value -ne $mode) {
Set-AdvancedSetting $certModeSetting -Value $mode -confirm:$false
# Restart "VMware Certificate Authority" and "VMware Certificate Management" services.
Write-Output 'Restarting vCenter Server services ("VMware Certificate Authority" and "VMware Certificate Management") for the change to take effect.'
$services = @("certificateauthority", "certificatemanagement")
$failedServices = @()
foreach ($service in $services) {
$serviceStatus = Restart-VcenterService -serviceName $service
if ($serviceStatus.status -ne "GREEN") {
$failedServices += $serviceStatus
}
}
if ($failedServices.Count -gt 0) {
$failedServicesErrorString = ""
foreach ($failedItem in $failedServices) {
$failedServicesErrorString += "$($failedItem.name): $($failedItem.result). `n"
}
Write-Error "The following services failed to restart successfully:`n$failedServicesErrorString`nSet-EsxiCertificateMode operation Failed." -ErrorAction Stop
} else {
Write-Output 'vCenter Server services ("VMware Certificate Authority" and "VMware Certificate Management") restarted successfully.'
}
} else {
Write-Warning "Certificate Management Mode already set to $mode on the vCenter Server instance $($vCenterServer.details.fqdn): SKIPPED"
}
} Catch {
Debug-ExceptionWriter -object $_
} Finally {
if ($vCenterServer) { Disconnect-VIServer -server $vCenterServer.details.fqdn -Confirm:$false -WarningAction SilentlyContinue }
}
}
Function Get-vSANHealthSummary {
<#
.SYNOPSIS
Retrieves the vSAN health summary from vCenter Server for a cluster.
.DESCRIPTION
The Get-vSANHealthSummary cmdlet gets the vSAN health summary from vCenter Server for a cluster.
If any status is YELLOW or RED, a WARNING or ERROR will be raised.
.EXAMPLE
Get-vSANHealthSummary -server sfo-vcf01.sfo.rainpole.io -user [email protected] -pass VMw@re1! -domain sfo-m01 -cluster sfo-m01-cl01
This example gets the vSAN health summary for cluster sfo-m01-cl01.
.PARAMETER server
The fully qualified domain name of the SDDC Manager instance.
.PARAMETER user
The username to authenticate to the SDDC Manager instance.
.PARAMETER pass
The password to authenticate to the SDDC Manager instance.
.PARAMETER domain
The name of the workload domain in which the cluster is located.
.PARAMETER cluster
The name of the cluster to retrieve the vSAN health summary for.
#>
Param (
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $server,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $user,
[Parameter (Mandatory = $false)] [ValidateNotNullOrEmpty()] [String] $pass,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $domain,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $cluster
)
$pass = Get-Password -User $user -Password $pass
Try {
$vCenterServer = Get-vCenterServer -server $server -user $user -pass $pass -domain $domain
$vSANClusterHealthSystem = Get-VSANView -Id "VsanVcClusterHealthSystem-vsan-cluster-health-system"
$overallStatus = 0
if (!$vSANClusterHealthSystem) {
Write-Error "Cannot run the Get-vSANHealthSummary cmdlet because the vSAN health service is not running."
return 2
}
$cluster_view = (Get-Cluster -Name $cluster).ExtensionData.MoRef
$results = $vSANClusterHealthSystem.VsanQueryVcClusterHealthSummary($cluster_view, $null, $null, $true, $null, $null, 'defaultView')
$healthCheckGroups = $results.groups
foreach ($healthCheckGroup in $healthCheckGroups) {
$health = @("Yellow", "Red")
$output = $healthCheckGroup.grouptests | Where-Object TestHealth -in $health | Select-Object TestHealth, @{l = "TestId"; e = { $_.testid.split(".") | Select-Object -last 1 } }, TestName, TestShortDescription, @{l = "Group"; e = { $healthCheckGroup.GroupName } }
$healthCheckTestHealth = $output.TestHealth
$healthCheckTestName = $output.TestName
$healthCheckTestShortDescription = $output.TestShortDescription
if ($healthCheckTestName) {
if ($healthCheckTestHealth -eq "yellow") {
$overallStatus = ($overallStatus, 1 | Measure-Object -Max).Maximum
Write-Warning "$($vCenterServer.details.fqdn) - vSAN cluster $cluster | vSAN Alarm Name - $healthCheckTestName | Alarm Description - $healthCheckTestShortDescription"
}
if ($healthCheckTestHealth -eq "red") {
$overallStatus = ($overallStatus, 2 | Measure-Object -Max).Maximum
Write-Error "vSAN status is RED. Please check the vSAN health before continuing."
Write-Error "$($vCenterServer.details.fqdn) - vSAN Clustername $cluster | vSAN Alarm Name - $healthCheckTestName | Alarm Description - $healthCheckTestShortDescription"
}
}
}
if ($overallStatus -eq 0) {
Write-Output "The vSAN health status for $cluster is GREEN."
}
return $overallStatus
} Catch {
Debug-ExceptionWriter -object $_
} Finally {
if ($vCenterServer) { Disconnect-VIServer -server $vCenterServer.details.fqdn -Confirm:$false -WarningAction SilentlyContinue }
}
}
Function Get-EsxiConnectionState {
<#
.SYNOPSIS
Retrieves the ESXi host connection state from vCenter Server.
.DESCRIPTION
The Get-EsxiConnectionState cmdlet gets the connection state of an ESXi host.
One of "Connected", "Disconnected", "Maintenance", or "NotResponding"
Depends on a connection to a vCenter Server instance.
.EXAMPLE
Get-EsxiConnectionState -esxiFqdn sfo01-m01-esx01.sfo.rainpole.io
This example gets an ESXi host's connection state.
.PARAMETER esxiFqdn
The fully qualified domain name of the ESXi host.
#>
Param (
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $esxiFqdn
)
$response = Get-VMHost -name $esxiFqdn
return $response.ConnectionState
}
Function Get-EsxiHostVsanMaintenanceModePrecheck {
<#
.SYNOPSIS
Checks for any issues when the ESXi H=host enters a particular vSAN maintenance mode.
.DESCRIPTION
The Get-EsxiHostVsanMaintenanceModePrecheck cmdlet checks if there's any issues for the ESXi host entering a particular vSAN maintenance mode.
The cmdlet will halt the script if the pre check fails.
.EXAMPLE
Get-EsxiHostVsanMaintenanceModePrecheck -server sfo-vcf01.sfo.rainpole.io -user [email protected] -pass VMware1! -domain sfo-m01 -cluster sfo-m01-cl01 -vsanDataMigrationMode Full
This example checks each ESXi host within a cluster within the workload domain for any issues when entering a particular vSAN maintenance mode
Get-EsxiHostVsanMaintenanceModePrecheck -server sfo-vcf01.sfo.rainpole.io -user [email protected] -pass VMware1! -domain sfo-m01 -host sfo01-m01-esx01.sfo.rainpole.io -vsanDataMigrationMode Full
This example checks each ESXi host within a cluster within the workload domain for any issues when entering a particular vSAN maintenance mode
.PARAMETER server
The fully qualified domain name of the SDDC Manager instance.
.PARAMETER user
The username to authenticate to the SDDC Manager instance.
.PARAMETER pass
The password to authenticate to the SDDC Manager instance.
.PARAMETER domain
The name of the workload domain in which the cluster is located.
.PARAMETER cluster
The name of the cluster containing the ESXi hosts.
.PARAMETER esxiFqdn
The name of the FQDN of an ESXi host.
.PARAMETER vsanDataMigrationMode
The type of vSAN maintenance mode.
#>
Param (
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $server,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $user,
[Parameter (Mandatory = $false)] [ValidateNotNullOrEmpty()] [String] $pass,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $domain,
[Parameter (Mandatory = $true, ParameterSetName = "cluster")] [ValidateNotNullOrEmpty()] [String] $cluster,
[Parameter (Mandatory = $true, ParameterSetName = "esxiFqdn")] [ValidateNotNullOrEmpty()] [String] $esxiFqdn,
[Parameter (Mandatory = $true)] [ValidateSet ("Full", "EnsureAccessibility", "NoDataMigration")] [String] $vsanDataMigrationMode
)
if ($vsanDataMigrationMode -eq "Full") {
$vsanMigrationMode = "evacuateAllData"
} elseif ($vsanDataMigrationMode -eq "EnsureAccessibility") {
$vsanMigrationMode = "ensureObjectAccessibility"
} elseif ($vsanDataMigrationMode -eq "NoDataMigration") {
$vsanMigrationMode = "noAction"
} else {
Write-Error "No validate vsan Data migration mode selected" -ErrorAction Stop
}
Try {
$vCenterServer = Get-vCenterServer -server $server -user $user -pass $pass -domain $domain
if ($PsBoundParameters.ContainsKey("cluster")) {
$clusterDetails = Get-VCFCluster -Name $cluster
if ($clusterDetails) {
$esxiHosts = Get-VCFHost | Where-Object { $_.cluster.id -eq $clusterDetails.id } | Sort-Object -Property fqdn
if (!$esxiHosts) { Write-Warning "No ESXi hosts found in cluster $cluster." }
} else {
Write-Error "Unable to locate cluster $cluster in $($vCenterServer.details.fqdn) vCenter Server: PRE_VALIDATION_FAILED" -ErrorAction Stop
}
} else {
$esxiHosts = Get-VCFHost -fqdn $esxiFqdn
if (!$esxiHosts) { Write-Error "No ESXi host $esxiFqdn found in workload domain $domain." -ErrorAction Stop }
}
foreach ($esxiHost in $esxiHosts) {
$vsanReport = Get-VsanEnterMaintenanceModeReport -VMHost $esxiHost.fqdn -VsanDataMigrationMode $vsanMigrationMode
if ($vsanReport.OverallStatus -ne "green") {
Write-Error "ESXi host($($esxiHost.fqdn)) vSAN Data Migration($vsanDataMigrationMode) Pre-check failed with error $($vsanReport.OverallStatus)" -ErrorAction Stop
} else {
Write-Output "ESXi host($($esxiHost.fqdn)) vSAN Data Migration($vsanDataMigrationMode) Pre-check: $($vsanReport.OverallStatus)"
}
}
} Catch {
Debug-ExceptionWriter -object $_
} Finally {
if ($vCenterServer) { Disconnect-VIServer -server $vCenterServer.details.fqdn -Confirm:$false -WarningAction SilentlyContinue }
}
}
Function Set-EsxiConnectionState {
<#
.SYNOPSIS
Sets the ESXi host connection state in vCenter Server.
.DESCRIPTION
The Set-EsxiConnectionState cmdlet sets the connection state of an ESXi host.
One of "Connected", "Disconnected" or "Maintenance".
If setting the connection state to Maintenance, provide the VsanDataMigrationMode for a vSAN environment.
One of "Full", "EnsureAccessibility", or "NoDataMigration".
Depends on a connection to a vCenter Server instance.
.EXAMPLE
Set-EsxiConnectionState -esxiFqdn sfo01-m01-esx01.sfo.rainpole.io -state Connected
This example sets an ESXi host's connection state to Connected.
.EXAMPLE
Set-EsxiConnectionState -esxiFqdn sfo01-m01-esx01.sfo.rainpole.io -state Maintenance -vsanDataMigrationMode Full
This example sets an ESXi host's connection state to Maintenance with a vSAN data migration mode set to Full data migration.
.EXAMPLE
Set-EsxiConnectionState -esxiFqdn sfo01-m01-esx01.sfo.rainpole.io -state Maintenance -vsanDataMigrationMode EnsureAccessibility -migratePowerOffVMs
This example sets an ESXi host's connection state to Maintenance and will migrate any Power Off or Suspend VMs to other ESXi hosts and
will set vSAN data migration mode to Ensure Accessibility.
.PARAMETER esxiFqdn
The fully qualified domain name of the ESXi host.
.PARAMETER state
The connection state to set the ESXi host to. One of "Connected", "Disconnected" or "Maintenance".
.PARAMETER migratePowerOffVMs
This optional switch argument will determined if power off and suspended VMs will be migrated off the ESXi host when setting the ESXi host to Maintenance.
.PARAMETER vsanDataMigrationMode
The vSAN data migration mode to use when setting the ESXi host to Maintenance. One of "Full", "EnsureAccessibility", or "NoDataMigration".
.PARAMETER timeout
The timeout in seconds to wait for the ESXi host to reach the desired connection state. Default is 18000 seconds (5 hours).
.PARAMETER pollInterval
The poll interval in seconds to check the ESXi host connection state. Default is 60 seconds.
#>
Param (
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $esxiFqdn,
[Parameter (Mandatory = $true)] [ValidateSet ("Connected", "Disconnected", "Maintenance")] [String] $state,
[Parameter (Mandatory = $false)] [Switch] $migratePowerOffVMs,
[Parameter (Mandatory = $false)] [ValidateSet ("Full", "EnsureAccessibility", "NoDataMigration")] [String] $vsanDataMigrationMode,
[Parameter (Mandatory = $false)] [ValidateNotNullOrEmpty()] [String] $timeout = 18000,
[Parameter (Mandatory = $false)] [ValidateNotNullOrEmpty()] [String] $pollInterval = 60
)
if ($state -ieq (Get-EsxiConnectionState -esxiFqdn $esxiFqdn)) {
Write-Warning "ESXi host $esxiFqdn is already in the $state connection state: SKIPPED"
return
}
if ($state -ieq "maintenance") {
if ($PSBoundParameters.ContainsKey("vsanDataMigrationMode")) {
if (($vsanDataMigrationMode -eq "EnsureAccessibility") -and !($migratePowerOffVMs.IsPresent)) {
Write-Output "Entering $state connection state for ESXi host $esxiFqdn with vSAN data migration mode set to $vsanDataMigrationMode."
Write-Output "Power off VMs and suspended VMs are left on the ESXi host $esxiFqdn."
Set-VMHost -VMHost $esxiFqdn -State $state -VsanDataMigrationMode $vsanDataMigrationMode -confirm:$false
} elseif (($vsanDataMigrationMode -eq "NoDataMigration") -and !($migratePowerOffVMs.IsPresent)) {
Write-Output "Entering $state connection state for ESXi host $esxiFqdn with vSAN data migration mode set to $vsanDataMigrationMode."
Write-Output "Power off VMs and suspended VMs are left on the ESXi host $esxiFqdn."
Set-VMHost -VMHost $esxiFqdn -State $state -VsanDataMigrationMode $vsanDataMigrationMode -confirm:$false
} else {
Write-Output "Entering $state connection state for ESXi host $esxiFqdn with vSAN data migration mode set to $vsanDataMigrationMode."
Write-Output "Power off VMs and suspended VMs will be migrated off to other ESXi hosts."
Set-VMHost -VMHost $esxiFqdn -State $state -VsanDataMigrationMode $vsanDataMigrationMode -Evacuate -confirm:$false
}
} else {
if ($migratePowerOffVMs.IsPresent) {
Write-Output "Entering $state connection state for ESXi host $esxiFqdn. (Power off VMs and suspended VMs will be migrated off to other ESXi hosts)"