Skip to content

Commit c83785e

Browse files
J0F3johlju
authored andcommitted
Fix for Issue #8 (#9)
- Fixed an issue in CheckForPreReqs function where on Server Core the installation of the non existing Windows Feature 'RSAT-Feature-Tools-BitLocker-RemoteAdminTool' was erroneously checked (issue #8).
1 parent 609e54d commit c83785e

File tree

4 files changed

+284
-51
lines changed

4 files changed

+284
-51
lines changed

Misc/xBitlockerCommon.psm1

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ function CheckForPreReqs
425425
Write-Error "The RSAT-Feature-Tools-BitLocker feature needs to be installed before the xBitlocker module can be used"
426426
}
427427

428-
if ($blAdminToolsRemoteFeature.InstallState -ne "Installed")
428+
if ($blAdminToolsRemoteFeature.InstallState -ne 'Installed' -and (Get-OSEdition) -notmatch 'Core')
429429
{
430430
$hasAllPreReqs = $false
431431

@@ -518,4 +518,14 @@ function RemoveParameters
518518
}
519519
}
520520

521+
<#
522+
.SYNOPSIS
523+
Returns the OS edtion we currently running on
524+
#>
525+
function Get-OSEdition
526+
{
527+
(Get-ItemProperty -Path 'HKLM:/software/microsoft/windows nt/currentversion' -Name InstallationType).InstallationType
528+
}
529+
530+
521531
Export-ModuleMember -Function *

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ Defaults to false.
134134
* Fixed issue which caused Test to incorrectly succeed on fully decrypted volumes when correct Key Protectors were present ([issue #13](https://github.com/PowerShell/xBitlocker/issues/13))
135135
* Fixed issue which caused xBLAutoBitlocker to incorrectly detect Fixed vs Removable volumes. ([issue #11](https://github.com/PowerShell/xBitlocker/issues/11))
136136
* Fixed issue which made xBLAutoBitlocker unable to encrypt volumes with drive letters assigned. ([issue #10](https://github.com/PowerShell/xBitlocker/issues/10))
137+
* Fixed an issue in CheckForPreReqs function where on Server Core the installation of the non existing Windows Feature 'RSAT-Feature-Tools-BitLocker-RemoteAdminTool' was erroneously checked. ([issue #8](https://github.com/PowerShell/xBitlocker/issues/8))
138+
137139

138140
### 1.1.0.0
139141

Tests/Unit/xBitlockerCommon.tests.ps1

Lines changed: 270 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -P
44
# Begin Testing
55
try
66
{
7-
InModuleScope "xBitlockerCommon" {
7+
InModuleScope 'xBitlockerCommon' {
88

99
function Get-BitlockerVolume
1010
{
@@ -16,25 +16,25 @@ try
1616
)
1717
}
1818

19-
Describe "xBitlockerCommon\TestBitlocker" {
19+
Describe 'xBitlockerCommon\TestBitlocker' {
2020

2121
Context 'When OS Volume is not Encrypted and No Key Protectors Assigned' {
2222
Mock `
2323
-CommandName Get-BitlockerVolume `
2424
-ModuleName 'xBitlockerCommon' `
2525
-MockWith {
26-
# Decrypted with no Key Protectors
27-
return @{
28-
VolumeType = 'OperatingSystem'
29-
MountPoint = $MountPoint
30-
CapacityGB = 500
31-
VolumeStatus = 'FullyDecrypted'
32-
EncryptionPercentage = 0
33-
KeyProtector = @()
34-
AutoUnlockEnabled = $null
35-
ProtectionStatus = 'Off'
36-
}
26+
# Decrypted with no Key Protectors
27+
return @{
28+
VolumeType = 'OperatingSystem'
29+
MountPoint = $MountPoint
30+
CapacityGB = 500
31+
VolumeStatus = 'FullyDecrypted'
32+
EncryptionPercentage = 0
33+
KeyProtector = @()
34+
AutoUnlockEnabled = $null
35+
ProtectionStatus = 'Off'
3736
}
37+
}
3838

3939
It 'Should Fail The Test (TPM and RecoveryPassword Protectors)' {
4040
TestBitlocker -MountPoint 'C:' -PrimaryProtector 'TPMProtector' -RecoveryPasswordProtector $true | Should -Be $false
@@ -46,25 +46,25 @@ try
4646
-CommandName Get-BitlockerVolume `
4747
-ModuleName 'xBitlockerCommon' `
4848
-MockWith {
49-
# Encrypted with TPM and Recovery Password Key Protectors
50-
return @{
51-
VolumeType = 'OperatingSystem'
52-
MountPoint = $MountPoint
53-
CapacityGB = 500
54-
VolumeStatus = 'FullyEncrypted'
55-
EncryptionPercentage = 100
56-
KeyProtector = @(
57-
@{
58-
KeyProtectorType = 'Tpm'
59-
},
60-
@{
61-
KeyProtectorType = 'RecoveryPassword'
62-
}
63-
)
64-
AutoUnlockEnabled = $null
65-
ProtectionStatus = 'On'
66-
}
49+
# Encrypted with TPM and Recovery Password Key Protectors
50+
return @{
51+
VolumeType = 'OperatingSystem'
52+
MountPoint = $MountPoint
53+
CapacityGB = 500
54+
VolumeStatus = 'FullyEncrypted'
55+
EncryptionPercentage = 100
56+
KeyProtector = @(
57+
@{
58+
KeyProtectorType = 'Tpm'
59+
},
60+
@{
61+
KeyProtectorType = 'RecoveryPassword'
62+
}
63+
)
64+
AutoUnlockEnabled = $null
65+
ProtectionStatus = 'On'
6766
}
67+
}
6868

6969
It 'Should Pass The Test (TPM and RecoveryPassword Protectors)' {
7070
TestBitlocker -MountPoint 'C:' -PrimaryProtector 'TPMProtector' -RecoveryPasswordProtector $true -verbose | Should -Be $true
@@ -76,31 +76,252 @@ try
7676
-CommandName Get-BitlockerVolume `
7777
-ModuleName 'xBitlockerCommon' `
7878
-MockWith {
79-
# Encrypted with TPM and Recovery Password Key Protectors
80-
return @{
81-
VolumeType = 'OperatingSystem'
82-
MountPoint = $MountPoint
83-
CapacityGB = 500
84-
VolumeStatus = 'FullyDecrypted'
85-
EncryptionPercentage = 0
86-
KeyProtector = @(
87-
@{
88-
KeyProtectorType = 'Tpm'
89-
},
90-
@{
91-
KeyProtectorType = 'RecoveryPassword'
92-
}
93-
)
94-
AutoUnlockEnabled = $null
95-
ProtectionStatus = 'Off'
96-
}
79+
# Encrypted with TPM and Recovery Password Key Protectors
80+
return @{
81+
VolumeType = 'OperatingSystem'
82+
MountPoint = $MountPoint
83+
CapacityGB = 500
84+
VolumeStatus = 'FullyDecrypted'
85+
EncryptionPercentage = 0
86+
KeyProtector = @(
87+
@{
88+
KeyProtectorType = 'Tpm'
89+
},
90+
@{
91+
KeyProtectorType = 'RecoveryPassword'
92+
}
93+
)
94+
AutoUnlockEnabled = $null
95+
ProtectionStatus = 'Off'
9796
}
97+
}
9898

9999
It 'Should Fail The Test (TPM and RecoveryPassword Protectors)' {
100100
TestBitlocker -MountPoint 'C:' -PrimaryProtector 'TPMProtector' -RecoveryPasswordProtector $true | Should -Be $false
101101
}
102102
}
103103
}
104+
105+
Describe 'xBitlockerCommon\CheckForPreReqs' {
106+
function Get-WindowsFeature
107+
{
108+
param
109+
(
110+
[string]
111+
$FeatureName
112+
)
113+
}
114+
115+
function Get-OSEdition
116+
{
117+
118+
}
119+
120+
Context 'When OS is Server Core and all required features are installed' {
121+
Mock -CommandName Get-OSEdition -MockWith {
122+
'Server Core'
123+
}
124+
125+
Mock -CommandName Get-WindowsFeature -MockWith {
126+
if ($FeatureName -eq 'RSAT-Feature-Tools-BitLocker-RemoteAdminTool')
127+
{
128+
return $null
129+
}
130+
else
131+
{
132+
return @{
133+
DisplayName = $FeatureName
134+
Name = $FeatureName
135+
InstallState = 'Installed'
136+
}
137+
}
138+
}
139+
140+
It 'Should not generate any error messages' {
141+
Mock -CommandName Write-Error
142+
CheckForPreReqs
143+
Assert-MockCalled -Command Write-Error -Exactly -Times 0 -Scope It
144+
}
145+
146+
It 'Should run the CheckForPreReqs function without exceptions' {
147+
{CheckForPreReqs} | Should -Not -Throw
148+
}
149+
}
150+
151+
Context 'When OS is Full Server and all required features are installed' {
152+
Mock -CommandName Get-OSEdition -MockWith {
153+
return 'Server'
154+
}
155+
156+
Mock -CommandName Get-WindowsFeature -MockWith {
157+
param
158+
(
159+
[string]
160+
$FeatureName
161+
)
162+
163+
return @{
164+
DisplayName = $FeatureName
165+
Name = $FeatureName
166+
InstallState = 'Installed'
167+
}
168+
}
169+
170+
It 'Should not generate any error messages' {
171+
Mock -CommandName Write-Error
172+
CheckForPreReqs
173+
Assert-MockCalled -Command Write-Error -Exactly -Times 0 -Scope It
174+
}
175+
176+
It 'Should run the CheckForPreReqs function without exceptions' {
177+
{CheckForPreReqs} | Should -Not -Throw
178+
}
179+
}
180+
181+
Context 'When OS is Full Server without the required features installed' {
182+
Mock -CommandName Get-OSEdition -MockWith {
183+
return 'Server'
184+
}
185+
186+
Mock -CommandName Get-WindowsFeature -MockWith {
187+
return @{
188+
DisplayName = $FeatureName
189+
Name = $FeatureName
190+
InstallState = 'Available'
191+
}
192+
}
193+
194+
Mock -CommandName Write-Error
195+
196+
It 'Should give an error that Bitlocker Windows Feature needs to be installed' {
197+
{CheckForPreReqs} | Should -Throw
198+
Assert-MockCalled -Command Write-Error -Exactly -Times 1 -Scope It -ParameterFilter {
199+
$Message -eq 'The Bitlocker feature needs to be installed before the xBitlocker module can be used'
200+
}
201+
}
202+
203+
It 'Should give an error that RSAT-Feature-Tools-BitLocker Windows Feature needs to be installed' {
204+
{CheckForPreReqs} | Should -Throw
205+
Assert-MockCalled -Command Write-Error -Exactly -Times 1 -Scope It -ParameterFilter {
206+
$Message -eq 'The RSAT-Feature-Tools-BitLocker feature needs to be installed before the xBitlocker module can be used'
207+
}
208+
}
209+
210+
It 'Should give an error that RSAT-Feature-Tools-BitLocker-RemoteAdminTool Windows Feature needs to be installed' {
211+
{CheckForPreReqs} | Should -Throw
212+
Assert-MockCalled -Command Write-Error -Exactly -Times 1 -Scope It -ParameterFilter {
213+
$Message -eq 'The RSAT-Feature-Tools-BitLocker-RemoteAdminTool feature needs to be installed before the xBitlocker module can be used'
214+
}
215+
}
216+
217+
It 'The CheckForPreReqs function should throw an exceptions about missing required Windows Features' {
218+
{CheckForPreReqs} | Should -Throw 'Required Bitlocker features need to be installed before xBitlocker can be used'
219+
}
220+
}
221+
222+
Context 'When OS is Server Core without the required features installed' {
223+
Mock -CommandName Get-OSEdition -MockWith {
224+
return 'Server Core'
225+
}
226+
227+
Mock -CommandName Get-WindowsFeature -MockWith {
228+
param
229+
(
230+
[string]
231+
$FeatureName
232+
)
233+
234+
if ($FeatureName -eq 'RSAT-Feature-Tools-BitLocker-RemoteAdminTool')
235+
{
236+
return $null
237+
}
238+
else
239+
{
240+
241+
return @{
242+
DisplayName = $FeatureName
243+
Name = $FeatureName
244+
InstallState = 'Available'
245+
}
246+
}
247+
}
248+
249+
Mock -CommandName Write-Error
250+
251+
It 'Should give an error that Bitlocker Windows Feature needs to be installed' {
252+
{CheckForPreReqs} | Should -Throw
253+
Assert-MockCalled -Command Write-Error -Exactly -Times 1 -Scope It -ParameterFilter {
254+
$Message -eq 'The Bitlocker feature needs to be installed before the xBitlocker module can be used'
255+
}
256+
}
257+
258+
It 'Should give an error that RSAT-Feature-Tools-BitLocker Windows Feature needs to be installed' {
259+
{CheckForPreReqs} | Should -Throw
260+
Assert-MockCalled -Command Write-Error -Exactly -Times 1 -Scope It -ParameterFilter {
261+
$Message -eq 'The RSAT-Feature-Tools-BitLocker feature needs to be installed before the xBitlocker module can be used'
262+
}
263+
}
264+
265+
It 'Should not give an error that RSAT-Feature-Tools-BitLocker-RemoteAdminTool Windows Feature needs to be installed as this Windows Features is not available on Server Core.' {
266+
{CheckForPreReqs} | Should -Throw
267+
Assert-MockCalled -Command Write-Error -Exactly -Times 0 -Scope It -ParameterFilter {
268+
$Message -eq 'The RSAT-Feature-Tools-BitLocker-RemoteAdminTool feature needs to be installed before the xBitlocker module can be used'
269+
}
270+
}
271+
272+
It 'The CheckForPreReqs function should throw an exceptions about missing required Windows Features' {
273+
{CheckForPreReqs} | Should -Throw 'Required Bitlocker features need to be installed before xBitlocker can be used'
274+
}
275+
}
276+
}
277+
278+
Describe 'xBitLockerCommon\Get-OSEdition' {
279+
It 'Should return "Server Core" if the OS is Windows Server Core' {
280+
Mock -CommandName Get-ItemProperty -MockWith {
281+
[PSCustomObject]@{
282+
InstallationType = 'Server Core'
283+
PSPath = 'Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\software\microsoft\windows nt\currentversion'
284+
PSParentPath = 'Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\software\microsoft\windows nt'
285+
PSChildName = 'currentversion'
286+
PSDrive = 'HKLM'
287+
PSProvider = 'Microsoft.PowerShell.Core\Registry'
288+
}
289+
}
290+
291+
$OSVersion = Get-OSEdition
292+
$OSVersion | Should -Be 'Server Core'
293+
}
294+
295+
It 'Should return "Server" if the OS is Full Windows Server' {
296+
Mock -CommandName Get-ItemProperty -MockWith {
297+
[PSCustomObject]@{
298+
InstallationType = 'Server'
299+
PSPath = 'Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\software\microsoft\windows nt\currentversion'
300+
PSParentPath = 'Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\software\microsoft\windows nt'
301+
PSChildName = 'currentversion'
302+
PSDrive = 'HKLM'
303+
PSProvider = 'Microsoft.PowerShell.Core\Registry'
304+
}
305+
}
306+
307+
$OSVersion = Get-OSEdition
308+
$OSVersion | Should -Be 'Server'
309+
}
310+
311+
It 'Should run without exceptions' {
312+
Mock -CommandName Get-ItemProperty -MockWith {
313+
[PSCustomObject]@{
314+
InstallationType = 'Some other os'
315+
PSPath = 'Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\software\microsoft\windows nt\currentversion'
316+
PSParentPath = 'Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\software\microsoft\windows nt'
317+
PSChildName = 'currentversion'
318+
PSDrive = 'HKLM'
319+
PSProvider = 'Microsoft.PowerShell.Core\Registry'
320+
}
321+
}
322+
{Get-OSEdition} | Should -Not -Throw
323+
}
324+
}
104325
}
105326
}
106327
finally

0 commit comments

Comments
 (0)