@@ -39,43 +39,55 @@ function Get-TargetResource
3939
4040 Assert-ParametersValid @PSBoundParameters
4141
42- $fileContent = Get-Content - Path $Path - Raw
43- $fileEncoding = Get-FileEncoding - Path $Path
44-
45- Write-Verbose - Message ($script :localizedData.SearchForKeyMessage -f `
46- $Path , $Name )
47-
48- # Setup the Regex Options that will be used
49- $regExOptions = [System.Text.RegularExpressions.RegexOptions ]::Multiline
50-
51- # Search the key that matches the requested key
52- $results = [regex ]::Matches($fileContent , " ^[\s]*$Name =([^\n\r]*)" , $regExOptions )
53-
5442 $ensure = ' Absent'
5543 $text = $null
44+ $fileEncoding = $null
5645
57- if ($results .Count -eq 0 )
46+ if (Test-Path - Path $Path )
5847 {
59- # No matches found
60- Write-Verbose - Message ($script :localizedData.KeyNotFoundMessage -f `
61- $Path , $Name )
62- }
63- else
64- {
65- # One of more key value pairs were found
66- $ensure = ' Present'
67- $textValues = @ ()
48+ $fileContent = Get-Content - Path $Path - Raw
49+ $fileEncoding = Get-FileEncoding - Path $Path
6850
69- foreach ($match in $results )
51+ if ($null -ne $fileContent )
7052 {
71- $textValues += $match.Groups [1 ].Value
72- }
53+ Write-Verbose - Message ($script :localizedData.SearchForKeyMessage -f $Path , $Name )
7354
74- $text = ($textValues -join ' ,' )
55+ # Setup the Regex Options that will be used
56+ $regExOptions = [System.Text.RegularExpressions.RegexOptions ]::Multiline
7557
76- Write-Verbose - Message ($script :localizedData.KeyFoundMessage -f `
77- $Path , $Name , $text )
78- } # if
58+ # Search the key that matches the requested key
59+ $results = [regex ]::Matches($fileContent , " ^[\s]*$Name =([^\n\r]*)" , $regExOptions )
60+
61+ if ($results.Count -eq 0 )
62+ {
63+ # No matches found
64+ Write-Verbose - Message ($script :localizedData.KeyNotFoundMessage -f $Path , $Name )
65+ }
66+ else
67+ {
68+ # One of more key value pairs were found
69+ $ensure = ' Present'
70+ $textValues = @ ()
71+
72+ foreach ($match in $results )
73+ {
74+ $textValues += $match.Groups [1 ].Value
75+ }
76+
77+ $text = ($textValues -join ' ,' )
78+
79+ Write-Verbose - Message ($script :localizedData.KeyFoundMessage -f $Path , $Name , $text )
80+ } # if
81+ }
82+ else
83+ {
84+ Write-Verbose - Message ($script :localizedData.KeyValuePairFileIsEmpty -f $Path )
85+ }
86+ }
87+ else
88+ {
89+ Write-Verbose - Message ($script :localizedData.KeyValuePairFileNotFound -f $Path )
90+ }
7991
8092 return @ {
8193 Path = $Path
@@ -174,16 +186,15 @@ function Set-TargetResource
174186 Assert-ParametersValid @PSBoundParameters
175187
176188 $fileContent = Get-Content - Path $Path - Raw - ErrorAction SilentlyContinue
177- $fileEncoding = Get-FileEncoding - Path $Path
189+ $fileEncoding = Get-FileEncoding - Path $Path - ErrorAction SilentlyContinue
178190
179191 $fileProperties = @ {
180192 Path = $Path
181193 NoNewline = $true
182194 Force = $true
183195 }
184196
185- Write-Verbose - Message ($script :localizedData.SearchForKeyMessage -f `
186- $Path , $Name )
197+ Write-Verbose - Message ($script :localizedData.SearchForKeyMessage -f $Path , $Name )
187198
188199 if ($Type -eq ' Secret' )
189200 {
@@ -221,16 +232,14 @@ function Set-TargetResource
221232
222233 $fileContent += $keyValuePair
223234
224- Write-Verbose - Message ($script :localizedData.KeyAddMessage -f `
225- $Path , $Name )
235+ Write-Verbose - Message ($script :localizedData.KeyAddMessage -f $Path , $Name )
226236 }
227237 else
228238 {
229239 # The key value pair was found so update it
230240 $fileContent = [regex ]::Replace($fileContent , " ^[\s]*$Name =(.*)($eolChars *)" , $keyValuePair , $regExOptions )
231241
232- Write-Verbose - Message ($script :localizedData.KeyUpdateMessage -f `
233- $Path , $Name )
242+ Write-Verbose - Message ($script :localizedData.KeyUpdateMessage -f $Path , $Name )
234243 } # if
235244 }
236245 else
@@ -244,17 +253,15 @@ function Set-TargetResource
244253 }
245254 else
246255 {
247- Write-Verbose - Message ($script :localizedData.FileEncodingNotInDesiredState -f `
248- $fileEncoding , $Encoding )
256+ Write-Verbose - Message ($script :localizedData.FileEncodingNotInDesiredState -f $fileEncoding , $Encoding )
249257 }
250258 }
251259 else
252260 {
253261 # The Key exists in the file but should not so remove it
254262 $fileContent = [regex ]::Replace($fileContent , " ^[\s]*$Name =(.*)$eolChars " , ' ' , $regExOptions )
255263
256- Write-Verbose - Message ($script :localizedData.KeyRemoveMessage -f `
257- $Path , $Name )
264+ Write-Verbose - Message ($script :localizedData.KeyRemoveMessage -f $Path , $Name )
258265 }
259266 } # if
260267 }
@@ -360,23 +367,32 @@ function Test-TargetResource
360367
361368 Assert-ParametersValid @PSBoundParameters
362369
363- # Flag to signal whether settings are correct
364- [ System.Boolean ] $desiredConfigurationMatch = $ true
365-
366- # Check if file being managed exists. If not return $False.
370+ <#
371+ If the file being managed does not exist then return true if
372+ the key should be absent or false if it should be present.
373+ #>
367374 if (-not (Test-Path - Path $Path ))
368375 {
369- return $false
376+ Write-Verbose - Message ($script :localizedData.KeyValuePairFileNotFound -f $Path )
377+
378+ return ($Ensure -eq ' Absent' )
370379 }
371380
372381 $fileContent = Get-Content - Path $Path - Raw
373- $fileEncoding = Get-FileEncoding - Path $Path
374382
375- Write-Verbose - Message ($script :localizedData.SearchForKeyMessage -f `
376- $Path , $Name )
383+ if ($null -eq $fileContent )
384+ {
385+ Write-Verbose - Message ($script :localizedData.KeyValuePairFileIsEmpty -f $Path )
377386
378- # Setup the Regex Options that will be used
387+ return ($Ensure -eq ' Absent' )
388+ }
389+
390+ $desiredConfigurationMatch = $true
391+ $fileEncoding = Get-FileEncoding - Path $Path
379392 $regExOptions = [System.Text.RegularExpressions.RegexOptions ]::Multiline
393+
394+ Write-Verbose - Message ($script :localizedData.SearchForKeyMessage -f $Path , $Name )
395+
380396 if ($IgnoreNameCase )
381397 {
382398 $regExOptions += [System.Text.RegularExpressions.RegexOptions ]::IgnoreCase
@@ -391,16 +407,14 @@ function Test-TargetResource
391407 if ($Ensure -eq ' Present' )
392408 {
393409 # The key value pairs should exist but do not
394- Write-Verbose - Message ($script :localizedData.KeyNotFoundButShouldExistMessage -f `
395- $Path , $Name )
410+ Write-Verbose - Message ($script :localizedData.KeyNotFoundButShouldExistMessage -f $Path , $Name )
396411
397412 $desiredConfigurationMatch = $false
398413 }
399414 else
400415 {
401416 # The key value pairs should exist and do
402- Write-Verbose - Message ($script :localizedData.KeyNotFoundAndShouldNotExistMessage -f `
403- $Path , $Name )
417+ Write-Verbose - Message ($script :localizedData.KeyNotFoundAndShouldNotExistMessage -f $Path , $Name )
404418 } # if
405419 }
406420 else
@@ -426,15 +440,13 @@ function Test-TargetResource
426440
427441 if ($desiredConfigurationMatch )
428442 {
429- Write-Verbose - Message ($script :localizedData.KeyFoundButNoReplacementMessage -f `
430- $Path , $Name )
443+ Write-Verbose - Message ($script :localizedData.KeyFoundButNoReplacementMessage -f $Path , $Name )
431444 }
432445 }
433446 else
434447 {
435448 # The key value pairs should not exist
436- Write-Verbose - Message ($script :localizedData.KeyFoundButShouldNotExistMessage -f `
437- $Path , $Name )
449+ Write-Verbose - Message ($script :localizedData.KeyFoundButShouldNotExistMessage -f $Path , $Name )
438450
439451 $desiredConfigurationMatch = $false
440452 } # if
@@ -443,8 +455,7 @@ function Test-TargetResource
443455 if ($PSBoundParameters.ContainsKey (' Encoding' ) -and ($Encoding -ne $fileEncoding ))
444456 {
445457 # File encoding is not in desired state
446- Write-Verbose - Message ($script :localizedData.FileEncodingNotInDesiredState -f `
447- $fileEncoding , $Encoding )
458+ Write-Verbose - Message ($script :localizedData.FileEncodingNotInDesiredState -f $fileEncoding , $Encoding )
448459
449460 $desiredConfigurationMatch = $false
450461 }
0 commit comments