Skip to content

Commit d81ed2b

Browse files
authored
Merge pull request #27 from nehrua/Issue#5
KeyValuePairFile and ReplaceText: Adds a encoding param - Fixes Issue#5
2 parents 162df55 + 2f586f2 commit d81ed2b

16 files changed

Lines changed: 1050 additions & 152 deletions

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
## Unreleased
44

55
- Added .VSCode settings for applying DSC PSSA rules - fixes [Issue #25](https://github.com/PlagueHO/FileContentDsc/issues/25).
6+
- Added an Encoding parameter to the KeyValuePairFile and ReplaceText
7+
resources - fixes [Issue #5](https://github.com/PlagueHO/FileContentDsc/issues/5).
68

79
## 1.1.0.0
810

DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1

Lines changed: 96 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,22 @@ function Get-TargetResource
4040
(
4141
[Parameter(Mandatory = $true)]
4242
[ValidateNotNullOrEmpty()]
43-
[String]
43+
[System.String]
4444
$Path,
4545

4646
[Parameter(Mandatory = $true)]
4747
[ValidateNotNullOrEmpty()]
48-
[String]
48+
[System.String]
4949
$Name
5050
)
5151

5252
Assert-ParametersValid @PSBoundParameters
5353

5454
$fileContent = Get-Content -Path $Path -Raw
55+
$fileEncoding = Get-FileEncoding -Path $Path
5556

5657
Write-Verbose -Message ($localizedData.SearchForKeyMessage -f `
57-
$Path, $Name)
58+
$Path, $Name)
5859

5960
# Setup the Regex Options that will be used
6061
$regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline
@@ -69,7 +70,7 @@ function Get-TargetResource
6970
{
7071
# No matches found
7172
Write-Verbose -Message ($localizedData.KeyNotFoundMessage -f `
72-
$Path, $Name)
73+
$Path, $Name)
7374
}
7475
else
7576
{
@@ -85,12 +86,13 @@ function Get-TargetResource
8586
$text = ($textValues -join ',')
8687

8788
Write-Verbose -Message ($localizedData.KeyFoundMessage -f `
88-
$Path, $Name, $text)
89+
$Path, $Name, $text)
8990
} # if
9091

9192
return @{
9293
Path = $Path
9394
Name = $Name
95+
Encoding = $fileEncoding
9496
Ensure = $ensure
9597
Type = 'Text'
9698
Text = $text
@@ -129,6 +131,9 @@ function Get-TargetResource
129131
.PARAMETER IgnoreValueCase
130132
Ignore the case of any text or secret when determining if it they need to be updated.
131133
Defaults to $False.
134+
135+
.PARAMETER Encoding
136+
Specifies the file encoding. Defaults to ASCII.
132137
#>
133138
function Set-TargetResource
134139
{
@@ -139,26 +144,26 @@ function Set-TargetResource
139144
(
140145
[Parameter(Mandatory = $true)]
141146
[ValidateNotNullOrEmpty()]
142-
[String]
147+
[System.String]
143148
$Path,
144149

145150
[Parameter(Mandatory = $true)]
146151
[ValidateNotNullOrEmpty()]
147-
[String]
152+
[System.String]
148153
$Name,
149154

150155
[Parameter()]
151156
[ValidateSet('Present', 'Absent')]
152-
[String]
157+
[System.String]
153158
$Ensure = 'Present',
154159

155160
[Parameter()]
156161
[ValidateSet('Text', 'Secret')]
157-
[String]
162+
[System.String]
158163
$Type = 'Text',
159164

160165
[Parameter()]
161-
[String]
166+
[System.String]
162167
$Text,
163168

164169
[Parameter()]
@@ -172,15 +177,27 @@ function Set-TargetResource
172177

173178
[Parameter()]
174179
[System.Boolean]
175-
$IgnoreValueCase = $false
180+
$IgnoreValueCase = $false,
181+
182+
[Parameter()]
183+
[ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")]
184+
[System.String]
185+
$Encoding
176186
)
177187

178188
Assert-ParametersValid @PSBoundParameters
179189

180190
$fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue
191+
$fileEncoding = Get-FileEncoding -Path $Path
192+
193+
$fileProperties = @{
194+
Path = $Path
195+
NoNewline = $true
196+
Force = $true
197+
}
181198

182199
Write-Verbose -Message ($localizedData.SearchForKeyMessage -f `
183-
$Path, $Name)
200+
$Path, $Name)
184201

