Public/Get-VMSpeculativeExecution.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
function Get-VMSpeculativeExecution {
    <#
     .SYNOPSIS
       Get VM compliance on VMSA-2018-0004 Security Advisory
     .DESCRIPTION
       Will validate VM for VMware Security Advisory VMSA-2018-0004 Compliance (https://www.vmware.com/us/security/advisories/VMSA-2018-0004.html)
     .NOTES
       File Name : Get-VMSpeculativeExecution.ps1
       Author : Edgar Sanchez - @edmsanchez13
       Contributor : Ariel Sanchez - @arielsanchezmor
       Version : 2.4.3
     .Link
       https://github.com/arielsanchezmora/vDocumentation
     .PARAMETER VM
       Specifies the virtual machine(s) which you want to validate compliance on.
     .EXAMPLE
       C:\PS> Get-VM -Server $VCServer | Get-VMSpeculativeExecution
 
       Retrieves the compliance status on Spectre of all virtual machines which run in the $VCServer vCenter Server.
      
     .EXAMPLE
       C:\PS> Get-VM -Name "labvm001" | Get-VMSpeculativeExecution
 
       Name : labvm001
       Power State : PoweredOn
       Hardware Version : v10
       ESXi Host : labhost010
       ESXi CPUID Features : IBPB,IBRS,STIBP
       VM CPUID Features : STIBP,IBRS,IBPB
       SafeFromSpectre : True
 
     .EXAMPLE
       C:\PS> Get-VM -Location "MyClusterName" | Get-VMSpeculativeExecution
 
       Retrieves the compliance status on Spectre of all virtual machines which run in the "MyClusterName" cluster.
  
     .EXAMPLE
       C:\PS> Get-VMHost "MyESXiHostName" | Get-VM | Get-VMSpeculativeExecution
 
       Retrieves the compliance status on Spectre of all virtual machines which run on the "MyESXiHostName" ESXi host.
      
       .EXAMPLE
      C:\PS> Get-VMHost "MyESXiHostName" | Get-VM | Get-VMSpeculativeExecution | Export-Excel "VMValidation.xlsx" -WorkSheetname "VMresults"
 
       Retrieves the compliance status on Spectre of all virtual machines which run on the "MyESXiHostName" ESXi host and exports to Excel
 
     .NOTES
       This advanced function assumes that you are connected to at least one vCenter Server system.
    #>
 
    
    <#
     ----------------------------------------------------------[Declarations]----------------------------------------------------------
    #>

  
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true,
            ValueFromPipeLine = $true,
            ValueFromPipelinebyPropertyName = $True,
            Position = 0)]
        [ValidateNotNullOrEmpty()]
        [VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]] $VM
    )
     
    <#
      Main code execution
    #>

    Process {
        $vmCollection = @()
        $hostCpuidCollection = @()
        $hostList = $vm | Select-Object -ExpandProperty VMhost -Unique
        foreach ($esxhost in $hostList) {
            $hostCpuidCollection += [PSCustomObject]@{
                'Name'              = $esxhost.Name
                'FeatureCapability' = $esxhost.ExtensionData.Config.FeatureCapability
            } #END [PSCustomObject]
        } #END foreach

        foreach ($oneVM in $VM) {
            $vmhost = $vm.VMHost
            if ($vmhost.Parent.EVCMode) {
                $clusEvcMode = $vmhost.Parent.EVCMode
            }
            else {
                $clusEvcMode = "Disabled"
            } #END if/else
            $hostCpuPcid = $false
            $hostMcuCpuid = $null
            $vmMcuCpuid = $null
            $vmCpuPcid = $null
            $powerOnEvent = $null
            $hardwareVersion = ($oneVM.Version.ToString()).Split('v')[1]
            $hostFeatureCapability = $hostCpuidCollection | Where-Object {$_.Name -eq $oneVM.VMHost.Name} | Select-Object -ExpandProperty 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
            $vmFeatureRequirement = $oneVM.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 ($oneVM.Guest.OSFullName) {
                $vmGuestOs = $oneVM.Guest.OSFullName
            }
            else {
                $vmGuestOs = $oneVM.ExtensionData.Config.GuestFullName
            } #END if
            if ($hostInvPcid -and $hostPcid) {
                $hostCpuPcid = $true
            } #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 ($hostCpuid) {
                $hostMcuCpuid = (@($hostCpuid.split('.') | Where-Object {$_ -ne "cpuid"}) -join ',')
            } #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
            #>

            $info = New-Object PSObject
            $info | Add-Member -type NoteProperty -Name 'Name' -Value $oneVM.Name
            $info | Add-Member -type NoteProperty -Name 'Power state' -Value $oneVM.PowerState
            $info | Add-Member -type NoteProperty -Name 'VM Guest OS' -Value $vmGuestOs
            $info | Add-Member -type NoteProperty -Name 'ESXi Host' -Value $vmhost.Name
            $info | Add-Member -type NoteProperty -Name 'Cluster EVC mode' -Value $clusEvcMode
            $info | Add-Member -type NoteProperty -Name 'Hardware version' -Value $oneVM.Version
            $info | Add-Member -type NoteProperty -Name 'ESXi MCU CPUID' -Value $hostMcuCPuid
            $info | Add-Member -type NoteProperty -Name 'ESXi PCID/INVPCID' -Value $hostCpuPcid
            $info | Add-Member -type NoteProperty -Name 'VM MCU CPUID' -Value $vmMcuCpuid
            $info | Add-Member -type NoteProperty -Name 'VM PCID/INVPCID' -Value $vmCpuPcid
            $info | Add-Member -type NoteProperty -Name 'Last PoweredOn' -Value $powerOnEvent
            $info | Add-Member -type NoteProperty -Name 'Hypervisor-Assisted Guest mitigation' -Value $spectreStatus
            $info | Add-Member -type NoteProperty -Name 'PCID optimization' -Value $pcidStatus

            $vmCollection += $info        
        } #END foreach
        $vmCollection
    } #END process
} #END function