Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
coverage - Fixes [Issue #50](https://github.com/dsccommunity/FileContentDsc/issues/50).
- Automatically publish documentation to GitHub Wiki - Fixes [Issue #51](https://github.com/dsccommunity/FileContentDsc/issues/51).
- Renamed `master` branch to `main` - Fixes [Issue #53](https://github.com/dsccommunity/FileContentDsc/issues/53).
- Added `UTF8BOM` and `UTF8NoBOM` options to the Encoding parameter of the KeyValuePairFile and ReplaceText - Fixes [Issue #56](https://github.com/dsccommunity/FileContentDsc/issues/56).
- Updated `GitVersion.yml` to latest pattern - Fixes [Issue #57](https://github.com/dsccommunity/FileContentDsc/issues/57).
- Updated build to use `Sampler.GitHubTasks` - Fixes [Issue #60](https://github.com/dsccommunity/FileContentDsc/issues/60).
- Added support for publishing code coverage to `CodeCov.io` and
Expand All @@ -37,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- CI Pipeline
- Updated pipeline files to match current DSC Community patterns - fixes [Issue #71](https://github.com/dsccommunity/FileContentDsc/issues/71).
- Updated HQRM and build steps to use windows-latest image.
- Keep GitVersion.Tool as v5

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ stages:
vmImage: 'windows-latest'
steps:
- pwsh: |
dotnet tool install --global GitVersion.Tool
dotnet tool install --global GitVersion.Tool --version 5.*
$gitVersionObject = dotnet-gitversion | ConvertFrom-Json
$gitVersionObject.PSObject.Properties.ForEach{
Write-Host -Object "Setting Task Variable '$($_.Name)' with value '$($_.Value)'."
Expand Down
51 changes: 33 additions & 18 deletions source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ function Get-TargetResource

if (Test-Path -Path $Path)
{
$fileContent = Get-Content -Path $Path -Raw
$fileEncoding = Get-FileEncoding -Path $Path
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
$fileContent = Get-FileContent -Path $Path -Encoding $fileEncoding

if ($null -ne $fileContent)
if (-not [string]::IsNullOrEmpty($fileContent))
{
Write-Verbose -Message ($script:localizedData.SearchForKeyMessage -f $Path, $Name)

Expand Down Expand Up @@ -180,15 +180,15 @@ function Set-TargetResource
$IgnoreValueCase = $false,

[Parameter()]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF32')]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)

Assert-ParametersValid @PSBoundParameters

$fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue
$fileEncoding = Get-FileEncoding -Path $Path -ErrorAction SilentlyContinue
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
$fileContent = Get-FileContent -Path $Path -Encoding $fileEncoding -ErrorAction SilentlyContinue

$fileProperties = @{
Path = $Path
Expand Down Expand Up @@ -248,10 +248,12 @@ function Set-TargetResource
{
if ($results.Count -eq 0)
{
if ($PSBoundParameters.ContainsKey('Encoding') -and ($Encoding -eq $fileEncoding))
if ($PSBoundParameters.ContainsKey('Encoding') -and `
(Test-FileEncodingEqual -FileEncoding $fileEncoding -ExpectedEncoding $Encoding)
)
{
# The Key does not exists and should not, and encoding is in the desired state, so don't do anything
return
# The Key does not exists and should not, and encoding is in the desired state, so don't do anything
return
}
else
{
Expand Down Expand Up @@ -281,7 +283,7 @@ function Set-TargetResource
$fileProperties.Add('Encoding', $Encoding)
}

Set-Content @fileProperties
Set-TextContent @fileProperties
}

<#
Expand Down Expand Up @@ -362,7 +364,7 @@ function Test-TargetResource
$IgnoreValueCase = $false,

[Parameter()]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF32')]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)
Expand All @@ -380,17 +382,17 @@ function Test-TargetResource
return ($Ensure -eq 'Absent')
}

$fileContent = Get-Content -Path $Path -Raw
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
$fileContent = Get-FileContent -Path $Path -Encoding $fileEncoding

if ($null -eq $fileContent)
if ([string]::IsNullOrEmpty($fileContent))
{
Write-Verbose -Message ($script:localizedData.KeyValuePairFileIsEmpty -f $Path)

return ($Ensure -eq 'Absent')
}

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

Write-Verbose -Message ($script:localizedData.SearchForKeyMessage -f $Path, $Name)
Expand Down Expand Up @@ -456,10 +458,23 @@ function Test-TargetResource

if ($PSBoundParameters.ContainsKey('Encoding') -and ($Encoding -ne $fileEncoding))
{
# File encoding is not in desired state
Write-Verbose -Message ($script:localizedData.FileEncodingNotInDesiredState -f $fileEncoding, $Encoding)
if ($Encoding -eq 'UTF8' -and $fileEncoding -like 'UTF8*')
{
#If the Encoding specified as UTF8, Either UTF8NoBOM or UTF8BOM is acceptable
$desiredConfigurationMatch = $true
}
elseif ($Encoding -eq 'UTF8NoBOM' -and $fileEncoding -eq 'ASCII')
{
#If the Encoding specified as UTF8NoBOM, Either UTF8NoBOM or ASCII is acceptable
$desiredConfigurationMatch = $true
}
else
{
# File encoding is not in desired state
Write-Verbose -Message ($script:localizedData.FileEncodingNotInDesiredState -f $fileEncoding, $Encoding)

$desiredConfigurationMatch = $false
$desiredConfigurationMatch = $false
}
}

return $desiredConfigurationMatch
Expand Down Expand Up @@ -543,7 +558,7 @@ function Assert-ParametersValid
$IgnoreValueCase = $false,

[Parameter()]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF32')]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ class DSC_KeyValuePairFile : OMI_BaseResource
[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;
[Write, Description("Ignore the case of the name of the key. Defaults to $False.")] Boolean IgnoreNameCase;
[Write, Description("Ignore the case of any text or secret when determining if it they need to be updated. Defaults to $False.")] Boolean IgnoreValueCase;
[Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"}] String Encoding;
[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;
};
56 changes: 24 additions & 32 deletions source/DSCResources/DSC_ReplaceText/DSC_ReplaceText.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ function Get-TargetResource

Assert-ParametersValid @PSBoundParameters

$fileContent = Get-Content -Path $Path -Raw
$fileEncoding = Get-FileEncoding $Path
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
$fileContent = Get-FileContent -Path $Path -Encoding $fileEncoding

Write-Verbose -Message ($script:localizedData.SearchForTextMessage -f `
$Path, $Search)
$Path, $Search)

$text = ''

Expand All @@ -56,14 +56,14 @@ function Get-TargetResource
{
# No matches found - already in state
Write-Verbose -Message ($script:localizedData.StringNotFoundMessage -f `
$Path, $Search)
$Path, $Search)
}
else
{
$text = ($results.Value -join ',')

Write-Verbose -Message ($script:localizedData.StringMatchFoundMessage -f `
$Path, $Search, $text)
$Path, $Search, $text)
} # if

return @{
Expand Down Expand Up @@ -136,15 +136,15 @@ function Set-TargetResource
$AllowAppend = $false,

[Parameter()]
[ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)

Assert-ParametersValid @PSBoundParameters

$fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue
$fileEncoding = Get-FileEncoding $Path
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
$fileContent = Get-FileContent -Path $Path -Encoding $fileEncoding -ErrorAction SilentlyContinue

$fileProperties = @{
Path = $Path
Expand All @@ -155,25 +155,17 @@ function Set-TargetResource
if ($Type -eq 'Secret')
{
Write-Verbose -Message ($script:localizedData.StringReplaceSecretMessage -f `
$Path)
$Path)

$Text = $Secret.GetNetworkCredential().Password
}
elseif ($PSBoundParameters.ContainsKey('Encoding'))
{
if ($Encoding -eq $fileEncoding)
{
Write-Verbose -Message ($script:localizedData.StringReplaceTextMessage -f `
$Path, $Text)
}
else
{
Write-Verbose -Message ($script:localizedData.StringReplaceTextMessage -f `
$Path, $Text)
Write-Verbose -Message ($script:localizedData.StringReplaceTextMessage -f `
$Path, $Text)

# Add encoding parameter and value to the hashtable
$fileProperties.Add('Encoding', $Encoding)
}
# Add encoding parameter and value to the hashtable
$fileProperties.Add('Encoding', $Encoding)
}

if ($null -eq $fileContent)
Expand All @@ -194,7 +186,7 @@ function Set-TargetResource

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

Set-Content @fileProperties
Set-TextContent @fileProperties
}

<#
Expand Down Expand Up @@ -259,7 +251,7 @@ function Test-TargetResource
$AllowAppend = $false,

[Parameter()]
[ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)
Expand All @@ -272,11 +264,11 @@ function Test-TargetResource
return $false
}

$fileContent = Get-Content -Path $Path -Raw
$fileEncoding = Get-FileEncoding $Path
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
$fileContent = Get-FileContent -Path $Path -Encoding $fileEncoding

Write-Verbose -Message ($script:localizedData.SearchForTextMessage -f `
$Path, $Search)
$Path, $Search)

# Search the file content for any matches
$results = [regex]::Matches($fileContent, $Search)
Expand All @@ -293,19 +285,19 @@ function Test-TargetResource
}
if ($PSBoundParameters.ContainsKey('Encoding'))
{
if ($Encoding -eq $fileEncoding)
if (Test-FileEncodingEqual -FileEncoding $fileEncoding -ExpectedEncoding $Encoding)
{
# No matches found and encoding is in desired state
Write-Verbose -Message ($script:localizedData.StringNotFoundMessage -f `
$Path, $Search)
$Path, $Search)

return $true
}
else
{
# No matches found but encoding is not in desired state
Write-Verbose -Message ($script:localizedData.FileEncodingNotInDesiredState -f `
$fileEncoding, $Encoding)
$fileEncoding, $Encoding)

return $false
}
Expand All @@ -331,12 +323,12 @@ function Test-TargetResource
if ($desiredConfigurationMatch)
{
Write-Verbose -Message ($script:localizedData.StringNoReplacementMessage -f `
$Path, $Search)
$Path, $Search)
}
else
{
Write-Verbose -Message ($script:localizedData.StringReplacementRequiredMessage -f `
$Path, $Search)
$Path, $Search)
} # if

return $desiredConfigurationMatch
Expand Down Expand Up @@ -401,7 +393,7 @@ function Assert-ParametersValid
$AllowAppend = $false,

[Parameter()]
[ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ class DSC_ReplaceText : OMI_BaseResource
[Write, Description("The text to replace the text identified by the RegEx. Only used when Type is set to 'Text'.")] String Text;
[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;
[Write, Description("Specifies to append text to the file being modified. Adds the ability to add a configuration entry.")] Boolean AllowAppend;
[Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"}] String Encoding;
[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;
};
Loading