185202
if ($Type -eq 'Secret')
186203
{
@@ -218,31 +235,39 @@ function Set-TargetResource
218235
$fileContent += $keyValuePair
219236

220237
Write-Verbose -Message ($localizedData.KeyAddMessage -f `
221-
$Path, $Name)
238+
$Path, $Name)
222239
}
223240
else
224241
{
225242
# The key value pair was found so update it
226243
$fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)($eolChars*)", $keyValuePair, $regExOptions)
227244

228245
Write-Verbose -Message ($localizedData.KeyUpdateMessage -f `
229-
$Path, $Name)
246+
$Path, $Name)
230247
} # if
231248
}
232249
else
233250
{
234251
if ($results.Count -eq 0)
235252
{
236-
# The Key does not exists and should not so don't do anything
237-
return
253+
if ($PSBoundParameters.ContainsKey('Encoding') -and ($Encoding -eq $fileEncoding))
254+
{
255+
# The Key does not exists and should not, and encoding is in the desired state, so don't do anything
256+
return
257+
}
258+
else
259+
{
260+
Write-Verbose -Message ($localizedData.FileEncodingNotInDesiredState -f `
261+
$fileEncoding, $Encoding)
262+
}
238263
}
239264
else
240265
{
241266
# The Key exists in the file but should not so remove it
242267
$fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)$eolChars", '', $regExOptions)
243268

244269
Write-Verbose -Message ($localizedData.KeyRemoveMessage -f `
245-
$Path, $Name)
270+
$Path, $Name)
246271
}
247272
} # if
248273
}
@@ -251,11 +276,16 @@ function Set-TargetResource
251276
$fileContent = '{0}={1}' -f $Name, $Text
252277
} # if
253278

254-
Set-Content `
255-
-Path $Path `
256-
-Value $fileContent `
257-
-NoNewline `
258-
-Force
279+
$fileProperties.Add('Value', $fileContent)
280+
281+
# Verify encoding is not set to the passed parameter or the default of ASCII
282+
if ($PSBoundParameters.ContainsKey('Encoding') -and ($Encoding -ne ($fileEncoding -or 'ASCII')))
283+
{
284+
# Add encoding parameter and value to the hashtable
285+
$fileProperties.Add('Encoding', $Encoding)
286+
}
287+
288+
Set-Content @fileProperties
259289
}
260290

261291
<#
@@ -288,6 +318,9 @@ function Set-TargetResource
288318
.PARAMETER IgnoreValueCase
289319
Ignore the case of any text or secret when determining if it they need to be updated.
290320
Defaults to $False.
321+
322+
.PARAMETER Encoding
323+
Specifies the file encoding. Defaults to ASCII.
291324
#>
292325
function Test-TargetResource
293326
{
@@ -297,26 +330,26 @@ function Test-TargetResource
297330
(
298331
[Parameter(Mandatory = $true)]
299332
[ValidateNotNullOrEmpty()]
300-
[String]
333+
[System.String]
301334
$Path,
302335

303336
[Parameter(Mandatory = $true)]
304337
[ValidateNotNullOrEmpty()]
305-
[String]
338+
[System.String]
306339
$Name,
307340

308341
[Parameter()]
309342
[ValidateSet('Present', 'Absent')]
310-
[String]
343+
[System.String]
311344
$Ensure = 'Present',
312345

313346
[Parameter()]
314347
[ValidateSet('Text', 'Secret')]
315-
[String]
348+
[System.String]
316349
$Type = 'Text',
317350

318351
[Parameter()]
319-
[String]
352+
[System.String]
320353
$Text,
321354

322355
[Parameter()]
@@ -330,7 +363,12 @@ function Test-TargetResource
330363

331364
[Parameter()]
332365
[System.Boolean]
333-
$IgnoreValueCase = $false
366+
$IgnoreValueCase = $false,
367+
368+
[Parameter()]
369+
[ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")]
370+
[System.String]
371+
$Encoding
334372
)
335373

336374
Assert-ParametersValid @PSBoundParameters
@@ -345,9 +383,10 @@ function Test-TargetResource
345383
}
346384

347385
$fileContent = Get-Content -Path $Path -Raw
386+
$fileEncoding = Get-FileEncoding -Path $Path
348387

349388
Write-Verbose -Message ($localizedData.SearchForKeyMessage -f `
350-
$Path, $Name)
389+
$Path, $Name)
351390

352391
# Setup the Regex Options that will be used
353392
$regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline
@@ -366,20 +405,20 @@ function Test-TargetResource
366405
{
367406
# The key value pairs should exist but do not
368407
Write-Verbose -Message ($localizedData.KeyNotFoundButShouldExistMessage -f `
369-
$Path, $Name)
408+
$Path, $Name)
370409

