diff --git a/CHANGELOG.md b/CHANGELOG.md index 7139809..bae60e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +- Fixed issue causing Assert-HasPrereqsForBitlocker to fail on Windows 10. + Credit: https://github.com/goldfinger2 + ([issue #47](https://github.com/PowerShell/xBitlocker/issues/47)) + ## 1.4.0.0 - Change double quoted string literals to single quotes diff --git a/DSCResources/MSFT_xBLAutoBitlocker/MSFT_xBLAutoBitlocker.psm1 b/DSCResources/MSFT_xBLAutoBitlocker/MSFT_xBLAutoBitlocker.psm1 index ee184c4..b103577 100644 --- a/DSCResources/MSFT_xBLAutoBitlocker/MSFT_xBLAutoBitlocker.psm1 +++ b/DSCResources/MSFT_xBLAutoBitlocker/MSFT_xBLAutoBitlocker.psm1 @@ -367,7 +367,9 @@ function Set-TargetResource else { Remove-FromPSBoundParametersUsingHashtable -PSBoundParametersIn $PSBoundParameters -ParamsToRemove 'DriveType', 'MinDiskCapacityGB' - Add-ToPSBoundParametersFromHashtable -PSBoundParametersIn $PSBoundParameters -ParamsToAdd @{'MountPoint' = ''} + Add-ToPSBoundParametersFromHashtable -PSBoundParametersIn $PSBoundParameters -ParamsToAdd @{ + 'MountPoint' = '' + } # Loop through each potential AutoBitlocker volume, see whether they are enabled for Bitlocker, and if not, enable it foreach ($key in $autoBlVols.Keys) @@ -575,7 +577,9 @@ function Test-TargetResource else { Remove-FromPSBoundParametersUsingHashtable -PSBoundParametersIn $PSBoundParameters -ParamsToRemove 'DriveType', 'MinDiskCapacityGB' - Add-ToPSBoundParametersFromHashtable -PSBoundParametersIn $PSBoundParameters -ParamsToAdd @{'MountPoint' = ''} + Add-ToPSBoundParametersFromHashtable -PSBoundParametersIn $PSBoundParameters -ParamsToAdd @{ + 'MountPoint' = '' + } # Check whether any potential AutoBitlocker volume is not currently enabled for Bitlocker, or doesn't have the correct settings foreach ($key in $autoBlVols.Keys) diff --git a/Misc/xBitlockerCommon.psm1 b/Misc/xBitlockerCommon.psm1 index 814b156..3e863e2 100644 --- a/Misc/xBitlockerCommon.psm1 +++ b/Misc/xBitlockerCommon.psm1 @@ -788,29 +788,42 @@ function Assert-HasPrereqsForBitlocker $hasAllPreReqs = $true - $blFeature = Get-WindowsFeature BitLocker - $blAdminToolsFeature = Get-WindowsFeature RSAT-Feature-Tools-BitLocker - $blAdminToolsRemoteFeature = Get-WindowsFeature RSAT-Feature-Tools-BitLocker-RemoteAdminTool - - if ($blFeature.InstallState -ne 'Installed') + if ((Get-OSEdition) -like 'Client') { - $hasAllPreReqs = $false - - Write-Error 'The Bitlocker feature needs to be installed before the xBitlocker module can be used' + $blFeature = Get-WindowsOptionalFeature -Online -FeatureName 'BitLocker' + if ($blFeature.State -ne 'Enabled') + { + $hasAllPreReqs = $false + Write-Error -Message 'The Bitlocker feature needs to be installed before the xBitlocker module can be used' + } } - - if ($blAdminToolsFeature.InstallState -ne 'Installed') + else { - $hasAllPreReqs = $false + $blFeature = Get-WindowsFeature -Name 'BitLocker' + $blAdminToolsFeature = Get-WindowsFeature -Name 'RSAT-Feature-Tools-BitLocker' + $blAdminToolsRemoteFeature = Get-WindowsFeature -Name 'RSAT-Feature-Tools-BitLocker-RemoteAdminTool' - Write-Error 'The RSAT-Feature-Tools-BitLocker feature needs to be installed before the xBitlocker module can be used' - } + if ($blFeature.InstallState -ne 'Installed') + { + $hasAllPreReqs = $false - if ($blAdminToolsRemoteFeature.InstallState -ne 'Installed' -and (Get-OSEdition) -notmatch 'Core') - { - $hasAllPreReqs = $false + Write-Error -Message 'The Bitlocker feature needs to be installed before the xBitlocker module can be used' + } + + if ($blAdminToolsFeature.InstallState -ne 'Installed') + { + $hasAllPreReqs = $false + + Write-Error -Message 'The RSAT-Feature-Tools-BitLocker feature needs to be installed before the xBitlocker module can be used' + } + + if ($blAdminToolsRemoteFeature.InstallState -ne 'Installed' -and (Get-OSEdition) -notmatch 'Core') + { + $hasAllPreReqs = $false + + Write-Error -Message 'The RSAT-Feature-Tools-BitLocker-RemoteAdminTool feature needs to be installed before the xBitlocker module can be used' + } - Write-Error 'The RSAT-Feature-Tools-BitLocker-RemoteAdminTool feature needs to be installed before the xBitlocker module can be used' } if ($hasAllPreReqs -eq $false) diff --git a/Tests/Unit/xBitlockerCommon.tests.ps1 b/Tests/Unit/xBitlockerCommon.tests.ps1 index e52cd6f..d5ec804 100644 --- a/Tests/Unit/xBitlockerCommon.tests.ps1 +++ b/Tests/Unit/xBitlockerCommon.tests.ps1 @@ -221,7 +221,16 @@ try { param ( - [string] + [String] + $FeatureName + ) + } + + function Get-WindowsOptionalFeature + { + param + ( + [String] $FeatureName ) } @@ -292,6 +301,38 @@ try } } + Context 'When OS is a Windows client' { + Mock -CommandName Get-OSEdition -MockWith { + return 'Client' + } + + Mock -CommandName Get-WindowsOptionalFeature -MockWith { + param + ( + [Switch] + $Online = $false, + [String] + $FeatureName + ) + + return @{ + DisplayName = $FeatureName + Name = $FeatureName + State = 'Enabled' + } + } + + It 'Should not generate any error messages' { + Mock -CommandName Write-Error + Assert-HasPrereqsForBitlocker + Assert-MockCalled -Command Write-Error -Exactly -Times 0 -Scope It + } + + It 'Should run the Assert-HasPrereqsForBitlocker function without exceptions' { + {Assert-HasPrereqsForBitlocker} | Should -Not -Throw + } + } + Context 'When OS is Full Server without the required features installed' { Mock -CommandName Get-OSEdition -MockWith { return 'Server' @@ -422,6 +463,22 @@ try $OSVersion | Should -Be 'Server' } + It 'Should return "Client" if the OS is a Windows Client' { + Mock -CommandName Get-ItemProperty -MockWith { + [PSCustomObject] @{ + InstallationType = 'Client' + PSPath = 'Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\software\microsoft\windows nt\currentversion' + PSParentPath = 'Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\software\microsoft\windows nt' + PSChildName = 'currentversion' + PSDrive = 'HKLM' + PSProvider = 'Microsoft.PowerShell.Core\Registry' + } + } + + $OSVersion = Get-OSEdition + $OSVersion | Should -Be 'Client' + } + It 'Should run without exceptions' { Mock -CommandName Get-ItemProperty -MockWith { [PSCustomObject] @{