Skip to content

Commit ee69b94

Browse files
committed
Added AllowAppend parameter
1 parent b6d59fe commit ee69b94

File tree

5 files changed

+229
-89
lines changed

5 files changed

+229
-89
lines changed

DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ function Set-TargetResource
133133
[Parameter()]
134134
[System.Management.Automation.PSCredential]
135135
[System.Management.Automation.Credential()]
136-
$Secret
136+
$Secret,
137+
138+
[Parameter()]
139+
[System.Boolean]
140+
$AllowAppend = $false
137141
)
138142

139143
Assert-ParametersValid @PSBoundParameters
@@ -158,7 +162,7 @@ function Set-TargetResource
158162
# Configuration file does not exist
159163
$fileContent = $Text
160164
}
161-
elseif ( [regex]::Matches($fileContent, $Search).Count -eq 0 )
165+
elseif ([regex]::Matches($fileContent, $Search).Count -eq 0 -and $AllowAppend -eq $true)
162166
{
163167
# Configuration file exists but Text does not exist so lets add it
164168
$fileContent = Add-ConfigurationEntry -FileContent $fileContent -Text $Text
@@ -225,7 +229,11 @@ function Test-TargetResource
225229
[Parameter()]
226230
[System.Management.Automation.PSCredential]
227231
[System.Management.Automation.Credential()]
228-
$Secret
232+
$Secret,
233+
234+
[Parameter()]
235+
[System.Boolean]
236+
$AllowAppend = $false
229237
)
230238

231239
Assert-ParametersValid @PSBoundParameters
@@ -246,11 +254,20 @@ function Test-TargetResource
246254

247255
if ($results.Count -eq 0)
248256
{
257+
if ($AllowAppend -eq $true)
258+
{
259+
# No matches found - but we want to append
260+
Write-Verbose -Message ($localizedData.StringNotFoundMessageAppend -f `
261+
$Path, $Search)
262+
263+
return $false
264+
}
265+
249266
# No matches found - already in state
250267
Write-Verbose -Message ($localizedData.StringNotFoundMessage -f `
251268
$Path, $Search)
252269

253-
return $false
270+
return $true
254271
}
255272

256273
# Flag to signal whether settings are correct
@@ -332,7 +349,11 @@ function Assert-ParametersValid
332349
[Parameter()]
333350
[System.Management.Automation.PSCredential]
334351
[System.Management.Automation.Credential()]
335-
$Secret
352+
$Secret,
353+
354+
[Parameter()]
355+
[System.Boolean]
356+
$AllowAppend = $false
336357
)
337358

338359
# Does the file's parent path exist?
@@ -361,11 +382,11 @@ function Add-ConfigurationEntry
361382
[CmdletBinding()]
362383
param
363384
(
364-
[Parameter()]
385+
[Parameter(Mandatory = $true)]
365386
[String]
366387
$FileContent,
367388

368-
[Parameter()]
389+
[Parameter(Mandatory = $true)]
369390
[String]
370391
$Text
371392
)
@@ -376,7 +397,8 @@ function Add-ConfigurationEntry
376397

377398
foreach ($line in $fileContentArray)
378399
{
379-
$null = $stringBuilder.AppendLine($line)
400+
# Lets remove the return character since AppendLine() always adds one
401+
$null = $stringBuilder.AppendLine($line -replace '\r')
380402
}
381403

382404
$null = $stringBuilder.AppendLine($Text)

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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
ConvertFrom-StringData @'
44
SearchForTextMessage = Searching using RegEx '{1}' in file '{0}'.
5-
StringNotFoundMessage = String not found using RegEx '{1}' in file '{0}', change required.
5+
StringNotFoundMessageAppend = String not found using RegEx '{1}' in file '{0}', change required.
6+
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.
89
StringNoReplacementMessage = String found using RegEx '{1}' in file '{0}', no replacement required.

0 commit comments

Comments
 (0)