371410
$desiredConfigurationMatch = $false
372411
}
373412
else
374413
{
375414
# The key value pairs should exist and do
376415
Write-Verbose -Message ($localizedData.KeyNotFoundAndShouldNotExistMessage -f `
377-
$Path, $Name)
416+
$Path, $Name)
378417
} # if
379418
}
380419
else
381420
{
382-
# One of more key value pairs were found
421+
# One or more key value pairs were found
383422
if ($Ensure -eq 'Present')
384423
{
385424
# The key value pairs should exist - but check values
@@ -401,24 +440,28 @@ function Test-TargetResource
401440
if ($desiredConfigurationMatch)
402441
{
403442
Write-Verbose -Message ($localizedData.KeyFoundButNoReplacementMessage -f `
404-
$Path, $Name)
443+
$Path, $Name)
405444
}
406-
else
407-
{
408-
Write-Verbose -Message ($localizedData.KeyFoundReplacementRequiredMessage -f `
409-
$Path, $Name)
410-
} # if
411445
}
412446
else
413447
{
414448
# The key value pairs should not exist
415449
Write-Verbose -Message ($localizedData.KeyFoundButShouldNotExistMessage -f `
416-
$Path, $Name)
450+
$Path, $Name)
417451

418452
$desiredConfigurationMatch = $false
419453
} # if
420454
} # if
421455

456+
if ($PSBoundParameters.ContainsKey('Encoding') -and ($Encoding -ne $fileEncoding))
457+
{
458+
# File encoding is not in desired state
459+
Write-Verbose -Message ($localizedData.FileEncodingNotInDesiredState -f `
460+
$fileEncoding, $Encoding)
461+
462+
$desiredConfigurationMatch = $false
463+
}
464+
422465
return $desiredConfigurationMatch
423466
}
424467

@@ -453,6 +496,9 @@ function Test-TargetResource
453496
.PARAMETER IgnoreValueCase
454497
Ignore the case of any text or secret when determining if it they need to be updated.
455498
Defaults to $False.
499+
500+
.PARAMETER Encoding
501+
Specifies the file encoding. Defaults to ASCII.
456502
#>
457503
function Assert-ParametersValid
458504
{
@@ -461,26 +507,26 @@ function Assert-ParametersValid
461507
(
462508
[Parameter(Mandatory = $true)]
463509
[ValidateNotNullOrEmpty()]
464-
[String]
510+
[System.String]
465511
$Path,
466512

467513
[Parameter(Mandatory = $true)]
468514
[ValidateNotNullOrEmpty()]
469-
[String]
515+
[System.String]
470516
$Name,
471517

472518
[Parameter()]
473519
[ValidateSet('Present', 'Absent')]
474-
[String]
520+
[System.String]
475521
$Ensure = 'Present',
476522

477523
[Parameter()]
478524
[ValidateSet('Text', 'Secret')]
479-
[String]
525+
[System.String]
480526
$Type = 'Text',
481527

482528
[Parameter()]
483-
[String]
529+
[System.String]
484530
$Text,
485531

486532
[Parameter()]
@@ -494,7 +540,12 @@ function Assert-ParametersValid
494540

495541
[Parameter()]
496542
[System.Boolean]
497-
$IgnoreValueCase = $false
543+
$IgnoreValueCase = $false,
544+
545+
[Parameter()]
546+
[ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")]
547+
[System.String]
548+
$Encoding
498549
)
499550

500551
# Does the file's parent path exist?

DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ class DSR_KeyValuePairFile : OMI_BaseResource
99
[write, Description("The secret text to replace the value with in the identified key. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret;
1010
[Write, Description("Ignore the case of the name of the key. Defaults to $False.")] Boolean IgnoreNameCase;
1111
[Write, Description("Ignore the case of any text or secret when determining if it they need to be updated. Defaults to $False.")] Boolean IgnoreValueCase;
12+
[Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"}] String Encoding;
1213
};

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ ConvertFrom-StringData @'
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.
1717
FileParentNotFoundError = File parent path '{0}' not found.
18+
FileEncodingNotInDesiredState = File encoding is set to '{0}' but should be set to '{1}', Change required.
1819
'@

0 commit comments

Comments
 (0)