Public/Get-ESXSpeculativeExecution.ps1

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
function Get-ESXSpeculativeExecution {
    <#
     .SYNOPSIS
       Get ESXi host mitigation status for Spectre
     .DESCRIPTION
       Will validate ESXi host for Spectre/Hypervisor-Assisted Guest Mitigation
       https://www.vmware.com/security/advisories/VMSA-2018-0004.html
     .NOTES
       File Name : Get-ESXSpeculativeExecution.ps1
       Author : Edgar Sanchez - @edmsanchez13
       Contributor : Ariel Sanchez - @arielsanchezmor
       Version : 2.4.3
     .Link
       https://github.com/arielsanchezmora/vDocumentation
     .INPUTS
       If inputBiosFile is specified then an offline CSV file path input must be provided to check against BIOS version
       If inputMcuFile is specified then an offline CSV file path input must be provided to check against MCU revision
       If UseSSH is specified then SSH will be used to gather details around current MCU revisions. You will be prompted for username and password
     .OUTPUTS
       CSV file
       Excel file
     .PARAMETER esxi
       The name(s) of the vSphere ESXi Host(s)
     .EXAMPLE
       Get-ESXSpeculativeExecution -esxi devvm001.lab.local
     .PARAMETER cluster
       The name(s) of the vSphere Cluster(s)
     .EXAMPLE
       Get-ESXSpeculativeExecution -cluster production-cluster
     .PARAMETER datacenter
       The name(s) of the vSphere Virtual Datacenter(s).
     .EXAMPLE
       Get-ESXSpeculativeExecution -datacenter vDC001
       Get-ESXSpeculativeExecution -datacenter "all vdc" will gather all hosts in vCenter(s). This is the default if no Parameter (-esxi, -cluster, or -datacenter) is specified.
     .PARAMETER inputBiosFile
       Specify input CSV file containing BIOS versions to validate against. Use this if you do not have access to the internet or wish to use your own offline version
     .EXAMPLE
       Get-ESXSpeculativeExecution -cluster production-cluster -inputBiosFile "c:\temp\BIOSUpdates.csv"
     .PARAMETER inputMcuFile
       Specify input CSV file containing Intel MCU version to validate against. Use this if you do not have access to the internet or wish to use your own offline version
     .EXAMPLE
       Get-ESXSpeculativeExecution -cluster production-cluster -inputMcuFile "c:\temp\Intel_MCU.csv"
     .PARAMETER UseSSH
       Switch to use SSH to gahter specific MCU revisions details from the ESXi host. This needs to be specified if needed. This relies on Posh-SSH Module to be installed.
       Posh-SSH Module can be installed directly from the PowerShell Gallery. See https://github.com/darkoperator/Posh-SSH for more information
     .EXAMPLE
       Get-ESXSpeculativeExecution -cluster production-cluster -ReportOnVMS
     .PARAMETER ReportOnVMs
       Switch to report on virtual machines. This needs to be specified if needed.
     .EXAMPLE
       Get-ESXSpeculativeExecution -cluster production-cluster -ReportOnVMS
     .PARAMETER ExportCSV
       Switch to export all data to CSV file. File is saved to the current user directory from where the script was executed. Use -folderPath parameter to specify a alternate location
     .EXAMPLE
       Get-ESXSpeculativeExecution -cluster production-cluster -ExportCSV
     .PARAMETER ExportExcel
       Switch to export all data to Excel file (No need to have Excel Installed). This relies on ImportExcel Module to be installed.
       ImportExcel Module can be installed directly from the PowerShell Gallery. See https://github.com/dfinke/ImportExcel for more information
       File is saved to the current user directory from where the script was executed. Use -folderPath parameter to specify a alternate location
     .EXAMPLE
       Get-ESXSpeculativeExecution -cluster production-cluster -ExportExcel
     .PARAMETER folderPath
       Specify an alternate folder path where the exported data should be saved.
     .EXAMPLE
       Get-ESXSpeculativeExecution -cluster production-cluster -ExportExcel -folderPath C:\temp
     .PARAMETER PassThru
       Switch to return object to command line
     .EXAMPLE
       Get-ESXSpeculativeExecution -esxi 192.168.1.100 -Hardware -PassThru
    #>
 
    
    <#
     ----------------------------------------------------------[Declarations]----------------------------------------------------------
    #>

  
    [CmdletBinding()]
    param (
        $esxi,
        $cluster,
        $datacenter,
        $inputBiosFile,
        $inputMcuFile,
        [switch]$UseSSH,
        [switch]$ReportOnVMs,
        [switch]$ExportCSV,
        [switch]$ExportExcel,
        [switch]$PassThru,
        $folderPath
    )
    
    $patchCollection = @()
    $skipCollection = @()
    $vmCollection = @()
    $vHostList = @()
    $returnCollection = @()
    $biosCsvCollection = @()
    $mcuCsvCollection = @()
    $biosUrl = 'https://raw.githubusercontent.com/edmsanchez/vDocumentation/master/powershell/vDocumentation/BIOSUpdates.csv'
    $mcuUrl = 'https://raw.githubusercontent.com/edmsanchez/vDocumentation/master/powershell/vDocumentation/Intel_MCU.csv'
    $date = Get-Date -format s
    $date = $date -replace ":", "-"
    $outputFile = "SpeculativeExecution" + $date
    <#
      VMSA-2018-0004.3 Build IDs
    #>

    $esx55Build = "7967571"
    $esx60Build = "7967664"
    $esx65Build = "7967591"
    
    <#
     ----------------------------------------------------------[Execution]----------------------------------------------------------
    #>

  
    <#
      Query PowerCLI and vDocumentation versions if
      running Verbose
    #>

    if ($VerbosePreference -eq "continue") {
        Write-Verbose -Message ((Get-Date -Format G) + "`tPowerCLI Version:")
        Get-Module -Name VMware.* | Select-Object -Property Name, Version | Format-Table -AutoSize
        Write-Verbose -Message ((Get-Date -Format G) + "`tvDocumentation Version:")
        Get-Module -Name vDocumentation | Select-Object -Property Name, Version | Format-Table -AutoSize
    } #END if

    <#
      Check for an active connection to a VIServer
    #>

    Write-Verbose -Message ((Get-Date -Format G) + "`tValidate connection to a vSphere server")
    if ($Global:DefaultViServers.Count -gt 0) {
        Write-Host "`tConnected to $Global:DefaultViServers" -ForegroundColor Green
    }
    else {
        Write-Error -Message "You must be connected to a vSphere server before running this Cmdlet."
        break
    } #END if/else
    
    <#
      Validate if a parameter was specified (-esxi, -cluster, or -datacenter)
      Although all 3 can be specified, only the first one is used
      Example: -esxi "host001" -cluster "test-cluster". -esxi is the first parameter
      and what will be used.
    #>

    Write-Verbose -Message ((Get-Date -Format G) + "`tValidate parameters used")
    if ([string]::IsNullOrWhiteSpace($esxi) -and [string]::IsNullOrWhiteSpace($cluster) -and [string]::IsNullOrWhiteSpace($datacenter)) {
        Write-Verbose -Message ((Get-Date -Format G) + "`tA parameter (-esxi, -cluster, -datacenter) was not specified. Will gather all hosts")
        $datacenter = "all vdc"
    } #END if
    
    <#
      Gather host list based on parameter used
    #>

    Write-Verbose -Message ((Get-Date -Format G) + "`tGather host list")
    if ([string]::IsNullOrWhiteSpace($esxi)) {      
        Write-Verbose -Message ((Get-Date -Format G) + "`t-esxi parameter is Null or Empty")
        if ([string]::IsNullOrWhiteSpace($cluster)) {
            Write-Verbose -Message ((Get-Date -Format G) + "`t-cluster parameter is Null or Empty")
            if ([string]::IsNullOrWhiteSpace($datacenter)) {
                Write-Verbose -Message ((Get-Date -Format G) + "`t-datacenter parameter is Null or Empty")
            }
            else {
                Write-Verbose -Message ((Get-Date -Format G) + "`tExecuting Cmdlet using datacenter parameter")
                if ($datacenter -eq "all vdc") {
                    Write-Host "`tGathering all hosts from the following vCenter(s): " $Global:DefaultViServers
                    $vHostList = Get-VMHost | Sort-Object -Property Name                    
                }
                else {
                    Write-Host "`tGathering host list from the following DataCenter(s): " (@($datacenter) -join ',')
                    foreach ($vDCname in $datacenter) {
                        $tempList = Get-Datacenter -Name $vDCname.Trim() -ErrorAction SilentlyContinue | Get-VMHost 
                        if ($tempList) {
                            $vHostList += $tempList | Sort-Object -Property Name
                        }
                        else {
                            Write-Warning -Message "`tDatacenter with name $vDCname was not found in $Global:DefaultViServers"
                        } #END if/else
                    } #END foreach
                } #END if/else
            } #END if/else
        }
        else {
            Write-Verbose -Message ((Get-Date -Format G) + "`tExecuting Cmdlet using cluster parameter")
            Write-Host "`tGathering host list from the following Cluster(s): " (@($cluster) -join ',')
            foreach ($vClusterName in $cluster) {
                $tempList = Get-Cluster -Name $vClusterName.Trim() -ErrorAction SilentlyContinue | Get-VMHost 
                if ($tempList) {
                    $vHostList += $tempList | Sort-Object -Property Name
                }
                else {
                    Write-Warning -Message "`tCluster with name $vClusterName was not found in $Global:DefaultViServers"
                } #END if/else
            } #END foreach
        } #END if/else
    }
    else { 
        Write-Verbose -Message ((Get-Date -Format G) + "`tExecuting Cmdlet using esxi parameter")
        Write-Host "`tGathering host list..."
        foreach ($invidualHost in $esxi) {
            $tempList = Get-VMHost -Name $invidualHost.Trim() -ErrorAction SilentlyContinue
            if ($tempList) {
                $vHostList += $tempList | Sort-Object -Property Name
            }
            else {
                Write-Warning -Message "`tESXi host $invidualHost was not found in $Global:DefaultViServers"
            } #END if/else
        } #END foreach
    } #END if/else
    $tempList = $null
    
    <#
      Validate export switches,
      folder path and dependencies
    #>

    Write-Verbose -Message ((Get-Date -Format G) + "`tValidate export switches and folder path")
    if ($ExportCSV -or $ExportExcel) {
        $currentLocation = (Get-Location).Path
        if ([string]::IsNullOrWhiteSpace($folderPath)) {
            Write-Verbose -Message ((Get-Date -Format G) + "`t-folderPath parameter is Null or Empty")
            Write-Warning -Message "`tFolder Path (-folderPath) was not specified for saving exported data. The current location: '$currentLocation' will be used"
            $outputFile = $currentLocation + "\" + $outputFile
        }
        else {
            if (Test-Path $folderPath) {
                Write-Verbose -Message ((Get-Date -Format G) + "`t'$folderPath' path found")
                $lastCharsOfFolderPath = $folderPath.Substring($folderPath.Length - 1)
                if ($lastCharsOfFolderPath -eq "\" -or $lastCharsOfFolderPath -eq "/") {
                    $outputFile = $folderPath + $outputFile
                }
                else {
                    $outputFile = $folderPath + "\" + $outputFile
                } #END if/else
                Write-Verbose -Message ((Get-Date -Format G) + "`t$outputFile")
            }
            else {
                Write-Warning -Message "`t'$folderPath' path not found. The current location: '$currentLocation' will be used instead"
                $outputFile = $currentLocation + "\" + $outputFile
            } #END if/else
        } #END if/else
    } #END if
    if ($ExportExcel) {
        if (Get-Module -ListAvailable -Name ImportExcel) {
            Write-Verbose -Message ((Get-Date -Format G) + "`tImportExcel Module available")
        }
        else {
            Write-Warning -Message "`tImportExcel Module missing. Will export data to CSV file instead"
            Write-Warning -Message "`tImportExcel Module can be installed directly from the PowerShell Gallery"
            Write-Warning -Message "`tSee https://github.com/dfinke/ImportExcel for more information"
            $ExportExcel = $false
            $ExportCSV = $true
        } #END if/else
    } #END if

    <#
      Validate UseSSH switch and dependencies
    #>

    Write-Verbose -Message ((Get-Date -Format G) + "`tValidate UseSSH switch")
    if ($UseSSH) {
        if (Get-Module -ListAvailable -Name Posh-SSH) {
            Write-Verbose -Message ((Get-Date -Format G) + "`tPosh-SSH Module available")
            $poshSSH = $true
            $rootCreds = Get-Credential -UserName root -Message "Enter ESXi SSH Credentials"
        }
        else {
            Write-Warning -Message "`tPosh-SSH Module missing. Will not be able to retrieve MCU revision"
            Write-Warning -Message "`tPosh-SSH Module can be installed directly from the PowerShell Gallery"
            Write-Warning -Message "`tSee https://github.com/darkoperator/Posh-SSH for more information"
            $poshSSH = $false
        } #END if/else
    }
    else {
        Write-Verbose -Message ((Get-Date -Format G) + "`t-UseSSH switch was not specified.")
        $poshSSH = $false
    } #END if/else

    <#
      Validate if inputBiosFile/inputMcuFile parameter was specified
      and if the CSV file(s) is reachable
    #>

    if ([string]::IsNullOrWhiteSpace($inputBiosFile)) {
        Write-Verbose -Message ((Get-Date -Format G) + "`t-inputBiosFile parameter is Null or Empty")
        Write-Verbose -Message ((Get-Date -Format G) + "`tValidating access to online CSV file")
        try {
            $webRequest = Invoke-WebRequest -Uri $biosUrl
        } 
        catch [System.Net.WebException] {
            $webRequest = $_.Exception.Response
        } #END try
        if ([int]$webRequest.StatusCode -eq "200") {
            $biosCsvCollection = $webRequest | ConvertFrom-Csv
        }
        else {
            Write-Warning -Message ("`tonline CSV file: '$biosUrl' is NOT reachable/unavailable (Return code: " + ([int]$webRequest.StatusCode) + ")")
            Write-Warning -Message "`tYou can download the file locally and use it offline with the -inputBiosFile parameter. See help for more information"
            Write-Warning -Message "`tSkipping BIOS Compliance check..."
        } #END if/else
    }
    else {
        if (Test-Path $inputBiosFile) {
            Write-Verbose -Message ((Get-Date -Format G) + "`t'$inputBiosFile' path found")
            $biosCsvCollection = Import-Csv -Path $inputBiosFile
        }
        else {
            Write-Warning -Message "`t'$inputBiosFile' path not found."
            Write-Warning -Message "`tSkipping BIOS Compliance check..."
        } #END if/else
    } #END if/else
    if ([string]::IsNullOrWhiteSpace($inputMcuFile)) {
        Write-Verbose -Message ((Get-Date -Format G) + "`t-inputMcuFile parameter is Null or Empty")
        Write-Verbose -Message ((Get-Date -Format G) + "`tValidating access to online CSV file")
        try {
            $webRequest = Invoke-WebRequest -Uri $mcuUrl
        } 
        catch [System.Net.WebException] {
            $webRequest = $_.Exception.Response
        } #END try
        if ([int]$webRequest.StatusCode -eq "200") {
            $mcuCsvCollection = $webRequest | ConvertFrom-Csv
        }
        else {
            Write-Warning -Message ("`tonline CSV file: '$mcuUrl' is NOT reachable/unavailable (Return code: " + ([int]$webRequest.StatusCode) + ")")
            Write-Warning -Message "`tYou can download the file locally and use it offline with the -inputMcuFile parameter. See help for more information"
            Write-Warning -Message "`tSkipping Intel MCU Compliance check..."
        } #END if/else
    }
    else {
        if (Test-Path $inputMcuFile) {
            Write-Verbose -Message ((Get-Date -Format G) + "`t'$inputMcuFile' path found")
            $mcuCsvCollection = Import-Csv -Path $inputMcuFile
        }
        else {
            Write-Warning -Message "`t'$inputMcuFile' path not found."
            Write-Warning -Message "`tSkipping Intel MCU Compliance check..."
        } #END if/else
    } #END if/else
    
    <#
      Main code execution
    #>

    foreach ($vmhost in $vHostList) {
    
        <#
          Skip if ESXi host is not in a Connected
          or Maintenance ConnectionState
        #>

        Write-Verbose -Message ((Get-Date -Format G) + "`t$vmhost Connection State: " + $vmhost.ConnectionState)
        if ($vmhost.ConnectionState -eq "Connected" -or $vmhost.ConnectionState -eq "Maintenance") {
            <#
              Do nothing - ESXi host is reachable
            #>

        }
        else {
            <#
              Use a custom object to keep track of skipped
              hosts and continue to the next foreach loop
            #>

            $skipCollection += [PSCustomObject]@{
                'Hostname'         = $vmhost.Name
                'Connection State' = $vmhost.ConnectionState
            } #END [PSCustomObject]
            continue
        } #END if/else

        <#
          Get ESXi CPUID details
        #>

        Write-Host "`tGathering details from $vmhost ..."
        $esxcli = Get-EsxCli -VMHost $vmhost -V2
        $vmhostView = $vmhost | Get-View
        $mgmtIP = $vmhost | Get-VMHostNetworkAdapter | Where-Object {$_.ManagementTrafficEnabled -eq 'True'} | Select-Object -ExpandProperty IP
        $cpuList = $esxcli.hardware.cpu.list.Invoke() | Where-Object {$_.Id -eq "0"}
        $cpuModel = $vmhost.ProcessorType -replace '\s+', ' '
        Write-Verbose -Message ((Get-Date -Format G) + "`tGathering ESXi CPUID details...")
        $hostCpuPcid = $false
        $hostMcuCPuid = $null
        $hostFeatureCapability = $vmhostView.Config.FeatureCapability
        $hostCpuid = $hostFeatureCapability | Where-Object {$_.FeatureName -eq "cpuid.IBRS" -and $_.Value -eq "1" -or $_.Featurename -eq "cpuid.IBPB" -and $_.Value -eq "1" -or $_.Featurename -eq "cpuid.STIBP" -and $_.Value -eq "1"} | Select-Object -ExpandProperty FeatureName
        $hostInvPcid = $hostFeatureCapability | Where-Object {$_.FeatureName -eq "cpuid.INVPCID" -and $_.Value -eq "1"} | Select-Object -ExpandProperty FeatureName
        $hostPcid = $hostFeatureCapability | Where-Object {$_.Featurename -eq "cpuid.PCID" -and $_.Value -eq "1"} | Select-Object -ExpandProperty FeatureName
        if ($hostInvPcid -and $hostPcid) {
            $hostCpuPcid = $true
        } #End if
        if ($hostCpuid) {
            $hostMcuCPuid = (@($hostCpuid.split('.') | Where-Object {$_ -ne "cpuid"}) -join ',')
        } #END if
        
        <#
          Get accurate last patched date if ESXi 6.5
          based on Date and time (UTC), which is
          converted to local time
        #>

        Write-Verbose -Message ((Get-Date -Format G) + "`tGathering last patched date...")
        $esxPatches = $esxcli.software.vib.list.Invoke()
        $vmhostPatch = $esxPatches | Where-Object {$_.ID -match $vmhost.Build} | Select-Object -First 1
        if ($vmhost.ApiVersion -notmatch '6.5') {
            $lastPatched = Get-Date $vmhostPatch.InstallDate -Format d
        }
        else {
            Write-Verbose -Message ((Get-Date -Format G) + "`tESXi version " + $vmhost.ApiVersion + ". Gathering VIB " + $vmhostPatch.Name + " install date through ImageConfigManager" )
            $configManagerView = Get-View $vmhostView.ConfigManager.ImageConfigManager
            $softwarePackages = $configManagerView.fetchSoftwarePackages() | Where-Object {$_.CreationDate -ge $vmhostPatch.InstallDate}
            $dateInstalledUTC = ($softwarePackages | Where-Object {$_.Name -eq $vmhostPatch.Name -and $_.Version -eq $vmhostPatch.Version}).CreationDate
            $lastPatched = Get-Date ($dateInstalledUTC.ToLocalTime()) -Format d
        } #END if/else
        
        <#
          Get ESXi VMSA-2018-0004.3
          path details
        #>

        $esxFrameworkStatus = $null
        $esxv2McuStatus = $null
        if ($vmhost.ApiVersion -eq "5.5") {
            $esxFrameworkStatus = "Framework Missing"
            $esxv2McuStatus = "v2 MCU Missing"
            if ($vmhost.Build -ge $esx55Build) {
                $esxFrameworkPatch = $esxPatches | Where-Object {$_.Name -eq "esx-base"}
                $esxv2McuPatch = $esxPatches | Where-Object {$_.Name -eq "cpu-microcode"}
                if ($esxFrameworkPatch) {
                    if (($esxFrameworkPatch.Version.Split('.') | Select-Object -Last 1) -ge $esx55Build) {
                        $esxFrameworkStatus = "Framework Installed"
                    } #END if
                } #END if
                if ($esxv2McuPatch) {
                    if (($esxv2McuPatch.Version.Split('.') | Select-Object -Last 1) -ge $esx55Build) {
                        $esxv2McuStatus = "v2 MCU Installed"
                    } #END if
                } #END if
            } #END if
        } #END if
        if ($vmhost.ApiVersion -eq "6.0") {
            $esxFrameworkStatus = "Framework Missing"
            $esxv2McuStatus = "v2 MCU Missing"
            if ($vmhost.Build -ge $esx60Build) {
                $esxFrameworkPatch = $esxPatches | Where-Object {$_.Name -eq "esx-base"}
                $esxv2McuPatch = $esxPatches | Where-Object {$_.Name -eq "cpu-microcode"}
                if ($esxFrameworkPatch) {
                    if (($esxFrameworkPatch.Version.Split('.') | Select-Object -Last 1) -ge $esx60Build) {
                        $esxFrameworkStatus = "Framework Installed"
                    } #END if
                } #END if
                if ($esxv2McuPatch) {
                    if (($esxv2McuPatch.Version.Split('.') | Select-Object -Last 1) -ge $esx60Build) {
                        $esxv2McuStatus = "v2 MCU Installed"
                    } #END if
                } #END if
            } #END if
        } #END if
        if ($vmhost.ApiVersion -eq "6.5") {
            $esxFrameworkStatus = "Framework Missing"
            $esxv2McuStatus = "v2 MCU Missing"
            if ($vmhost.Build -ge $esx65Build) {
                $esxFrameworkPatch = $esxPatches | Where-Object {$_.Name -eq "esx-base"}
                $esxv2McuPatch = $esxPatches | Where-Object {$_.Name -eq "cpu-microcode"}
                if ($esxFrameworkPatch) {
                    if (($esxFrameworkPatch.Version.Split('.') | Select-Object -Last 1) -ge $esx65Build) {
                        $esxFrameworkStatus = "Framework Installed"
                    } #END if
                } #END if
                if ($esxv2McuPatch) {
                    if (($esxv2McuPatch.Version.Split('.') | Select-Object -Last 1) -ge $esx65Build) {
                        $esxv2McuStatus = "v2 MCU Installed"
                    } #END if
                } #END if
            } #END if
        } #END if

        <#
          Get UpTime
        #>

        Write-Verbose -Message ((Get-Date -Format G) + "`tGathering ESXi UpTime details...")
        $bootTimeUTC = $vmhostView.Runtime.BootTime
        $bootTime = $bootTimeUTC.ToLocalTime()
        $upTime = New-TimeSpan -Seconds $vmhostView.Summary.QuickStats.Uptime
        $upTimeDays = $upTime.Days
        $upTimeHours = $upTime.Hours
        $upTimeMinutes = $upTime.Minutes

        <#
          Get ESXi MCU Revisions
        #>

        $mcuUpdate = $null
        $mcuOriginal = $null
        $mcuCurrent = $null
        $biosReleaseDate = $null
        if ($poshSSH) {
            Write-Verbose -Message ((Get-Date -Format G) + "`tGathering ESXi MCU revisions and BIOS release date through SSH...")
            $sshServerFWException = $vmhost | Get-VMHostFirewallException -Name "SSH Server"
            if ($sshServerFWException.Enabled -eq $false) {
                $sshServerFWException | Set-VMHostFirewallException -Enabled $true -Confirm:$false | Out-Null
            } #END if
            $vmServices = $vmhost | Get-VMHostService
            $vmServices | Where-Object {$_.Key -eq "TSM-SSH"} | Start-VMHostService -Confirm:$false | Out-Null
            $sshSession = New-SSHSession -ComputerName $vmhost -Credential $rootCreds -AcceptKey -ConnectionTimeout 90 -KeepAliveInterval 5 -ErrorAction SilentlyContinue
            if ($sshSession) {
                Write-Verbose -Message ((Get-Date -Format G) + "`tSSH session established...")
                $sshCommand = Invoke-SSHCommand -Command "vsish -e cat /hardware/cpu/cpuList/0 | grep microcode -A 2" -SessionId $sshSession.SessionId
                if ($sshCommand.ExitStatus -eq '0') {
                    $mcuUpdate = ($sshCommand.Output | Where-Object {$_ -match "Number of microcode"}).Split(':')[1]
                    $mcuOriginal = ($sshCommand.Output | Where-Object {$_ -match "Original Revision"}).Split(':')[1]
                    $mcuCurrent = ($sshCommand.Output | Where-Object {$_ -match "Current Revision"}).Split(':')[1]
                } #END if
                $sshCommand = Invoke-SSHCommand -Command "esxcfg-info --hardware | grep -i 'bios releasedate'" -SessionId $sshSession.SessionId
                if ($sshCommand.ExitStatus -eq '0') {
                    Write-Verbose -Message ((Get-Date -Format G) + $sshCommand.Output)
                    $intDate = @()
                    $stringDate = (($sshCommand.Output).Split('.') | Select-Object -Last 1 -ErrorAction SilentlyContinue).Split('T')[0]
                    foreach ($string in $stringDate.Split('-')) {
                        $intDate += [int]$string
                    } #END foreach
                    $biosReleaseDate = Get-Date (@($intDate) -join '/') -ErrorAction SilentlyContinue
                } #END if
                Remove-SSHSession $sshSession | Out-Null
            }
            else {
                Write-Warning -Message ("`tFailed to establish SSH connection: " + $Error[0].Exception.Message)
            } #END if/else
            $vmServices | Where-Object {$_.Key -eq "TSM-SSH"} | Stop-VMHostService -Confirm:$false | Out-Null
            if ($mcuUpdate -eq "0") {
                $mcuUpdate = "Inactive"
            }
            elseif ($mcuUpdate -eq "1") {
                $mcuUpdate = "Active"
            } #END if/elseif
        } #END if
        if ($biosReleaseDate) {
            Write-Verbose -Message ((Get-Date -Format G) + "`tBIOS release date was gathered through SSH...")
        }
        else {
            if ($vmhostView.Hardware.BiosInfo.ReleaseDate) {
                $biosReleaseDate = Get-Date $vmhostView.Hardware.BiosInfo.ReleaseDate
            }
            else {
                Write-Verbose -Message ((Get-Date -Format G) + "`tFailed to gather BIOS release date, its null...")
            } #END if/else
        } #END if/else
        
        <#
          Check for BIOS
          version details
        #>
        
        Write-Verbose -Message ((Get-Date -Format G) + "`tGathering BIOS details...")
        if ($biosCsvCollection) {
            $minVersion = $biosCsvCollection | Where-Object {$_.Model -eq $vmhost.Model}
            if ($minVersion) {

                <#
                  If HP compare against Release Date
                  else compare against BIOS version
                #>

                if ($minVersion.Manufacturer -like "HP*") {
                    $biosCsvDate = [DateTime]$minVersion.BIOSReleaseDate
                    if ($biosReleaseDate -ge $biosCsvDate) {
                        $biosComplianceStatus = "Proper BIOS installed"
                    }
                    else {
                        $biosComplianceStatus = "BIOS update available - install v" + $minVersion.BIOS + " (" + $minVersion.BIOSReleaseDate + ")"
                    } #END if/else
                }
                else {
                    if ($vmhostView.Hardware.BiosInfo.BiosVersion -ge $minVersion.BIOS) {
                        $biosComplianceStatus = "Proper BIOS installed"
                    }
                    else {
                        $biosComplianceStatus = "BIOS update available - install v" + $minVersion.BIOS
                    } #END if/else
                } #END if/else
            }
            else {
                $biosComplianceStatus = "Unknown - Check with manufacturer"
            } #END if/else
        } #END if
        if ($biosReleaseDate) {
            $biosReleaseDate = $biosReleaseDate.ToShortDateString()
        } #END if
            
        <#
          Check for CPU Signatures
          Intel Sighting
        #>

        Write-Verbose -Message ((Get-Date -Format G) + "`tGathering Intel MCU and sighting details...")
        $cpuidEAX = ($esxcli.hardware.cpu.cpuid.get.Invoke(@{cpu = 0}) | Where-Object {$_.Level -eq 1}).EAX
        $cpuidHEX = [System.Convert]::ToString($cpuidEAX, 16)
        if ($cpuList.Family -eq "6") {
            if ($mcuCsvCollection) {
                $intelMcuList = $mcuCsvCollection | Where-Object {$_.CPUID -eq $cpuidHEX}
                if ($intelMcuList) {
                    $cpuAffected = $null
                    if ($intelMcuList.Count -gt 1) {
                        if ($mcu = $intelMcuList | Where-Object {$cpuModel.Contains($_.Name.Split('')[3])}) {
                            $intelProduct = $mcu.Product
                            $prodStatus = $mcu.Status
                            $affectedMCU = $mcu.AffectedMCU
                            $prodMCU = $mcu.ProductionMCU
                            if ($affectedMCU -eq "N/A") {
                                $cpuAffected = $false
                            }
                            else {
                                if ($mcuCurrent) {
                                    $cpuAffected = $affectedMCU.Contains($mcuCurrent)
                                }
                                else {
                                    $cpuAffected = "Unknown"        
                                } #END if/else
                            } #END if/else
                        }
                        else {
                            Write-Verbose -Message ((Get-Date -Format G) + "`tNo Match found for: $cpuModel")
                            $intelProduct = "Unknown"
                            $prodStatus = "Unknown"
                            $affectedMCU = "Unknown"
                            $prodMCU = "Unknown"
                            $cpuAffected = "Unknown"
                        } #END if/else
                    }
                    else {
                        $intelProduct = $intelMcuList.Product
                        $prodStatus = $intelMcuList.Status
                        $affectedMCU = $intelMcuList.AffectedMCU
                        $prodMCU = $intelMcuList.ProductionMCU
                        if ($affectedMCU -eq "N/A") {
                            $cpuAffected = $false
                        }
                        else {
                            if ($mcuCurrent) {
                                $cpuAffected = $affectedMCU.Contains($mcuCurrent)
                            }
                            else {
                                $cpuAffected = "Unknown"        
                            } #END if/else
                        } #END if/else
                    } #END if/else
                }
                else {
                    Write-Verbose -Message ((Get-Date -Format G) + "`t$cpuModel not found in Intel MCU Guidance list provided.")
                    $intelProduct = "Unknown"
                    $prodStatus = "Unknown"
                    $affectedMCU = "Unknown"
                    $prodMCU = "Unknown"
                    $cpuAffected = "Unknown"
                } #END if/else
            } #END if
        }
        else {
            Write-Verbose -Message ((Get-Date -Format G) + "`tNot an Intel CPU, skipping...")
            $intelProduct = "N/A"
            $prodStatus = "N/A"
            $affectedMCU = "N/A"
            $prodMCU = "N/A"
            $cpuAffected = "Unknown - Check with manufacturer"
        } #END if/Else

        <#
          Check for VMware
          MCU workaround
        #>

        Write-Verbose -Message ((Get-Date -Format G) + "`tGathering details for VMware MCU workaround...")
        $vmhostName = $vmhost.Name
        $url = "https://$vmhostName/host/vmware_config"
        $sessionManager = Get-View ($global:DefaultVIServer.ExtensionData.Content.sessionManager)
        $spec = New-Object VMware.Vim.SessionManagerHttpServiceRequestSpec
        $spec.Method = "httpGet"
        $spec.Url = $url
        $ticket = $sessionManager.AcquireGenericServiceTicket($spec)
        $websession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
        $cookie = New-Object System.Net.Cookie
        $cookie.Name = "vmware_cgi_ticket"
        $cookie.Value = $ticket.id
        $cookie.Domain = $vmhostName
        $websession.Cookies.Add($cookie)
        $result = Invoke-WebRequest -Uri $url -WebSession $websession
        $esxconfig = $result.content
        foreach ($line in $esxconfig.Split("`n")) {
            if ($line -eq 'cpuid.7.edx = "----:00--:----:----:----:----:----:----"') {
                $intelWorkaround = $true
                break
            }
            else {
                $intelWorkaround = $false
            } #END if/else
        } #END foreach

        <#
          Use a custom object to store
          collected data
        #>

        $patchCollection += [PSCustomObject]@{
            'Hostname'                      = $vmhost.Name
            'Management IP'                 = $mgmtIP
            'Cluster'                       = $vmhost.Parent            
            'Product'                       = $vmhostView.Config.Product.Name
            'Version'                       = $vmhostView.Config.Product.Version
            'Build'                         = $vmhost.Build            
            'Last patched'                  = $lastPatched
            'Boot time'                     = $bootTime
            'Uptime'                        = "$upTimeDays Day(s), $upTimeHours Hr(s), $upTimeMinutes Min(s)"            
            'Make'                          = $vmhost.Manufacturer
            'Model'                         = $vmhost.Model
            'CPU model'                     = $cpuModel
            'CPUID'                         = $cpuidHEX
            'Max EVC mode'                  = $vmhost.MaxEVCMode
            'BIOS version'                  = $vmhostView.Hardware.BiosInfo.BiosVersion
            'BIOS release date'             = $biosReleaseDate
            'BIOS guidance'                 = $biosComplianceStatus
            'ESXi guidance'                 = $esxFrameworkStatus + "/" + $esxv2McuStatus
            'MCU CPUID'                     = $hostMcuCPuid
            'PCID/INVPCID'                  = $hostCpuPcid
            'ESXi applied MCU'              = $mcuUpdate
            'MCU BIOS rev.'                 = $mcuOriginal
            'MCU boot rev.'                 = $mcuCurrent
            'Intel product'                 = $intelProduct
            'Intel MCU status'              = $prodStatus
            'Intel MCU(s) at risk'          = $affectedMCU
            'Intel production MCU'          = $prodMCU
            'MCU boot rev. at risk'         = $cpuAffected
            'VMware MCU workaround applied' = $intelWorkaround
        } #END [PSCustomObject]

        <#
          Report on VMs if switch was specified
        #>

        if ($ReportOnVMs) {
            $vmlist = $vmhost | Get-VM | Sort-Object -Property Name
            if ($vmhost.Parent.EVCMode) {
                $clusEvcMode = $vmhost.Parent.EVCMode
            }
            else {
                $clusEvcMode = "Disabled"
            } #END if/else
            foreach ($vm in $vmlist) {
                $vmMcuCpuid = $null
                $vmCpuPcid = $null
                $powerOnEvent = $null
                Write-Host "`tGathering VM hypervisor-assisted guest mitigation details from $vm ..."
                $hardwareVersion = ($vm.Version.ToString()).Split('v')[1]              
                $vmFeatureRequirement = $vm.ExtensionData.Runtime.FeatureRequirement
                $vmCpuid = $vmFeatureRequirement | Where-Object {$_.FeatureName -eq "cpuid.IBRS" -or $_.Featurename -eq "cpuid.IBPB" -or $_.Featurename -eq "cpuid.STIBP"} | Select-Object -ExpandProperty FeatureName
                $vmPcid = $vmFeatureRequirement | Where-Object {$_.FeatureName -eq "cpuid.INVPCID" -or $_.Featurename -eq "cpuid.PCID"} | Select-Object -ExpandProperty FeatureName
                if ($vm.Guest.OSFullName) {
                    $vmGuestOs = $vm.Guest.OSFullName
                }
                else {
                    $vmGuestOs = $vm.ExtensionData.Config.GuestFullName
                } #END if

                <#
                  Validate VM Hardware
                #>

                Write-Verbose -Message ((Get-Date -Format G) + "`tValidating VM Hardware")
                if ([int]$hardwareVersion -ge "9" -and $vm.PowerState -eq "PoweredOn") {
                    if ($hostCpuid) {
                        if ($vmCpuid) {
                            $spectreStatus = "Supported/Enabled"
                        }
                        else {
                            $spectreStatus = "Supported/Disabled"
                        } #END if/else
                    }
                    else {
                        $spectreStatus = "NotSupported/Disabled"
                    } #END if/else
                    if ($hostCpuPcid) {
                        if ([int]$hardwareVersion -ge "11") {
                            if ($vmPcid.Count -eq "2") {
                                $pcidStatus = "Supported/Enabled"
                            }
                            else {
                                $pcidStatus = "Supported/Disabled"
                            } #END if/else
                        }
                        else {
                            $pcidStatus = "Supported/Upgrade VM Hardware"
                        } #END if/else
                    }
                    else {
                        $pcidStatus = "NotSupported/NA"
                    } #END if/else
                    $powerOnEvents = $vm | Get-VIEvent -MaxSamples ([int]::MaxValue) -Types Info | Where-Object {$_ -is [VMware.Vim.VmPoweredOnEvent]}
                    if ($powerOnEvents) {
                        $sortedEvents = Sort-Object -InputObject $powerOnEvents -Property CreatedTime -Descending
                        $powerOnEvent = ($sortedEvents | Select-Object -First 1).CreatedTime
                    } #END if
                }
                else {
                    if ([int]$hardwareVersion -ge "9" -and $vm.PowerState -eq "PoweredOff") {
                        $spectreStatus = "UnknownSinceOff"
                        $pcidStatus = "UnknownSinceOff"
                    }
                    else {
                        $spectreStatus = "Supported/Upgrade VM Hardware"
                        if ($hostCpuPcid) {
                            $pcidStatus = "Supported/Upgrade VM Hardware"
                        }
                        else {
                            $pcidStatus = "NotSupported/NA"
                        } #END if
                    } #END if/else
                } #END if/else
                if ($vmCpuid) {
                    $vmMcuCpuid = (@($vmCpuid.split('.') | Where-Object {$_ -ne "cpuid"}) -join ',')
                } #END if/else
                if ($vmPcid) {
                    $vmCpuPcid = (@($vmPcid.split('.') | Where-Object {$_ -ne "cpuid"}) -join ',')
                } #END if/else

                <#
                  Use a custom object to store
                  collected data
                #>

                $vmCollection += [PSCustomObject]@{
                    'Name'                                 = $vm.Name
                    'Power state'                          = $vm.PowerState
                    'VM guest OS'                          = $vmGuestOs
                    'ESXi'                                 = $vmhost.Name
                    'Cluster'                              = $vmhost.Parent
                    'Cluster EVC mode'                     = $clusEvcMode
                    'Hardware version'                     = $vm.Version
                    'ESXi MCU CPUID'                       = $hostMcuCPuid
                    'ESXi PCID/INVPCID'                    = $hostCpuPcid
                    'VM MCU CPUID'                         = $vmMcuCpuid
                    'VM PCID/INVPCID'                      = $vmCpuPcid
                    'Last PoweredOn'                       = $powerOnEvent
                    'Hypervisor-Assisted Guest mitigation' = $spectreStatus
                    'PCID optimization'                    = $pcidStatus
                } #END [PSCustomObject]
            } #END foreach
        } #END if
    } #END foreach
    Write-Verbose -Message ((Get-Date -Format G) + "`tMain code execution completed")
    
    <#
      Display skipped hosts and their connection status
    #>

    If ($skipCollection) {
        Write-Warning -Message "`tCheck Connection State or Host name"
        Write-Warning -Message "`tSkipped hosts:"
        $skipCollection | Format-Table -AutoSize
    } #END if
    
    <#
      Validate output arrays
    #>

    if ($patchCollection -or $vmCollection) {
        Write-Verbose -Message ((Get-Date -Format G) + "`tInformation gathered")
    }
    else {
        Write-Verbose -Message ((Get-Date -Format G) + "`tNo information gathered")
    } #END if/else
    
    <#
      Output to screen
      Export data to CSV, Excel
    #>

    if ($patchCollection) {
        Write-Host "`n" "ESXi Speculative Execution:" -ForegroundColor Green
        if ($ExportCSV) {
            $patchCollection | Export-Csv ($outputFile + "ESXi.csv") -NoTypeInformation
            Write-Host "`tData exported to" ($outputFile + "ESXi.csv") "file" -ForegroundColor Green
        }
        elseif ($ExportExcel) {
            $patchCollection | Export-Excel ($outputFile + ".xlsx") -WorkSheetname ESXi -NoNumberConversion * -AutoSize -BoldTopRow
            Write-Host "`tData exported to" ($outputFile + ".xlsx") "file" -ForegroundColor Green
        }
        elseif ($PassThru) {
            $returnCollection += $patchCollection 
            $returnCollection 
        }
        else {
            $patchCollection | Format-List
        }#END if/else
    } #END if

    if ($vmCollection) {
        Write-Host "`n" "VM Hypervisor-Assisted Guest Mitigation:" -ForegroundColor Green
        if ($ExportCSV) {
            $VMCollection | Export-Csv ($outputFile + "VM.csv") -NoTypeInformation
            Write-Host "`tData exported to" ($outputFile + "VM.csv") "file" -ForegroundColor Green
        }
        elseif ($ExportExcel) {
            $VMCollection | Export-Excel ($outputFile + ".xlsx") -WorkSheetname VM -NoNumberConversion * -BoldTopRow
            Write-Host "`tData exported to" ($outputFile + ".xlsx") "file" -ForegroundColor Green
        }
        elseif ($PassThru) {
            $returnCollection += $VMCollection
            $returnCollection  
        }
        else {
            $vmCollection | Format-List
        }#END if/else
    } #END if
} #END function