@@ -10,39 +10,38 @@ Import-Module -Name (Join-Path -Path $modulePath -ChildPath 'DscResource.Common'
1010# Import Localization Strings
1111$script :localizedData = Get-LocalizedData - DefaultUICulture ' en-US'
1212
13-
14- # Registry key paths for proxy settings
15- $script :connectionsRegistryKeyPath = ' SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Connections'
16-
1713<#
1814 . SYNOPSIS
19- Returns the current state of the proxy settings for
20- the computer.
15+ Returns the current state of the proxy settings.
2116
22- . PARAMETER IsSingleInstance
23- Specifies the resource is a single instance, the
24- value must be 'Yes'. Not used in Get-TargetResource .
17+ . PARAMETER Target
18+ Specifies if the proxy settings should be set for the LocalMachine
19+ or for the CurrentUser. Defaults to 'LocalMachine' .
2520#>
2621function Get-TargetResource
2722{
2823 [OutputType ([System.Collections.Hashtable ])]
2924 param
3025 (
3126 [Parameter (Mandatory = $true )]
32- [ValidateSet (' Yes ' )]
27+ [ValidateSet (' LocalMachine ' , ' CurrentUser ' )]
3328 [System.String ]
34- $IsSingleInstance
29+ $Target
3530 )
3631
3732 Write-Verbose - Message ( @ (" $ ( $MyInvocation.MyCommand ) : "
38- $ ($script :localizedData.GettingProxySettingsMessage )
33+ $ ($script :localizedData.GettingProxySettingsMessage -f $Target )
3934 ) -join ' ' )
4035
41- $returnValue = @ {}
36+ $proxySettingsPath = Get-ProxySettingsRegistryKeyPath `
37+ - Target $Target
38+ $returnValue = @ {
39+ Target = $Target
40+ }
4241
4342 # Get the registry values in the Connections registry key
4443 $connectionsRegistryValues = Get-ItemProperty `
45- - Path " HKLM:\ $ ( $ script :connectionsRegistryKeyPath ) " `
44+ - Path $proxySettingsPath `
4645 - ErrorAction SilentlyContinue
4746
4847 $proxySettingsRegistryBinary = $null
@@ -81,15 +80,14 @@ function Get-TargetResource
8180
8281<#
8382 . SYNOPSIS
84- Sets the current state of the proxy settings for
85- the computer.
83+ Sets the current state of the proxy settings.
8684
87- . PARAMETER IsSingleInstance
88- Specifies the resource is a single instance, the
89- value must be 'Yes '.
85+ . PARAMETER Target
86+ Specifies if the proxy settings should be set for the LocalMachine
87+ or for the CurrentUser. Defaults to 'LocalMachine '.
9088
9189 . PARAMETER Ensure
92- Specifies if computer proxy settings should be set.
90+ Specifies if proxy settings should be set.
9391 Defaults to 'Present'.
9492
9593 . PARAMETER ConnectionType
@@ -130,9 +128,9 @@ function Set-TargetResource
130128 param
131129 (
132130 [Parameter (Mandatory = $true )]
133- [ValidateSet (' Yes ' )]
131+ [ValidateSet (' LocalMachine ' , ' CurrentUser ' )]
134132 [System.String ]
135- $IsSingleInstance ,
133+ $Target ,
136134
137135 [Parameter ()]
138136 [ValidateSet (' Present' , ' Absent' )]
@@ -174,42 +172,45 @@ function Set-TargetResource
174172 )
175173
176174 Write-Verbose - Message ( @ (" $ ( $MyInvocation.MyCommand ) : "
177- $ ($script :localizedData.ApplyingProxySettingsMessage -f $Ensure )
175+ $ ($script :localizedData.ApplyingProxySettingsMessage -f $Target , $ Ensure )
178176 ) -join ' ' )
179177
178+ $proxySettingsPath = Get-ProxySettingsRegistryKeyPath `
179+ - Target $Target
180+
180181 if ($Ensure -eq ' Absent' )
181182 {
182183 # Remove all the Proxy Settings
183184 Write-Verbose - Message ( @ (" $ ( $MyInvocation.MyCommand ) : "
184- $ ($script :localizedData.DisablingComputerProxyMessage )
185+ $ ($script :localizedData.DisablingProxyMessage -f $Target )
185186 ) -join ' ' )
186187
187188 if ($ConnectionType -in (' All' , ' Default' ))
188189 {
189190 Remove-ItemProperty `
190- - Path " HKLM:\ $ ( $ script :connectionsRegistryKeyPath ) " `
191+ - Path $proxySettingsPath `
191192 - Name ' DefaultConnectionSettings' `
192193 - ErrorAction SilentlyContinue
193194 }
194195
195196 if ($ConnectionType -in (' All' , ' Legacy' ))
196197 {
197198 Remove-ItemProperty `
198- - Path " HKLM:\ $ ( $ script :connectionsRegistryKeyPath ) " `
199+ - Path $proxySettingsPath `
199200 - Name ' SavedLegacySettings' `
200201 - ErrorAction SilentlyContinue
201202 }
202203 }
203204 else
204205 {
205206 Write-Verbose - Message ( @ (" $ ( $MyInvocation.MyCommand ) : "
206- $ ($script :localizedData.EnablingComputerProxyMessage )
207+ $ ($script :localizedData.EnablingProxyMessage -f $Target )
207208 ) -join ' ' )
208209
209210 # Generate the Proxy Settings binary value
210211 $convertToProxySettingsBinaryParameters = @ {} + $PSBoundParameters
211212
212- $convertToProxySettingsBinaryParameters.Remove (' IsSingleInstance ' )
213+ $convertToProxySettingsBinaryParameters.Remove (' Target ' )
213214 $convertToProxySettingsBinaryParameters.Remove (' Ensure' )
214215 $convertToProxySettingsBinaryParameters.Remove (' ConnectionType' )
215216
@@ -218,23 +219,23 @@ function Set-TargetResource
218219 if ($ConnectionType -in (' All' , ' Default' ))
219220 {
220221 Write-Verbose - Message ( @ (" $ ( $MyInvocation.MyCommand ) : "
221- $ ($script :localizedData.WritingComputerProxyBinarySettingsMessage -f ' DefaultConnectionSettings' , ($proxySettings -join ' ,' ))
222+ $ ($script :localizedData.WritingProxyBinarySettingsMessage -f $Target , ' DefaultConnectionSettings' , ($proxySettings -join ' ,' ))
222223 ) -join ' ' )
223224
224225 Set-BinaryRegistryValue `
225- - Path " HKEY_LOCAL_MACHINE\ $ ( $ script :connectionsRegistryKeyPath ) " `
226+ - Path $proxySettingsPath `
226227 - Name ' DefaultConnectionSettings' `
227228 - Value $proxySettings
228229 }
229230
230231 if ($ConnectionType -in (' All' , ' Legacy' ))
231232 {
232233 Write-Verbose - Message ( @ (" $ ( $MyInvocation.MyCommand ) : "
233- $ ($script :localizedData.WritingComputerProxyBinarySettingsMessage -f ' SavedLegacySettings' , ($proxySettings -join ' ,' ))
234+ $ ($script :localizedData.WritingProxyBinarySettingsMessage -f $Target , ' SavedLegacySettings' , ($proxySettings -join ' ,' ))
234235 ) -join ' ' )
235236
236237 Set-BinaryRegistryValue `
237- - Path " HKEY_LOCAL_MACHINE\ $ ( $ script :connectionsRegistryKeyPath ) " `
238+ - Path $proxySettingsPath `
238239 - Name ' SavedLegacySettings' `
239240 - Value $proxySettings
240241 }
@@ -243,15 +244,14 @@ function Set-TargetResource
243244
244245<#
245246 . SYNOPSIS
246- Tests the current state of the proxy settings for
247- the computer.
247+ Tests the current state of the proxy settings.
248248
249- . PARAMETER IsSingleInstance
250- Specifies the resource is a single instance, the
251- value must be 'Yes '.
249+ . PARAMETER Target
250+ Specifies if the proxy settings should be set for the LocalMachine
251+ or for the CurrentUser. Defaults to 'LocalMachine '.
252252
253253 . PARAMETER Ensure
254- Specifies if computer proxy settings should be set.
254+ Specifies if proxy settings should be set.
255255 Defaults to 'Present'.
256256
257257 . PARAMETER ConnectionType
@@ -293,9 +293,9 @@ function Test-TargetResource
293293 param
294294 (
295295 [Parameter (Mandatory = $true )]
296- [ValidateSet (' Yes ' )]
296+ [ValidateSet (' LocalMachine ' , ' CurrentUser ' )]
297297 [System.String ]
298- $IsSingleInstance ,
298+ $Target ,
299299
300300 [Parameter ()]
301301 [ValidateSet (' Present' , ' Absent' )]
@@ -337,14 +337,16 @@ function Test-TargetResource
337337 )
338338
339339 Write-Verbose - Message ( @ (" $ ( $MyInvocation.MyCommand ) : "
340- $ ($script :localizedData.CheckingProxySettingsMessage -f $Ensure )
340+ $ ($script :localizedData.CheckingProxySettingsMessage -f $Target , $ Ensure )
341341 ) -join ' ' )
342342
343- [System.Boolean ] $desiredConfigurationMatch = $true
343+ $desiredConfigurationMatch = $true
344+ $proxySettingsPath = Get-ProxySettingsRegistryKeyPath `
345+ - Target $Target
344346
345347 # Get the registry values in the Connections registry key
346348 $connectionsRegistryValues = Get-ItemProperty `
347- - Path " HKLM:\ $ ( $ script :connectionsRegistryKeyPath ) " `
349+ - Path $proxySettingsPath `
348350 - ErrorAction SilentlyContinue
349351
350352 if ($Ensure -eq ' Absent' )
@@ -356,7 +358,7 @@ function Test-TargetResource
356358 if ($connectionsRegistryValues.DefaultConnectionSettings )
357359 {
358360 Write-Verbose - Message ( @ (" $ ( $MyInvocation.MyCommand ) : "
359- $ ($script :localizedData.ComputerProxyBinarySettingsRequiresRemovalMessage -f ' DefaultConnectionSettings' )
361+ $ ($script :localizedData.ProxyBinarySettingsRequiresRemovalMessage -f $Target , ' DefaultConnectionSettings' )
360362 ) -join ' ' )
361363
362364 $desiredConfigurationMatch = $false
@@ -369,7 +371,7 @@ function Test-TargetResource
369371 if ($connectionsRegistryValues.SavedLegacySettings )
370372 {
371373 Write-Verbose - Message ( @ (" $ ( $MyInvocation.MyCommand ) : "
372- $ ($script :localizedData.ComputerProxyBinarySettingsRequiresRemovalMessage -f ' SavedLegacySettings' )
374+ $ ($script :localizedData.ProxyBinarySettingsRequiresRemovalMessage -f $Target , ' SavedLegacySettings' )
373375 ) -join ' ' )
374376
375377 $desiredConfigurationMatch = $false
@@ -380,15 +382,15 @@ function Test-TargetResource
380382 {
381383 $desiredValues = @ {} + $PSBoundParameters
382384
383- $desiredValues.Remove (' IsSingleInstance ' )
385+ $desiredValues.Remove (' Target ' )
384386 $desiredValues.Remove (' Ensure' )
385387 $desiredValues.Remove (' ConnectionType' )
386388
387389 if ($ConnectionType -in (' All' , ' Default' ))
388390 {
389391 # Check if the Default Connection proxy settings are in the desired state
390392 Write-Verbose - Message ( @ (" $ ( $MyInvocation.MyCommand ) : "
391- $ ($script :localizedData.CheckingComputerProxyBinarySettingsMessage -f ' DefaultConnectionSettings' )
393+ $ ($script :localizedData.CheckingProxyBinarySettingsMessage -f $Target , ' DefaultConnectionSettings' )
392394 ) -join ' ' )
393395
394396 if ($connectionsRegistryValues.DefaultConnectionSettings )
@@ -408,7 +410,7 @@ function Test-TargetResource
408410 if (-not $inDesiredState )
409411 {
410412 Write-Verbose - Message ( @ (" $ ( $MyInvocation.MyCommand ) : "
411- $ ($script :localizedData.ComputerProxyBinarySettingsNoMatchMessage -f ' DefaultConnectionSettings' )
413+ $ ($script :localizedData.ProxyBinarySettingsNoMatchMessage -f $Target , ' DefaultConnectionSettings' )
412414 ) -join ' ' )
413415
414416 $desiredConfigurationMatch = $false
@@ -419,7 +421,7 @@ function Test-TargetResource
419421 {
420422 # Check if the Saved Legacy proxy settings are in the desired state
421423 Write-Verbose - Message ( @ (" $ ( $MyInvocation.MyCommand ) : "
422- $ ($script :localizedData.CheckingComputerProxyBinarySettingsMessage -f ' SavedLegacySettings' )
424+ $ ($script :localizedData.CheckingProxyBinarySettingsMessage -f $Target , ' SavedLegacySettings' )
423425 ) -join ' ' )
424426
425427 if ($connectionsRegistryValues.SavedLegacySettings )
@@ -439,7 +441,7 @@ function Test-TargetResource
439441 if (-not $inDesiredState )
440442 {
441443 Write-Verbose - Message ( @ (" $ ( $MyInvocation.MyCommand ) : "
442- $ ($script :localizedData.ComputerProxyBinarySettingsNoMatchMessage -f ' SavedLegacySettings' )
444+ $ ($script :localizedData.ProxyBinarySettingsNoMatchMessage -f $Target , ' SavedLegacySettings' )
443445 ) -join ' ' )
444446
445447 $desiredConfigurationMatch = $false
@@ -481,6 +483,7 @@ function Set-BinaryRegistryValue
481483 $Value
482484 )
483485
486+ $Path = ConvertTo-Win32RegistryPath - Path $Path
484487 $null = [Microsoft.Win32.Registry ]::SetValue($Path , $Name , $Value , ' Binary' )
485488}
486489
@@ -510,7 +513,7 @@ function Test-ProxySettings
510513 $DesiredValues
511514 )
512515
513- [ System.Boolean ] $inState = $true
516+ $inState = $true
514517
515518 $proxySettingsToCompare = @ (
516519 ' EnableManualProxy'
@@ -745,7 +748,6 @@ function ConvertTo-ProxySettingsBinary
745748 . PARAMETER ProxySettings
746749 The binary extracted from the registry key
747750 DefaultConnectionSettings or SavedLegacySettings.
748-
749751#>
750752function ConvertFrom-ProxySettingsBinary
751753{
@@ -861,4 +863,69 @@ function ConvertFrom-ProxySettingsBinary
861863 return [PSObject ] $proxyParameters
862864}
863865
866+ <#
867+ . SYNOPSIS
868+ Get the proxy settings registry key path.
869+
870+ . PARAMETER Target
871+ Specify the target of the regisry key path to return.
872+
873+ It will return HKLM:\ if LocalMachine is specified and HKCU:\
874+ if CurrentUser is specified.
875+ #>
876+ function Get-ProxySettingsRegistryKeyPath
877+ {
878+ [CmdletBinding ()]
879+ [OutputType ([System.String ])]
880+ param
881+ (
882+ [Parameter ()]
883+ [ValidateSet (' LocalMachine' , ' CurrentUser' )]
884+ [System.String ]
885+ $Target = ' LocalMachine'
886+ )
887+
888+ if ($Target -eq ' LocalMachine' )
889+ {
890+ $path = ' HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Connections'
891+ }
892+ else
893+ {
894+ <#
895+ This path is almost identical to the LocalMachine one, but the
896+ case of 'Software' is different. This mostly shouldn't matter, but
897+ it is possible some future functions will be case sensitive.
898+ #>
899+ $path = ' HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections'
900+ }
901+
902+ return $path
903+ }
904+
905+ <#
906+ . SYNOPSIS
907+ Convert a registry path to be compatible with Win32.
908+
909+ . PARAMETER Path
910+ The registry path to convert from a PowerShell path to
911+ a path compatible with Win32.
912+ #>
913+ function ConvertTo-Win32RegistryPath
914+ {
915+ [CmdletBinding ()]
916+ [OutputType ([System.String ])]
917+ param
918+ (
919+ [Parameter ()]
920+ [System.String ]
921+ $Path
922+ )
923+
924+ # Translate the registry key from PS
925+ $Path = $Path -replace ' ^HKLM:\\' , ' HKEY_LOCAL_MACHINE\'
926+ $Path = $Path -replace ' ^HKCU:\\' , ' HKEY_CURRENT_USER\'
927+
928+ return $Path
929+ }
930+
864931Export-ModuleMember - function *- TargetResource
0 commit comments