Skip to content

Commit 2a0e9ba

Browse files
committed
Add UTF8BOM and UTF8NoBOM for Encoding parameter of the KeyValuePairFile and ReplaceText
1 parent ee6e067 commit 2a0e9ba

File tree

10 files changed

+556
-248
lines changed

10 files changed

+556
-248
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
coverage - Fixes [Issue #50](https://github.com/dsccommunity/FileContentDsc/issues/50).
2424
- Automatically publish documentation to GitHub Wiki - Fixes [Issue #51](https://github.com/dsccommunity/FileContentDsc/issues/51).
2525
- Renamed `master` branch to `main` - Fixes [Issue #53](https://github.com/dsccommunity/FileContentDsc/issues/53).
26+
- Added `UTF8BOM` and `UTF8NoBOM` for Encoding parameter of the KeyValuePairFile and ReplaceText - Fixes [Issue #56](https://github.com/dsccommunity/FileContentDsc/issues/56).
2627
- Updated `GitVersion.yml` to latest pattern - Fixes [Issue #57](https://github.com/dsccommunity/FileContentDsc/issues/57).
2728
- Updated build to use `Sampler.GitHubTasks` - Fixes [Issue #60](https://github.com/dsccommunity/FileContentDsc/issues/60).
2829
- Added support for publishing code coverage to `CodeCov.io` and

source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.psm1

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,19 @@ function Get-TargetResource
4747

4848
if (Test-Path -Path $Path)
4949
{
50-
$fileContent = Get-Content -Path $Path -Raw
51-
$fileEncoding = Get-FileEncoding -Path $Path
50+
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
51+
if ($null -eq $fileEncoding)
52+
{
53+
$fileContent = Get-Content -Path $Path -Raw
54+
}
55+
elseif ($fileEncoding -like 'UTF8*')
56+
{
57+
$fileContent = Get-Content -Path $Path -Raw -Encoding 'UTF8'
58+
}
59+
else
60+
{
61+
$fileContent = Get-Content -Path $Path -Raw -Encoding $fileEncoding
62+
}
5263

5364
if ($null -ne $fileContent)
5465
{
@@ -180,15 +191,26 @@ function Set-TargetResource
180191
$IgnoreValueCase = $false,
181192

182193
[Parameter()]
183-
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF32')]
194+
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
184195
[System.String]
185196
$Encoding
186197
)
187198

188199
Assert-ParametersValid @PSBoundParameters
189200

190-
$fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue
191-
$fileEncoding = Get-FileEncoding -Path $Path -ErrorAction SilentlyContinue
201+
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
202+
if ($null -eq $fileEncoding)
203+
{
204+
$fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue
205+
}
206+
elseif ($fileEncoding -like 'UTF8*')
207+
{
208+
$fileContent = Get-Content -Path $Path -Raw -Encoding 'UTF8' -ErrorAction SilentlyContinue
209+
}
210+
else
211+
{
212+
$fileContent = Get-Content -Path $Path -Raw -Encoding $fileEncoding -ErrorAction SilentlyContinue
213+
}
192214

193215
$fileProperties = @{
194216
Path = $Path
@@ -248,10 +270,13 @@ function Set-TargetResource
248270
{
249271
if ($results.Count -eq 0)
250272
{
251-
if ($PSBoundParameters.ContainsKey('Encoding') -and ($Encoding -eq $fileEncoding))
273+
if ($PSBoundParameters.ContainsKey('Encoding') -and `
274+
(($Encoding -eq $fileEncoding) -or `
275+
($Encoding -eq 'UTF8' -and $fileEncoding -like 'UTF8*') -or `
276+
($Encoding -eq 'UTF8NoBOM' -and $fileEncoding -eq 'ASCII')))
252277
{
253-
# The Key does not exists and should not, and encoding is in the desired state, so don't do anything
254-
return
278+
# The Key does not exists and should not, and encoding is in the desired state, so don't do anything
279+
return
255280
}
256281
else
257282
{
@@ -281,7 +306,7 @@ function Set-TargetResource
281306
$fileProperties.Add('Encoding', $Encoding)
282307
}
283308

284-
Set-Content @fileProperties
309+
Set-TextContent @fileProperties
285310
}
286311

287312
<#
@@ -362,7 +387,7 @@ function Test-TargetResource
362387
$IgnoreValueCase = $false,
363388

364389
[Parameter()]
365-
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF32')]
390+
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
366391
[System.String]
367392
$Encoding
368393
)
@@ -380,7 +405,19 @@ function Test-TargetResource
380405
return ($Ensure -eq 'Absent')
381406
}
382407

383-
$fileContent = Get-Content -Path $Path -Raw
408+
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
409+
if ($null -eq $fileEncoding)
410+
{
411+
$fileContent = Get-Content -Path $Path -Raw
412+
}
413+
elseif ($fileEncoding -like 'UTF8*')
414+
{
415+
$fileContent = Get-Content -Path $Path -Raw -Encoding 'UTF8'
416+
}
417+
else
418+
{
419+
$fileContent = Get-Content -Path $Path -Raw -Encoding $fileEncoding
420+
}
384421

385422
if ($null -eq $fileContent)
386423
{
@@ -390,7 +427,6 @@ function Test-TargetResource
390427
}
391428

392429
$desiredConfigurationMatch = $true
393-
$fileEncoding = Get-FileEncoding -Path $Path
394430
$regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline
395431

396432
Write-Verbose -Message ($script:localizedData.SearchForKeyMessage -f $Path, $Name)
@@ -456,10 +492,23 @@ function Test-TargetResource
456492

457493
if ($PSBoundParameters.ContainsKey('Encoding') -and ($Encoding -ne $fileEncoding))
458494
{
459-
# File encoding is not in desired state
460-
Write-Verbose -Message ($script:localizedData.FileEncodingNotInDesiredState -f $fileEncoding, $Encoding)
495+
if ($Encoding -eq 'UTF8' -and $fileEncoding -like 'UTF8*')
496+
{
497+
#If the Encoding specified as UTF8, Either UTF8NoBOM or UTF8BOM is acceptable
498+
$desiredConfigurationMatch = $true
499+
}
500+
elseif ($Encoding -eq 'UTF8NoBOM' -and $fileEncoding -eq 'ASCII')
501+
{
502+
#If the Encoding specified as UTF8NoBOM, Either UTF8NoBOM or ASCII is acceptable
503+
$desiredConfigurationMatch = $true
504+
}
505+
else
506+
{
507+
# File encoding is not in desired state
508+
Write-Verbose -Message ($script:localizedData.FileEncodingNotInDesiredState -f $fileEncoding, $Encoding)
461509

462-
$desiredConfigurationMatch = $false
510+
$desiredConfigurationMatch = $false
511+
}
463512
}
464513

465514
return $desiredConfigurationMatch
@@ -543,7 +592,7 @@ function Assert-ParametersValid
543592
$IgnoreValueCase = $false,
544593

545594
[Parameter()]
546-
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF32')]
595+
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
547596
[System.String]
548597
$Encoding
549598
)

source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.schema.mof

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ class DSC_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;
12+
[Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF8BOM", "UTF8NoBOM", "UTF32"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF8BOM", "UTF8NoBOM", "UTF32"}] String Encoding;
1313
};

source/DSCResources/DSC_ReplaceText/DSC_ReplaceText.psm1

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,22 @@ function Get-TargetResource
4141

4242
Assert-ParametersValid @PSBoundParameters
4343

44-
$fileContent = Get-Content -Path $Path -Raw
45-
$fileEncoding = Get-FileEncoding $Path
44+
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
45+
if ($null -eq $fileEncoding)
46+
{
47+
$fileContent = Get-Content -Path $Path -Raw
48+
}
49+
elseif ($fileEncoding -like 'UTF8*')
50+
{
51+
$fileContent = Get-Content -Path $Path -Raw -Encoding 'UTF8'
52+
}
53+
else
54+
{
55+
$fileContent = Get-Content -Path $Path -Raw -Encoding $fileEncoding
56+
}
4657

4758
Write-Verbose -Message ($script:localizedData.SearchForTextMessage -f `
48-
$Path, $Search)
59+
$Path, $Search)
4960

5061
$text = ''
5162

@@ -56,14 +67,14 @@ function Get-TargetResource
5667
{
5768
# No matches found - already in state
5869
Write-Verbose -Message ($script:localizedData.StringNotFoundMessage -f `
59-
$Path, $Search)
70+
$Path, $Search)
6071
}
6172
else
6273
{
6374
$text = ($results.Value -join ',')
6475

6576
Write-Verbose -Message ($script:localizedData.StringMatchFoundMessage -f `
66-
$Path, $Search, $text)
77+
$Path, $Search, $text)
6778
} # if
6879

6980
return @{
@@ -136,15 +147,26 @@ function Set-TargetResource
136147
$AllowAppend = $false,
137148

138149
[Parameter()]
139-
[ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")]
150+
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
140151
[System.String]
141152
$Encoding
142153
)
143154

144155
Assert-ParametersValid @PSBoundParameters
145156

146-
$fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue
147-
$fileEncoding = Get-FileEncoding $Path
157+
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
158+
if ($null -eq $fileEncoding)
159+
{
160+
$fileContent = Get-Content -Path $Path -Raw
161+
}
162+
elseif ($fileEncoding -like 'UTF8*')
163+
{
164+
$fileContent = Get-Content -Path $Path -Raw -Encoding 'UTF8'
165+
}
166+
else
167+
{
168+
$fileContent = Get-Content -Path $Path -Raw -Encoding $fileEncoding
169+
}
148170

149171
$fileProperties = @{
150172
Path = $Path
@@ -155,25 +177,17 @@ function Set-TargetResource
155177
if ($Type -eq 'Secret')
156178
{
157179
Write-Verbose -Message ($script:localizedData.StringReplaceSecretMessage -f `
158-
$Path)
180+
$Path)
159181

160182
$Text = $Secret.GetNetworkCredential().Password
161183
}
162184
elseif ($PSBoundParameters.ContainsKey('Encoding'))
163185
{
164-
if ($Encoding -eq $fileEncoding)
165-
{
166-
Write-Verbose -Message ($script:localizedData.StringReplaceTextMessage -f `
167-
$Path, $Text)
168-
}
169-
else
170-
{
171-
Write-Verbose -Message ($script:localizedData.StringReplaceTextMessage -f `
172-
$Path, $Text)
186+
Write-Verbose -Message ($script:localizedData.StringReplaceTextMessage -f `
187+
$Path, $Text)
173188

174-
# Add encoding parameter and value to the hashtable
175-
$fileProperties.Add('Encoding', $Encoding)
176-
}
189+
# Add encoding parameter and value to the hashtable
190+
$fileProperties.Add('Encoding', $Encoding)
177191
}
178192

179193
if ($null -eq $fileContent)
@@ -194,7 +208,7 @@ function Set-TargetResource
194208

195209
$fileProperties.Add('Value', $fileContent)
196210

197-
Set-Content @fileProperties
211+
Set-TextContent @fileProperties
198212
}
199213

200214
<#
@@ -259,7 +273,7 @@ function Test-TargetResource
259273
$AllowAppend = $false,
260274

261275
[Parameter()]
262-
[ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")]
276+
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
263277
[System.String]
264278
$Encoding
265279
)
@@ -272,11 +286,22 @@ function Test-TargetResource
272286
return $false
273287
}
274288

275-
$fileContent = Get-Content -Path $Path -Raw
276-
$fileEncoding = Get-FileEncoding $Path
289+
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
290+
if ($null -eq $fileEncoding)
291+
{
292+
$fileContent = Get-Content -Path $Path -Raw
293+
}
294+
elseif ($fileEncoding -like 'UTF8*')
295+
{
296+
$fileContent = Get-Content -Path $Path -Raw -Encoding 'UTF8'
297+
}
298+
else
299+
{
300+
$fileContent = Get-Content -Path $Path -Raw -Encoding $fileEncoding
301+
}
277302

278303
Write-Verbose -Message ($script:localizedData.SearchForTextMessage -f `
279-
$Path, $Search)
304+
$Path, $Search)
280305

281306
# Search the file content for any matches
282307
$results = [regex]::Matches($fileContent, $Search)
@@ -293,19 +318,21 @@ function Test-TargetResource
293318
}
294319
if ($PSBoundParameters.ContainsKey('Encoding'))
295320
{
296-
if ($Encoding -eq $fileEncoding)
321+
if (($Encoding -eq $fileEncoding) -or `
322+
($Encoding -eq 'UTF8' -and $fileEncoding -like 'UTF8*') -or `
323+
($Encoding -eq 'UTF8NoBOM' -and $fileEncoding -eq 'ASCII'))
297324
{
298325
# No matches found and encoding is in desired state
299326
Write-Verbose -Message ($script:localizedData.StringNotFoundMessage -f `
300-
$Path, $Search)
327+
$Path, $Search)
301328

302329
return $true
303330
}
304331
else
305332
{
306333
# No matches found but encoding is not in desired state
307334
Write-Verbose -Message ($script:localizedData.FileEncodingNotInDesiredState -f `
308-
$fileEncoding, $Encoding)
335+
$fileEncoding, $Encoding)
309336

310337
return $false
311338
}
@@ -331,12 +358,12 @@ function Test-TargetResource
331358
if ($desiredConfigurationMatch)
332359
{
333360
Write-Verbose -Message ($script:localizedData.StringNoReplacementMessage -f `
334-
$Path, $Search)
361+
$Path, $Search)
335362
}
336363
else
337364
{
338365
Write-Verbose -Message ($script:localizedData.StringReplacementRequiredMessage -f `
339-
$Path, $Search)
366+
$Path, $Search)
340367
} # if
341368

342369
return $desiredConfigurationMatch
@@ -401,7 +428,7 @@ function Assert-ParametersValid
401428
$AllowAppend = $false,
402429

403430
[Parameter()]
404-
[ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")]
431+
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
405432
[System.String]
406433
$Encoding
407434
)

source/DSCResources/DSC_ReplaceText/DSC_ReplaceText.schema.mof

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ class DSC_ReplaceText : OMI_BaseResource
77
[Write, Description("The text to replace the text identified by the RegEx. Only used when Type is set to 'Text'.")] String Text;
88
[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;
99
[Write, Description("Specifies to append text to the file being modified. Adds the ability to add a configuration entry.")] Boolean AllowAppend;
10-
[Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"}] String Encoding;
10+
[Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF8BOM", "UTF8NoBOM", "UTF32"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF8BOM", "UTF8NoBOM", "UTF32"}] String Encoding;
1111
};

0 commit comments

Comments
 (0)