Skip to content

Commit 3251c85

Browse files
dbasnerGeorge Gelashvili
andcommitted
Add Set-RegistryProperty and Set-InternetExplorerRegistries.
Made Set-Registry work properly for pipelining purposes. [#167407675](https://www.pivotaltracker.com/story/show/167407675) As a platform engineer I want to see Internet Explorer-based policies that comply with MSFT Baseline Security Standard - 20193 Co-authored-by: George Gelashvili <[email protected]>
1 parent 4dca127 commit 3251c85

File tree

5 files changed

+175
-27
lines changed

5 files changed

+175
-27
lines changed

bosh-psmodules/modules/BOSH.Registry/BOSH.Registry.Tests.ps1

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,87 @@ Import-Module ./BOSH.Registry.psd1
33

44
Describe "BOSH.Registry" {
55
BeforeEach {
6-
Mock Set-ItemProperty { } -ModuleName BOSH.Registry
7-
Mock New-Item { } -ModuleName BOSH.Registry
6+
Mock Set-ItemProperty { } -ModuleName BOSH.Registry #actually
7+
$newItemReturn = [pscustomobject]@{"NewPath" = "HKCU:/Path/created";}
8+
Mock New-Item { $newItemReturn } -ModuleName BOSH.Registry
9+
# reset for our -parameterfilter mock
10+
Mock New-Item { $newItemReturn } -ModuleName BOSH.Registry -ParameterFilter { $PSBoundParameters['ErrorAction'] -eq "Stop" }
811
}
912

1013
It "Set-RegistryProperty adds a property to the registry" {
11-
Set-RegistryProperty -Path "HKLM:/Some/Registry/Path" -Name "A Registry Key" -Value "yes"
14+
{Set-RegistryProperty -Path "HKLM:/Some/Registry/Path" -Name "A Registry Key" -Value "yes"} | Should -Not -Throw
1215

1316
Assert-MockCalled Set-ItemProperty -Exactly 1 -Scope It -ModuleName BOSH.Registry -ParameterFilter {
1417
$Path -eq "HKLM:/Some/Registry/Path" -and $Name -eq "A Registry Key" -and $Value -eq "yes"
1518
}
1619
}
1720

1821
It "Set-RegistryProperty ensures the folder exists, before modifying the registry property" {
19-
Set-RegistryProperty -Path "HKLM:/Some/Registry/Path" -Name "A Registry Key" -Value "yes"
22+
{Set-RegistryProperty -Path "HKLM:/Some/Registry/Path" -Name "A Registry Key" -Value "yes"} | Should -Not -Throw
2023

2124
Assert-MockCalled New-Item -Exactly 1 -Scope It -ModuleName BOSH.Registry -ParameterFilter {
22-
$Path -eq "HKLM:/Some/Registry/Path" -and $ItemType -eq "Directory"
25+
$Path -eq "HKLM:/Some/Registry/Path" -and $ItemType -eq "Directory" -and $Force -eq $True
26+
}
27+
}
28+
29+
It "a list of items piped to Set-Registry causes every item in the list to have a key value set" {
30+
$keyList = @(
31+
[pscustomobject]@{"Path" = "HKCU:/Registry/Key/Path/One"; "Name" = "RegistryOne"; "Value" = "1"},
32+
[pscustomobject]@{"Path" = "HKCU:/Registry/Key/Path/Two"; "Name" = "RegistryTwo"; "Value" = "2"},
33+
[pscustomobject]@{"Path" = "HKCU:/Registry/Key/Path/Three"; "Name" = "RegistryThree"; "Value" = "3"}
34+
)
35+
36+
$keyList | Set-RegistryProperty
37+
38+
Assert-MockCalled Set-ItemProperty -Exactly 3 -Scope It -ModuleName BOSH.Registry
39+
Assert-MockCalled Set-ItemProperty -Exactly 1 -Scope It -ModuleName BOSH.Registry -ParameterFilter {
40+
$Path -eq "HKCU:/Registry/Key/Path/One" -and $Name -eq "RegistryOne" -and $Value -eq "1"
41+
}
42+
Assert-MockCalled Set-ItemProperty -Exactly 1 -Scope It -ModuleName BOSH.Registry -ParameterFilter {
43+
$Path -eq "HKCU:/Registry/Key/Path/Two" -and $Name -eq "RegistryTwo" -and $Value -eq "2"
44+
}
45+
Assert-MockCalled Set-ItemProperty -Exactly 1 -Scope It -ModuleName BOSH.Registry -ParameterFilter {
46+
$Path -eq "HKCU:/Registry/Key/Path/Three" -and $Name -eq "RegistryThree" -and $Value -eq "3"
47+
}
48+
}
49+
50+
It "Set-RegistryProperty doesn't call Set-ItemProperty if New-Item fails" {
51+
# ErrorAction Parameterfilter is present to ensure we only throw an error on a New-Item call that is configured to throw errors
52+
Mock New-Item { Throw 'some error' } -ModuleName BOSH.Registry -ParameterFilter { $PSBoundParameters['ErrorAction'] -eq "Stop" }
53+
54+
{ Set-RegistryProperty -Path "HKLM:/Some/Registry/Path" -Name "A reigstry Key" -Value "no" } | Should -Throw
55+
56+
Assert-MockCalled Set-ItemProperty -Exactly 0 -Scope It -ModuleName BOSH.Registry
57+
}
58+
59+
It "Set-RegistryProperty throws path couldn't be created if New-Item fails" {
60+
# ErrorAction Parameterfilter is present to ensure we only throw an error on a New-Item call that is configured to throw errors
61+
Mock New-Item { Throw 'some error' } -ModuleName BOSH.Registry -ParameterFilter { $PSBoundParameters['ErrorAction'] -eq "Stop" }
62+
63+
{ Set-RegistryProperty -Path "Something" -Name "Thing" -Value "no" } | Should -Throw "Unable to create path 'Something'"
64+
}
65+
66+
It "Set-RegistryProperty throws could not set registry key if Set-ItemProperty fails" {
67+
# ErrorAction Parameterfilter is present to ensure we only throw an error on a Set-ItemProperty call that is configured to throw errors
68+
Mock Set-ItemProperty { Throw 'some error' } -ModuleName BOSH.Registry -ParameterFilter { $PSBoundParameters['ErrorAction'] -eq "Stop" }
69+
70+
{Set-RegistryProperty -Path "HKLM:/Some/Registry/Path" -Name "A Registry Key" -Value "yes"} |
71+
Should -Throw "Unable to set registry key at 'HKLM:/Some/Registry/Path'"
72+
}
73+
74+
It "Set-InternetExplorerRegistries imports internet-explorer.csv and pipes to Set-RegistryProperty" {
75+
Mock Import-Csv { [pscustomobject]@{"Path" = "a"; "Name" = "b"; "Value" = "c"} } -ModuleName BOSH.Registry
76+
Mock Set-RegistryProperty { } -ModuleName BOSH.Registry
77+
78+
{ Set-InternetExplorerRegistries } | Should -Not -Throw
79+
80+
$expectedPath = Join-Path -Path $PSScriptRoot -ChildPath "data\internet-explorer.csv"
81+
82+
Assert-MockCalled Import-Csv -Exactly 1 -Scope It -ModuleName BOSH.Registry -ParameterFilter {
83+
$Path -eq $expectedPath
84+
}
85+
Assert-MockCalled Set-RegistryProperty -Exactly 1 -Scope It -ModuleName BOSH.Registry -ParameterFilter {
86+
$Path -eq "a" -and $Name -eq "b" -and $Value -eq "c"
2387
}
2488
}
2589
}

bosh-psmodules/modules/BOSH.Registry/BOSH.Registry.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
Description = 'Install Microsoft SSHD'
88
PowerShellVersion = '4.0'
99
FunctionsToExport = @(
10-
'Set-RegistryProperty'
10+
'Set-RegistryProperty',
11+
'Set-InternetExplorerRegistries'
1112
)
1213
CmdletsToExport = @()
1314
VariablesToExport = @()
Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,64 @@
1-
<#
2-
.Synopsis
3-
Apply a registry property, ensuring the path to the registry exists
4-
.Description
5-
This cmdlet ensures the registry exists before configuring the requested property
6-
.Parameter Path
7-
The path of the registry the property should be associated with
8-
.Parameter Name
9-
The name of the registry property
10-
.Parameter Value
11-
The value of the registry property
12-
#>
13-
Function Set-RegistryProperty {
14-
Param(
15-
[string]$Path,
16-
[string]$Name,
17-
[string]$Value
18-
)
1+
function Set-RegistryProperty {
2+
<#
3+
.SYNOPSIS
4+
Apply a registry property, ensuring the path to the registry exists
5+
.DESCRIPTION
6+
This cmdlet ensures the registry exists before configuring the requested property
7+
.PARAMETER Path
8+
The path of the registry the property should be associated with
9+
.PARAMETER Name
10+
The name of the registry property
11+
.PARAMETER Value
12+
The value of the registry property
13+
.INPUTS
14+
Any object, or list of objects with properties names Path, Name & Value
15+
.OUTPUTS
16+
If successful Set-RegistryProperty will not return any output, however it will throw an exception if any part
17+
of the command fails
18+
#>
1919

20-
New-Item -Path $Path -ItemType "Directory"
20+
[CmdletBinding()]
21+
param(
22+
[Parameter(ValueFromPipelineByPropertyName)]
23+
[String]$Path,
24+
[Parameter(ValueFromPipelineByPropertyName)]
25+
[String]$Name,
26+
[Parameter(ValueFromPipelineByPropertyName)]
27+
[String]$Value
28+
)
2129

22-
Set-ItemProperty -Path $Path -Name $Name -Value $Value
30+
Process {
31+
try{
32+
New-Item -Path $Path -ItemType "Directory" -Force -ErrorAction "Stop"
33+
} catch {
34+
throw "Unable to create path '$Path':$_"
35+
}
36+
try {
37+
Set-ItemProperty -Path $Path -Name $Name -Value $Value -ErrorAction "Stop" #[System.Management.Automation.ActionPreference]::Stop
38+
} catch {
39+
throw "Unable to set registry key at '$Path':$_"
40+
}
41+
}
2342
}
43+
44+
function Set-InternetExplorerRegistries {
45+
<#
46+
.SYNOPSIS
47+
Apply BOSH Windows Stemcell registry settings related to internet explorer
48+
.DESCRIPTION
49+
Apply Internet Explorer registry settings taken from Microsoft's baseline security analysis tool
50+
.INPUTS
51+
None. You can't pipe anything in to this command
52+
.OUTPUTS
53+
Set-InternetExplorerRegistries will return any failure output from Import-Csv or Set-RegistryProperty
54+
#>
55+
56+
[CmdletBinding()]
57+
58+
param()
59+
60+
process {
61+
$source = Join-Path -Path $PSScriptRoot -ChildPath "data\internet-explorer.csv"
62+
Import-Csv -Path $source | Set-RegistryProperty
63+
}
64+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Path,Name,Value
2+
HKCU:\Software\Policies\Microsoft\Internet Explorer\Control Panel,FormSuggest Passwords,1
3+
HKCU:\Software\Policies\Microsoft\Internet Explorer\Main,FormSuggest Passwords,no
4+
HKCU:\Software\Policies\Microsoft\Internet Explorer\Main,FormSuggest PW Ask,no
5+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Download,CheckExeSignatures,yes
6+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Download,RunInvalidSignatures,0
7+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Feeds,DisableEnclosureDownload,1
8+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main,DisableEPMCompat,1
9+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main,Isolation,PMEM
10+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main,Isolation64Bit,1
11+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_DISABLE_MK_PROTOCOL,(Reserved),1
12+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_DISABLE_MK_PROTOCOL,explorer.exe,1
13+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_DISABLE_MK_PROTOCOL,iexplore.exe,1
14+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_MIME_HANDLING,(Reserved),1
15+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_MIME_HANDLING,explorer.exe,1
16+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_MIME_HANDLING,iexplore.exe,1
17+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_MIME_SNIFFING,(Reserved),1
18+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_MIME_SNIFFING,explorer.exe,1
19+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_MIME_SNIFFING,iexplore.exe,1
20+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_RESTRICT_ACTIVEXINSTALL,(Reserved),1
21+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_RESTRICT_ACTIVEXINSTALL,explorer.exe,1
22+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_RESTRICT_ACTIVEXINSTALL,iexplore.exe,1
23+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_RESTRICT_FILEDOWNLOAD,(Reserved),1
24+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_RESTRICT_FILEDOWNLOAD,explorer.exe,1
25+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_RESTRICT_FILEDOWNLOAD,iexplore.exe,1
26+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_SECURITYBAND,(Reserved),1
27+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_SECURITYBAND,explorer.exe,1
28+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_SECURITYBAND,iexplore.exe,1
29+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_WINDOW_RESTRICTIONS,(Reserved),1
30+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_WINDOW_RESTRICTIONS,explorer.exe,1
31+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_WINDOW_RESTRICTIONS,iexplore.exe,1
32+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_ZONE_ELEVATION,(Reserved),1
33+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_ZONE_ELEVATION,explorer.exe,1
34+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_ZONE_ELEVATION,iexplore.exe,1
35+
HKLM:\Software\Policies\Microsoft\Internet Explorer\PhishingFilter,EnabledV9,1
36+
HKLM:\Software\Policies\Microsoft\Internet Explorer\PhishingFilter,PreventOverride,1
37+
HKLM:\Software\Policies\Microsoft\Internet Explorer\PhishingFilter,PreventOverrideAppRepUnknown,1
38+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Restrictions,NoCrashDetection,1
39+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Security,DisableSecuritySettingsCheck,0
40+
HKLM:\Software\Policies\Microsoft\Internet Explorer\Security\ActiveX,BlockNonAdminActiveXInstall,1

scripts/install-bosh-psmodules.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ $path = "C:\Program Files\WindowsPowerShell\Modules"
2929
Remove-Item -Path (Join-Path $path "BOSH.*") -Force -Recurse
3030

3131
Unzip -ZipFile "C:\provision\bosh-psmodules.zip" -OutPath $path -Keep $false
32-
Import-Module BOSH.Utils
32+
33+
Import-Module -Name BOSH.Utils
34+
Import-Module -Name BOSH.Registry

0 commit comments

Comments
 (0)