Skip to content

Commit 9c379d6

Browse files
authored
Merge pull request #14 from jcwalker/issue#13
Issue#13 - Update all DSC resources Test-TargetResource to return $false when file being manged does not exist.
2 parents e4a289c + 83eaab0 commit 9c379d6

11 files changed

Lines changed: 493 additions & 73 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ node_modules
1212
node_modules/*
1313
markdownissues.txt
1414
TestResults.xml
15+
launch.json

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
to root folder of repository and remove test harness - Fixes [Issue #11](https://github.com/PlagueHO/FileContentDsc/issues/11).
1010
- Converted Examples to support format for publishing to PowerShell
1111
Gallery.
12+
- Refactor Test-TargetResource to return $false in all DSC resource -Fixes [Issue #12](https://github.com/PlagueHO/FileContentDsc/issues/13)
1213

1314
## 1.0.0.0
1415

DSCResources/DSR_IniSettingsFile/DSR_IniSettingsFile.psm1

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ function Set-TargetResource
136136

137137
Assert-ParametersValid @PSBoundParameters
138138

139+
if (-not (Test-Path -Path $Path))
140+
{
141+
Out-File -FilePath $Path -Force
142+
}
143+
139144
if ($Type -eq 'Secret')
140145
{
141146
Write-Verbose -Message ($localizedData.SetIniSettingSecretMessage -f `
@@ -220,6 +225,12 @@ function Test-TargetResource
220225

221226
Assert-ParametersValid @PSBoundParameters
222227

228+
# Check if file being managed exists. If not return $False.
229+
if (-not (Test-Path -Path $Path))
230+
{
231+
return $false
232+
}
233+
223234
if ($Type -eq 'Secret')
224235
{
225236
$Text = $Secret.GetNetworkCredential().Password
@@ -306,11 +317,12 @@ function Assert-ParametersValid
306317
$Secret
307318
)
308319

309-
# Does the file in path exist?
310-
if (-not (Test-Path -Path $Path))
320+
# Does the file's parent path exist?
321+
$parentPath = Split-Path -Path $Path -Parent
322+
if (-not (Test-Path -Path $parentPath))
311323
{
312324
New-InvalidArgumentException `
313-
-Message ($localizedData.FileNotFoundError -f $Path) `
325+
-Message ($localizedData.FileParentNotFoundError -f $parentPath) `
314326
-ArgumentName 'Path'
315327
} # if
316328
}

DSCResources/DSR_IniSettingsFile/en-US/DSR_IniSettingsFile.strings.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ ConvertFrom-StringData @'
66
SetIniSettingSecretMessage = Setting the entry '{1}' key '{2}' to secret text in INI settings file '{0}'.
77
IniSettingMatchesMessage = The entry '{1}' key '{2}' in INI settings file '{0}' is in the correct state. Change not required.
88
IniSettingMismatchMessage = The entry '{1}' key '{2}' in INI settings file '{0}' is not in the correct state. Change required.
9-
FileNotFoundError = File '{0}' not found.
9+
FileParentNotFoundError = File parent path '{0}' not found.
1010
'@

DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ ConvertFrom-StringData @'
1414
KeyFoundButNoReplacementMessage = Key '{1}' found in file '{0}' and should exist and value(s) are correct. Change not required.
1515
KeyFoundReplacementRequiredMessage = Key '{1}' found in file '{0}' and should exist but value(s) are not correct. Change required.
1616
KeyFoundButShouldNotExistMessage = Key '{1}' found in file '{0}' but should not exist. Change required.
17-
FileNotFoundError = File '{0}' not found.
17+
FileParentNotFoundError = File parent path '{0}' not found.
1818
'@

DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ function Set-TargetResource
138138

139139
Assert-ParametersValid @PSBoundParameters
140140

141-
$fileContent = Get-Content -Path $Path -Raw
141+
$fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue
142142

143143
if ($Type -eq 'Secret')
144144
{
@@ -153,7 +153,14 @@ function Set-TargetResource
153153
$Path, $Text)
154154
} # if
155155

156-
$fileContent = $fileContent -Replace $Search, $Text
156+
if ($null -eq $fileContent)
157+
{
158+
$fileContent = $Text
159+
}
160+
else
161+
{
162+
$fileContent = $fileContent -Replace $Search, $Text
163+
}
157164

158165
Set-Content `
159166
-Path $Path `
@@ -216,6 +223,12 @@ function Test-TargetResource
216223

217224
Assert-ParametersValid @PSBoundParameters
218225

226+
# Check if file being managed exists. If not return $False.
227+
if (-not (Test-Path -Path $Path))
228+
{
229+
return $false
230+
}
231+
219232
$fileContent = Get-Content -Path $Path -Raw
220233

221234
Write-Verbose -Message ($localizedData.SearchForTextMessage -f `
@@ -315,11 +328,12 @@ function Assert-ParametersValid
315328
$Secret
316329
)
317330

318-
# Does the file in path exist?
319-
if (-not (Test-Path -Path $Path))
331+
# Does the file's parent path exist?
332+
$parentPath = Split-Path -Path $Path -Parent
333+
if (-not (Test-Path -Path $parentPath))
320334
{
321335
New-InvalidArgumentException `
322-
-Message ($localizedData.FileNotFoundError -f $Path) `
336+
-Message ($localizedData.FileParentNotFoundError -f $parentPath) `
323337
-ArgumentName 'Path'
324338
} # if
325339
}

DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ ConvertFrom-StringData @'
88
StringNoReplacementMessage = String found using RegEx '{1}' in file '{0}', no replacement required.
99
StringReplaceTextMessage = String replaced by '{1}' in file '{0}'.
1010
StringReplaceSecretMessage = String replaced by secret text in file '{0}'.
11-
FileNotFoundError = File '{0}' not found.
11+
FileParentNotFoundError = File parent path '{0}' not found.
1212
'@

0 commit comments

Comments
 (0)