@@ -177,7 +177,7 @@ function Set-TargetResource
177177
178178 Assert-ParametersValid @PSBoundParameters
179179
180- $fileContent = Get-Content - Path $Path - Raw
180+ $fileContent = Get-Content - Path $Path - Raw - ErrorAction SilentlyContinue
181181
182182 Write-Verbose - Message ($localizedData.SearchForKeyMessage -f `
183183 $Path , $Name )
@@ -187,61 +187,68 @@ function Set-TargetResource
187187 $Text = $Secret.GetNetworkCredential ().Password
188188 } # if
189189
190- # Determine the EOL characters used in the file
191- $eolChars = Get-TextEolCharacter - Text $fileContent
192-
193- # Setup the Regex Options that will be used
194- $regExOptions = [System.Text.RegularExpressions.RegexOptions ]::Multiline
195- if ($IgnoreNameCase )
190+ if ($null -ne $fileContent )
196191 {
197- $regExOptions += [ System.Text.RegularExpressions.RegexOptions ]::IgnoreCase
198- }
192+ # Determine the EOL characters used in the file
193+ $eolChars = Get-TextEolCharacter - Text $fileContent
199194
200- # Search the key that matches the requested key
201- $results = [regex ]::Matches($fileContent , " ^[\s]*$Name =([^\n\r]*)" , $regExOptions )
195+ # Setup the Regex Options that will be used
196+ $regExOptions = [System.Text.RegularExpressions.RegexOptions ]::Multiline
197+ if ($IgnoreNameCase )
198+ {
199+ $regExOptions += [System.Text.RegularExpressions.RegexOptions ]::IgnoreCase
200+ }
202201
203- if ($Ensure -eq ' Present' )
204- {
205- # The key value pair should exist
206- $keyValuePair = ' {0}={1}{2}' -f $Name , $Text , $eolChars
202+ # Search the key that matches the requested key
203+ $results = [regex ]::Matches($fileContent , " ^[\s]*$Name =([^\n\r]*)" , $regExOptions )
207204
208- if ($results .Count -eq 0 )
205+ if ($Ensure -eq ' Present ' )
209206 {
210- # The key value pair was not found so add it to the end of the file
211- if (-not $fileContent.EndsWith ($eolChars ))
207+ # The key value pair should exist
208+ $keyValuePair = ' {0}={1}{2}' -f $Name , $Text , $eolChars
209+
210+ if ($results.Count -eq 0 )
212211 {
213- $fileContent += $eolChars
214- } # if
212+ # The key value pair was not found so add it to the end of the file
213+ if (-not $fileContent.EndsWith ($eolChars ))
214+ {
215+ $fileContent += $eolChars
216+ } # if
215217
216- $fileContent += $keyValuePair
218+ $fileContent += $keyValuePair
217219
218- Write-Verbose - Message ($localizedData.KeyAddMessage -f `
219- $Path , $Name )
220+ Write-Verbose - Message ($localizedData.KeyAddMessage -f `
221+ $Path , $Name )
222+ }
223+ else
224+ {
225+ # The key value pair was found so update it
226+ $fileContent = [regex ]::Replace($fileContent , " ^[\s]*$Name =(.*)($eolChars *)" , $keyValuePair , $regExOptions )
227+
228+ Write-Verbose - Message ($localizedData.KeyUpdateMessage -f `
229+ $Path , $Name )
230+ } # if
220231 }
221232 else
222233 {
223- # The key value pair was found so update it
224- $fileContent = [regex ]::Replace($fileContent , " ^[\s]*$Name =(.*)($eolChars *)" , $keyValuePair , $regExOptions )
234+ if ($results.Count -eq 0 )
235+ {
236+ # The Key does not exists and should not so don't do anything
237+ return
238+ }
239+ else
240+ {
241+ # The Key exists in the file but should not so remove it
242+ $fileContent = [regex ]::Replace($fileContent , " ^[\s]*$Name =(.*)$eolChars " , ' ' , $regExOptions )
225243
226- Write-Verbose - Message ($localizedData.KeyUpdateMessage -f `
227- $Path , $Name )
244+ Write-Verbose - Message ($localizedData.KeyRemoveMessage -f `
245+ $Path , $Name )
246+ }
228247 } # if
229248 }
230249 else
231250 {
232- if ($results.Count -eq 0 )
233- {
234- # The Key does not exists and should not so don't do anything
235- return
236- }
237- else
238- {
239- # The Key exists in the file but should not so remove it
240- $fileContent = [regex ]::Replace($fileContent , " ^[\s]*$Name =(.*)$eolChars " , ' ' , $regExOptions )
241-
242- Write-Verbose - Message ($localizedData.KeyRemoveMessage -f `
243- $Path , $Name )
244- }
251+ $fileContent = ' {0}={1}' -f $Name , $Text
245252 } # if
246253
247254 Set-Content `
@@ -331,6 +338,12 @@ function Test-TargetResource
331338 # Flag to signal whether settings are correct
332339 [Boolean ] $desiredConfigurationMatch = $true
333340
341+ # Check if file being managed exists. If not return $False.
342+ if (-not (Test-Path - Path $Path ))
343+ {
344+ return $false
345+ }
346+
334347 $fileContent = Get-Content - Path $Path - Raw
335348
336349 Write-Verbose - Message ($localizedData.SearchForKeyMessage -f `
@@ -484,11 +497,12 @@ function Assert-ParametersValid
484497 $IgnoreValueCase = $false
485498 )
486499
487- # Does the file in path exist?
488- if (-not (Test-Path - Path $Path ))
500+ # Does the file's parent path exist?
501+ $parentPath = Split-Path - Path $Path - Parent
502+ if (-not (Test-Path - Path $parentPath ))
489503 {
490504 New-InvalidArgumentException `
491- - Message ($localizedData.FileNotFoundError -f $Path ) `
505+ - Message ($localizedData.FileParentNotFoundError -f $Path ) `
492506 - ArgumentName ' Path'
493507 } # if
494508}
0 commit comments