Skip to content

Commit 12ceadd

Browse files
authored
Merge pull request #21 from jcwalker/issue#20
Issue#20
2 parents f6b6166 + 40ed0df commit 12ceadd

File tree

6 files changed

+378
-73
lines changed

6 files changed

+378
-73
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
- Refactor Test-TargetResource to return $false in all DSC resource - Fixes
1313
[Issue #12](https://github.com/PlagueHO/FileContentDsc/issues/13).
1414
- Correct configuration names in Examples - fixes [Issue #15](https://github.com/PowerShell/FileContentDsc/issues/15).
15+
- Refactor Test/Set-TargetResource in ReplaceText to be able to add a key if it
16+
doesn't exist but should -Fixes
17+
[Issue#20](https://github.com/PlagueHO/FileContentDsc/issues/20).
1518

1619
## 1.0.0.0
1720

DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ function Get-TargetResource
103103
.PARAMETER Secret
104104
The secret text to replace the text identified by the RegEx.
105105
Only used when Type is set to 'Secret'.
106+
107+
.PARAMETER AllowAppend
108+
Specifies to append text to the file being modified. Adds the ability to add a configuration entry.
106109
#>
107110
function Set-TargetResource
108111
{
@@ -133,7 +136,11 @@ function Set-TargetResource
133136
[Parameter()]
134137
[System.Management.Automation.PSCredential]
135138
[System.Management.Automation.Credential()]
136-
$Secret
139+
$Secret,
140+
141+
[Parameter()]
142+
[System.Boolean]
143+
$AllowAppend = $false
137144
)
138145

139146
Assert-ParametersValid @PSBoundParameters
@@ -155,10 +162,17 @@ function Set-TargetResource
155162

156163
if ($null -eq $fileContent)
157164
{
165+
# Configuration file does not exist
158166
$fileContent = $Text
159167
}
168+
elseif ([regex]::Matches($fileContent, $Search).Count -eq 0 -and $AllowAppend -eq $true)
169+
{
170+
# Configuration file exists but Text does not exist so lets add it
171+
$fileContent = Add-ConfigurationEntry -FileContent $fileContent -Text $Text
172+
}
160173
else
161174
{
175+
# Configuration file exists but Text not in a desired state so lets update it
162176
$fileContent = $fileContent -Replace $Search, $Text
163177
}
164178

@@ -189,6 +203,9 @@ function Set-TargetResource
189203
.PARAMETER Secret
190204
The secret text to replace the text identified by the RegEx.
191205
Only used when Type is set to 'Secret'.
206+
207+
.PARAMETER AllowAppend
208+
Specifies to append text to the file being modified. Adds the ability to add a configuration entry.
192209
#>
193210
function Test-TargetResource
194211
{
@@ -218,7 +235,11 @@ function Test-TargetResource
218235
[Parameter()]
219236
[System.Management.Automation.PSCredential]
220237
[System.Management.Automation.Credential()]
221-
$Secret
238+
$Secret,
239+
240+
[Parameter()]
241+
[System.Boolean]
242+
$AllowAppend = $false
222243
)
223244

224245
Assert-ParametersValid @PSBoundParameters
@@ -239,6 +260,15 @@ function Test-TargetResource
239260

240261
if ($results.Count -eq 0)
241262
{
263+
if ($AllowAppend -eq $true)
264+
{
265+
# No matches found - but we want to append
266+
Write-Verbose -Message ($localizedData.StringNotFoundMessageAppend -f `
267+
$Path, $Search)
268+
269+
return $false
270+
}
271+
242272
# No matches found - already in state
243273
Write-Verbose -Message ($localizedData.StringNotFoundMessage -f `
244274
$Path, $Search)
@@ -325,7 +355,11 @@ function Assert-ParametersValid
325355
[Parameter()]
326356
[System.Management.Automation.PSCredential]
327357
[System.Management.Automation.Credential()]
328-
$Secret
358+
$Secret,
359+
360+
[Parameter()]
361+
[System.Boolean]
362+
$AllowAppend = $false
329363
)
330364

331365
# Does the file's parent path exist?
@@ -338,4 +372,49 @@ function Assert-ParametersValid
338372
} # if
339373
}
340374

375+
<#
376+
.SYNOPSIS
377+
Uses the stringBuilder class to append a configuration entry to the existing file content.
378+
379+
.PARAMETER FileContent
380+
The existing file content of the configuration file.
381+
382+
.PARAMETER Text
383+
The text to append to the end of the FileContent.
384+
#>
385+
function Add-ConfigurationEntry
386+
{
387+
[OutputType([String])]
388+
[CmdletBinding()]
389+
param
390+
(
391+
[Parameter(Mandatory = $true)]
392+
[String]
393+
$FileContent,
394+
395+
[Parameter(Mandatory = $true)]
396+
[String]
397+
$Text
398+
)
399+
400+
if ($FileContent -match '\n$' -and $FileContent -notmatch '\r\n$')
401+
{
402+
# default *nix line ending
403+
$detectedNewLineFormat = "`n"
404+
}
405+
else
406+
{
407+
# default Windows line ending
408+
$detectedNewLineFormat = "`r`n"
409+
}
410+
411+
$stringBuilder = New-Object -TypeName System.Text.StringBuilder
412+
413+
$null = $stringBuilder.Append($FileContent)
414+
$null = $stringBuilder.Append($Text)
415+
$null = $stringBuilder.Append($detectedNewLineFormat)
416+
417+
return $stringBuilder.ToString()
418+
}
419+
341420
Export-ModuleMember -Function *-TargetResource

DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ class DSR_ReplaceText : OMI_BaseResource
55
[Key, Description("The RegEx string to use to search the text file.")] String Search;
66
[Write, Description("Specifies the value type to use as the replacement string. Defaults to 'Text'."),ValueMap{"Text", "Secret"},Values{"Text", "Secret"}] String Type;
77
[Write, Description("The text to replace the text identified by the RegEx. Only used when Type is set to 'Text'.")] String Text;
8-
[write, Description("The secret text to replace the text identified by the RegEx. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret;
8+
[Write, Description("The secret text to replace the text identified by the RegEx. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret;
9+
[Write, Description("Specifies to append text to the file being modified. Adds the ability to add a configuration entry.")] Boolean AllowAppend;
910
};

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
ConvertFrom-StringData @'
44
SearchForTextMessage = Searching using RegEx '{1}' in file '{0}'.
5+
StringNotFoundMessageAppend = String not found using RegEx '{1}' in file '{0}', change required.
56
StringNotFoundMessage = String not found using RegEx '{1}' in file '{0}', change not required.
67
StringMatchFoundMessage = String(s) '{2}' found using RegEx '{1}' in file '{0}'.
78
StringReplacementRequiredMessage = String found using RegEx '{1}' in file '{0}', replacement required.

TestFile.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Setting1=Value1
2+
Setting.Two='Value2'
3+
Setting.Two='Value3'
4+
Setting.Two='TestText'
5+
Setting3.Test=Value4
6+
7+
Setting.NotExist='TestText'Setting1=Value1
8+
Setting.Two='Value2'
9+
Setting.Two='Value3'
10+
Setting.Two='TestText'
11+
Setting3.Test=Value4
12+
13+
Setting.NotExist='TestText'Setting1=Value1
14+
Setting.Two='Value2'
15+
Setting.Two='Value3'
16+
Setting.Two='TestText'
17+
Setting3.Test=Value4
18+
19+
Setting.NotExist='TestText'Setting1=Value1
20+
Setting.Two='Value2'
21+
Setting.Two='Value3'
22+
Setting.Two='TestText'
23+
Setting3.Test=Value4
24+
25+
Setting.NotExist='TestText'

0 commit comments

Comments
 (0)