DSCResources/xInternetExplorerHomePage/xInternetExplorerHomePage.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#---------------------------------------------------------------------------------
#The sample scripts are not supported under any Microsoft standard support
#program or service. The sample scripts are provided AS IS without warranty
#of any kind. Microsoft further disclaims all implied warranties including,
#without limitation, any implied warranties of merchantability or of fitness for
#a particular purpose. The entire risk arising out of the use or performance of
#the sample scripts and documentation remains with you. In no event shall
#Microsoft, its authors, or anyone else involved in the creation, production, or
#delivery of the scripts be liable for any damages whatsoever (including,
#without limitation, damages for loss of business profits, business interruption,
#loss of business information, or other pecuniary loss) arising out of the use
#of or inability to use the sample scripts or documentation, even if Microsoft
#has been advised of the possibility of such damages
#---------------------------------------------------------------------------------

function Get-TargetResource
{
    [CmdletBinding()]
    [OutputType([Hashtable])]
    param
    (
        [parameter(Mandatory = $true)]
        [String]
        $StartPage,

        [String]
        $SecondaryStartPages
    )
    
    $IEMainReg = 'HKLM:\Software\Microsoft\Internet Explorer\Main' 

    Write-Verbose "Detecting the start page of Internet Explorer."
    $StartPageReg = (Get-ItemProperty -Path $IEMainReg).'Start Page'

    Write-Verbose "Detecting the secondary start pages of Internet Explorer."
    $SecondaryStartPagesReg = (Get-ItemProperty -Path $IEMainReg).'Secondary Start Pages'


    $returnValue = @{
                        #Verify that the value exists, if it does not exist, the value of output is "NULL".
                        StartPage = $(If($StartPageReg){$StartPageReg}Else{"NULL"})
                        SecondaryStartPages = $(If($SecondaryStartPagesReg){$SecondaryStartPagesReg}Else{"NULL"})
                    }

    If($SecondaryStartPages)
    {
        If($($StartPage -eq $StartPageReg) -and $($SecondaryStartPages -eq $SecondaryStartPagesReg))
        {
            $returnValue.Ensure = "Present"
        }
        Else
        {
            $returnValue.Ensure = "Absent"
        }
    }
    Else
    {
        If($StartPage -eq $StartPageReg)
        {
            $returnValue.Ensure = "Present"
        }
        Else
        {
            $returnValue.Ensure = "Absent"
        }
    }

    $returnValue
}


function Set-TargetResource
{
    [CmdletBinding(SupportsShouldProcess=$true)]
    param
    (
        [parameter(Mandatory = $true)]
        [String]
        $StartPage,

        [String]
        $SecondaryStartPages,

        [ValidateSet("Present","Absent")]
        [String]
        $Ensure = 'Present'
    )

    $IEMainReg = 'HKLM:\Software\Microsoft\Internet Explorer\Main' 

    Switch($Ensure)
    {
        #Set the home page of IE
        'Present'
        {
            If($SecondaryStartPages)
            {
                If($PSCmdlet.ShouldProcess("Internet Explorer","Set the start page and secondary start page"))
                {
                    Try
                    {
                        Write-Verbose "Setting the start page of Internet Explorer."
                        Set-ItemProperty -Path $IEMainReg -Name "Start Page" -Value "$StartPage" -ErrorAction Stop
                    
                        Write-Verbose "Setting the secondary start page of Internete Explorer."
                        Set-ItemProperty -Path $IEMainReg -Name "Secondary Start Pages" -Value "$SecondaryStartPages" -ErrorAction Stop
                    }
                    Catch
                    {
                        $ErrorMsg = $_.Exception.Message
                        Write-Verbose $ErrorMsg
                    }
                }
            }
            Else
            {
                If($PSCmdlet.ShouldProcess("Internet Explorer","Set the start page"))
                {
                    Try
                    {
                        Write-Verbose "Setting the start page of Internet Explorer."
                        Set-ItemProperty -Path $IEMainReg -Name "Start Page" -Value "$StartPage" -ErrorAction Stop
                 
                    }
                    Catch
                    {
                        $ErrorMsg = $_.Exception.Message
                        Write-Verbose $ErrorMsg
                    } 
                }
            }
        }

        #Remove the home page of IE
        'Absent'
        {
            If($SecondaryStartPages)
            {
                If($PSCmdlet.ShouldProcess("Internet Explorer","Remove the start page and secondary start page"))
                {
                    Try
                    {
                        Write-Verbose "Removing the start page of Internete Explorer."
                        Set-ItemProperty -Path $IEMainReg -Name "Start Page" -Value "" -ErrorAction Stop

                        Write-Verbose "Removing the secondary start page of Internete Explorer."
                        Set-ItemProperty -Path $IEMainReg -Name "Secondary Start pages" -Value "" -ErrorAction Stop
                    }
                    Catch
                    {
                        $ErrorMsg = $_.Exception.Message
                        Write-Verbose $ErrorMsg
                    }
                }
            }
            Else
            {
                If($PSCmdlet.ShouldProcess("Internet Explorer","Remove the start page"))
                {
                    Try
                    {
                        Write-Verbose "Removing the start page of Internete Explorer."
                        Set-ItemProperty -Path $IEMainReg -Name "Start Page" -Value "" -ErrorAction Stop
                    }
                    Catch
                    {
                        $ErrorMsg = $_.Exception.Message
                        Write-Verbose $ErrorMsg
                    }
                }
            }
        }
    }
}


function Test-TargetResource
{
    [CmdletBinding()]
    [OutputType([Boolean])]
    param
    (
        [parameter(Mandatory = $true)]
        [String]
        $StartPage,

        [String]
        $SecondaryStartPages,

        [ValidateSet("Present","Absent")]
        [String]
        $Ensure = 'Present'
    )


    #Output the result of Get-TargetResource function.
    $Get = Get-TargetResource -StartPage $StartPage -SecondaryStartPages $SecondaryStartPages

    Switch($Ensure)
    {
        'Present'
        {
            If($SecondaryStartPages)
            {
                If($StartPage -eq $Get.StartPage -and $SecondaryStartPages -eq $Get.SecondaryStartPages)
                {
                    return $true
                }
                Else
                {
                    return $false
                }
            }
            Else
            {
                If($StartPage -eq $Get.StartPage -and $SecondaryStartPages -eq $Get.SecondaryStartPages)
                {
                    return $true
                }
                Else
                {
                    return $false
                }
            }
        }

        'Absent'
        {
           If($SecondaryStartPages)
           {
                If($Get.StartPage -eq "NULL" -and $Get.SecondaryStartPages -eq "NULL")
                {
                    return $true
                }
                Else
                {
                    return $false
                }
           }
           Else
           {
                If($Get.StartPage -eq "NULL")
                {
                    return $true
                }
                Else
                {
                    return $false
                }             
           }
        }
    }
}

Export-ModuleMember -Function *-TargetResource