|
| 1 | +$MemoryManagementPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management' |
| 2 | +$VirtualizationPath = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization' |
| 3 | + |
| 4 | +Function Get-TargetResource |
| 5 | +{ |
| 6 | + [OutputType([System.Collections.Hashtable])] |
| 7 | + [CMDLetBinding()] |
| 8 | + param |
| 9 | + ( |
| 10 | + # Parameter help description |
| 11 | + [Parameter(Mandatory = $true)] |
| 12 | + [ValidateSet('Enabled', 'Disabled')] |
| 13 | + [String] |
| 14 | + $Status |
| 15 | + ) |
| 16 | + |
| 17 | + $getTargetResourceResult = $null |
| 18 | + |
| 19 | + # Test if the fixes are enabled |
| 20 | + $FeatureSettingsOverrideEnabled = Test-RegistryItem -Path $MemoryManagementPath -Name 'FeatureSettingsOverride' -ExpectedValue 0 |
| 21 | + Write-Verbose -Message ('FeatureSettingsOverride is {0}' -f $FeatureSettingsOverrideEnabled) |
| 22 | + |
| 23 | + $FeatureSettingsOverrideMaskEnabled = Test-RegistryItem -Path $MemoryManagementPath -Name 'FeatureSettingsOverrideMask' -ExpectedValue 3 |
| 24 | + Write-Verbose -Message ('FeatureSettingsOverrideMask is {0}' -f $FeatureSettingsOverrideMaskEnabled) |
| 25 | + |
| 26 | + $MinVmVersionForCpuBasedMitigationsEnabled = Test-RegistryItem -Path $VirtualizationPath -Name 'MinVmVersionForCpuBasedMitigations' -ExpectedValue '1.0' |
| 27 | + Write-Verbose -Message ('MinVmVersionForCpuBasedMitigations is {0}' -f $MinVmVersionForCpuBasedMitigationsEnabled) |
| 28 | + |
| 29 | + if ($FeatureSettingsOverrideEnabled -and $FeatureSettingsOverrideMaskEnabled -and $MinVmVersionForCpuBasedMitigationsEnabled) |
| 30 | + { |
| 31 | + $Status = 'Enabled' |
| 32 | + } |
| 33 | + else |
| 34 | + { |
| 35 | + $Status = 'Disabled' |
| 36 | + } |
| 37 | + |
| 38 | + $getTargetResourceResult = @{ |
| 39 | + Status = $Status |
| 40 | + } |
| 41 | + |
| 42 | + $getTargetResourceResult |
| 43 | +} |
| 44 | + |
| 45 | +Function Set-TargetResource |
| 46 | +{ |
| 47 | + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] |
| 48 | + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '')] |
| 49 | + [CMDLetBinding(SupportsShouldProcess=$true)] |
| 50 | + param |
| 51 | + ( |
| 52 | + # Parameter help description |
| 53 | + [Parameter(Mandatory = $true)] |
| 54 | + [ValidateSet('Enabled', 'Disabled')] |
| 55 | + [String] |
| 56 | + $Status |
| 57 | + ) |
| 58 | + |
| 59 | + if ($Status -eq 'Enabled') |
| 60 | + { |
| 61 | + Write-Verbose -Message 'Enabling Protections' |
| 62 | + if ($PSCmdlet.ShouldProcess('Enable cSpeculationControlFix', 'Set-TargetResource')) { |
| 63 | + Update-RegistryItem -Path $MemoryManagementPath -Name 'FeatureSettingsOverride' -Value 0 -PropertyType 'DWORD' -Confirm:$false |
| 64 | + Update-RegistryItem -Path $MemoryManagementPath -Name 'FeatureSettingsOverrideMask' -Value 3 -PropertyType 'DWORD' -Confirm:$false |
| 65 | + Update-RegistryItem -Path $VirtualizationPath -Name 'MinVmVersionForCpuBasedMitigations' -Value '1.0' -PropertyType 'STRING' -Confirm:$false |
| 66 | + } |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + Write-Verbose -Message 'Disabling Protections' |
| 71 | + if ($PSCmdlet.ShouldProcess('Disable cSpeculationControlFix', 'Set-TargetResource')) { |
| 72 | + Update-RegistryItem -Path $MemoryManagementPath -Name 'FeatureSettingsOverride' -Value 3 -PropertyType 'DWORD' -Confirm:$false |
| 73 | + Update-RegistryItem -Path $MemoryManagementPath -Name 'FeatureSettingsOverrideMask' -Value 3 -PropertyType 'DWORD' -Confirm:$false |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + # Setting the global:DSCMachineStatus = 1 tells DSC that a reboot is required |
| 78 | + $global:DSCMachineStatus = 1 |
| 79 | +} |
| 80 | + |
| 81 | +Function Test-TargetResource |
| 82 | +{ |
| 83 | + [CMDLetBinding()] |
| 84 | + [OutputType([bool])] |
| 85 | + param |
| 86 | + ( |
| 87 | + # Parameter help description |
| 88 | + [Parameter(Mandatory = $true)] |
| 89 | + [ValidateSet('Enabled', 'Disabled')] |
| 90 | + [String] |
| 91 | + $Status |
| 92 | + ) |
| 93 | + |
| 94 | + if ($Status -eq 'Enabled') |
| 95 | + { |
| 96 | + $FeatureSettingsOverrideEnabled = Test-RegistryItem -Path $MemoryManagementPath -Name 'FeatureSettingsOverride' -ExpectedValue 0 |
| 97 | + Write-Verbose -Message ('FeatureSettingsOverride is {0}' -f $FeatureSettingsOverrideEnabled) |
| 98 | + |
| 99 | + $FeatureSettingsOverrideMaskEnabled = Test-RegistryItem -Path $MemoryManagementPath -Name 'FeatureSettingsOverrideMask' -ExpectedValue 3 |
| 100 | + Write-Verbose -Message ('FeatureSettingsOverrideMask is {0}' -f $FeatureSettingsOverrideMaskEnabled) |
| 101 | + |
| 102 | + $MinVmVersionForCpuBasedMitigationsEnabled = Test-RegistryItem -Path $VirtualizationPath -Name 'MinVmVersionForCpuBasedMitigations' -ExpectedValue '1.0' |
| 103 | + Write-Verbose -Message ('MinVmVersionForCpuBasedMitigations is {0}' -f $MinVmVersionForCpuBasedMitigationsEnabled) |
| 104 | + |
| 105 | + $FixStatus = $FeatureSettingsOverrideEnabled -and $FeatureSettingsOverrideMaskEnabled -and $MinVmVersionForCpuBasedMitigationsEnabled |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + $FeatureSettingsOverrideEnabled = Test-RegistryItem -Path $MemoryManagementPath -Name 'FeatureSettingsOverride' -ExpectedValue 3 |
| 110 | + Write-Verbose -Message ('FeatureSettingsOverride is {0}' -f $FeatureSettingsOverrideEnabled) |
| 111 | + |
| 112 | + $FeatureSettingsOverrideMaskEnabled = Test-RegistryItem -Path $MemoryManagementPath -Name 'FeatureSettingsOverrideMask' -ExpectedValue 3 |
| 113 | + Write-Verbose -Message ('FeatureSettingsOverrideMask is {0}' -f $FeatureSettingsOverrideMaskEnabled) |
| 114 | + |
| 115 | + $FixStatus = $FeatureSettingsOverrideEnabled -and $FeatureSettingsOverrideMaskEnabled |
| 116 | + } |
| 117 | + |
| 118 | + Write-Verbose -Message ('cSpeculationControlFix should be {0} = {1}' -f $Status, $FixStatus) |
| 119 | + $FixStatus |
| 120 | +} |
0 commit comments