From 2177b0176f907f30b143a51aad10add1aae48e59 Mon Sep 17 00:00:00 2001 From: Mike Nothhard Date: Mon, 24 Jun 2019 22:21:48 +0200 Subject: [PATCH 1/6] Added Windows "Client" OS support for Assert-HasPrereqsForBitlocker function. --- CHANGELOG.md | 3 ++ Misc/xBitlockerCommon.psm1 | 47 ++++++++++++++--------- Tests/Unit/xBitlockerCommon.tests.ps1 | 55 +++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7139809..24f06c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Change log for xBitlocker ## 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 diff --git a/Misc/xBitlockerCommon.psm1 b/Misc/xBitlockerCommon.psm1 index 814b156..f31c7e5 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 BitLocker + if ($blFeature.InstallState -ne 'Installed') + { + $hasAllPreReqs = $false + Write-Error '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 BitLocker + $blAdminToolsFeature = Get-WindowsFeature RSAT-Feature-Tools-BitLocker + $blAdminToolsRemoteFeature = Get-WindowsFeature 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 'The Bitlocker feature needs to be installed before the xBitlocker module can be used' + } + + if ($blAdminToolsFeature.InstallState -ne 'Installed') + { + $hasAllPreReqs = $false + + Write-Error '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 '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..ce6ccb3 100644 --- a/Tests/Unit/xBitlockerCommon.tests.ps1 +++ b/Tests/Unit/xBitlockerCommon.tests.ps1 @@ -226,6 +226,15 @@ try ) } + function Get-WindowsOptionalFeature + { + param + ( + [string] + $FeatureName + ) + } + function Get-OSEdition { @@ -292,6 +301,36 @@ try } } + Context 'When OS is a Windows client' { + Mock -CommandName Get-OSEdition -MockWith { + return 'Client' + } + + Mock -CommandName Get-WindowsOptionalFeature -MockWith { + param + ( + [string] + $FeatureName + ) + + return @{ + DisplayName = $FeatureName + Name = $FeatureName + InstallState = 'Installed' + } + } + + 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 +461,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] @{ From 1fb8d1564533af5cf12af1733c6bb4f789eadac6 Mon Sep 17 00:00:00 2001 From: Mike Nothhard Date: Mon, 24 Jun 2019 22:48:34 +0200 Subject: [PATCH 2/6] Corrected blanks-around-headers for CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24f06c7..bae60e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Change log for xBitlocker ## 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)) From 1cf4da15c8e132d2c273990c31ceb617720d1352 Mon Sep 17 00:00:00 2001 From: Mike Nothhard Date: Fri, 28 Jun 2019 09:58:59 -0400 Subject: [PATCH 3/6] Corrected Get-WindowsOptionalFeature logic for client OS. --- Misc/xBitlockerCommon.psm1 | 4 ++-- Tests/Unit/xBitlockerCommon.tests.ps1 | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Misc/xBitlockerCommon.psm1 b/Misc/xBitlockerCommon.psm1 index f31c7e5..fc8edcb 100644 --- a/Misc/xBitlockerCommon.psm1 +++ b/Misc/xBitlockerCommon.psm1 @@ -790,8 +790,8 @@ function Assert-HasPrereqsForBitlocker if ((Get-OSEdition) -like 'Client') { - $blFeature = Get-WindowsOptionalFeature BitLocker - if ($blFeature.InstallState -ne 'Installed') + $blFeature = Get-WindowsOptionalFeature -Online -FeatureName 'BitLocker' + if ($blFeature.State -ne 'Enabled') { $hasAllPreReqs = $false Write-Error 'The Bitlocker feature needs to be installed before the xBitlocker module can be used' diff --git a/Tests/Unit/xBitlockerCommon.tests.ps1 b/Tests/Unit/xBitlockerCommon.tests.ps1 index ce6ccb3..1e3036a 100644 --- a/Tests/Unit/xBitlockerCommon.tests.ps1 +++ b/Tests/Unit/xBitlockerCommon.tests.ps1 @@ -309,6 +309,8 @@ try Mock -CommandName Get-WindowsOptionalFeature -MockWith { param ( + [switch] + $online = $false, [string] $FeatureName ) @@ -316,7 +318,7 @@ try return @{ DisplayName = $FeatureName Name = $FeatureName - InstallState = 'Installed' + State = 'Enabled' } } From b6971ef3e0679c8c2a5efa23161cdffa51c40ce7 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Tue, 29 Oct 2019 20:26:17 +0100 Subject: [PATCH 4/6] Changes to xBitLocker - Fix hashtables according to the style guideline. --- CHANGELOG.md | 2 ++ .../MSFT_xBLAutoBitlocker/MSFT_xBLAutoBitlocker.psm1 | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7139809..ad60904 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Fix hashtables according to the style guideline. + ## 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) From 19e28541b8aadab8a0ff42a861af845649e9c6c9 Mon Sep 17 00:00:00 2001 From: Mike Nothhard <39061489+mnothh@users.noreply.github.com> Date: Fri, 8 Nov 2019 21:01:58 +0100 Subject: [PATCH 5/6] Update CHANGELOG.md --- CHANGELOG.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2c3891..bae60e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,13 +2,9 @@ ## Unreleased -Win10_Fix_Working_Branch - Fixed issue causing Assert-HasPrereqsForBitlocker to fail on Windows 10. Credit: https://github.com/goldfinger2 ([issue #47](https://github.com/PowerShell/xBitlocker/issues/47)) -======= -- Fix hashtables according to the style guideline. -dev ## 1.4.0.0 From 0a57d1fab10f2af6935a16b08dc696896fce68db Mon Sep 17 00:00:00 2001 From: Mike Nothhard Date: Wed, 24 Jun 2020 09:57:58 +0200 Subject: [PATCH 6/6] Corrected named parameters and object type cases per styleguide. --- Misc/xBitlockerCommon.psm1 | 14 +++++++------- Tests/Unit/xBitlockerCommon.tests.ps1 | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Misc/xBitlockerCommon.psm1 b/Misc/xBitlockerCommon.psm1 index fc8edcb..3e863e2 100644 --- a/Misc/xBitlockerCommon.psm1 +++ b/Misc/xBitlockerCommon.psm1 @@ -794,34 +794,34 @@ function Assert-HasPrereqsForBitlocker if ($blFeature.State -ne 'Enabled') { $hasAllPreReqs = $false - Write-Error 'The Bitlocker feature needs to be installed before the xBitlocker module can be used' + Write-Error -Message 'The Bitlocker feature needs to be installed before the xBitlocker module can be used' } } else { - $blFeature = Get-WindowsFeature BitLocker - $blAdminToolsFeature = Get-WindowsFeature RSAT-Feature-Tools-BitLocker - $blAdminToolsRemoteFeature = Get-WindowsFeature RSAT-Feature-Tools-BitLocker-RemoteAdminTool + $blFeature = Get-WindowsFeature -Name 'BitLocker' + $blAdminToolsFeature = Get-WindowsFeature -Name 'RSAT-Feature-Tools-BitLocker' + $blAdminToolsRemoteFeature = Get-WindowsFeature -Name 'RSAT-Feature-Tools-BitLocker-RemoteAdminTool' if ($blFeature.InstallState -ne 'Installed') { $hasAllPreReqs = $false - Write-Error 'The Bitlocker feature needs to be installed before the xBitlocker module can be used' + 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 'The RSAT-Feature-Tools-BitLocker feature needs to be installed before the xBitlocker module can be used' + 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 'The RSAT-Feature-Tools-BitLocker-RemoteAdminTool feature needs to be installed before the xBitlocker module can be used' + Write-Error -Message 'The RSAT-Feature-Tools-BitLocker-RemoteAdminTool feature needs to be installed before the xBitlocker module can be used' } } diff --git a/Tests/Unit/xBitlockerCommon.tests.ps1 b/Tests/Unit/xBitlockerCommon.tests.ps1 index 1e3036a..d5ec804 100644 --- a/Tests/Unit/xBitlockerCommon.tests.ps1 +++ b/Tests/Unit/xBitlockerCommon.tests.ps1 @@ -221,7 +221,7 @@ try { param ( - [string] + [String] $FeatureName ) } @@ -230,7 +230,7 @@ try { param ( - [string] + [String] $FeatureName ) } @@ -309,9 +309,9 @@ try Mock -CommandName Get-WindowsOptionalFeature -MockWith { param ( - [switch] - $online = $false, - [string] + [Switch] + $Online = $false, + [String] $FeatureName )