Skip to content
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
- Refactor Test-TargetResource to return $false in all DSC resource - Fixes
[Issue #12](https://github.com/PlagueHO/FileContentDsc/issues/13).
- Correct configuration names in Examples - fixes [Issue #15](https://github.com/PowerShell/FileContentDsc/issues/15).
- Refactor Test/Set-TargetResource in ReplaceText to be able to add a key if it
doesn't exist but should -Fixes
[Issue#20](https://github.com/PlagueHO/FileContentDsc/issues/20).

## 1.0.0.0

Expand Down
85 changes: 82 additions & 3 deletions DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ function Get-TargetResource
.PARAMETER Secret
The secret text to replace the text identified by the RegEx.
Only used when Type is set to 'Secret'.

.PARAMETER AllowAppend
Specifies to append text to the file being modified. Adds the ability to add a configuration entry.
#>
function Set-TargetResource
{
Expand Down Expand Up @@ -133,7 +136,11 @@ function Set-TargetResource
[Parameter()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Secret
$Secret,

[Parameter()]
[System.Boolean]
$AllowAppend = $false
)

Assert-ParametersValid @PSBoundParameters
Expand All @@ -155,10 +162,17 @@ function Set-TargetResource

if ($null -eq $fileContent)
{
# Configuration file does not exist
$fileContent = $Text
}
elseif ([regex]::Matches($fileContent, $Search).Count -eq 0 -and $AllowAppend -eq $true)
{
# Configuration file exists but Text does not exist so lets add it
$fileContent = Add-ConfigurationEntry -FileContent $fileContent -Text $Text
}
else
{
# Configuration file exists but Text not in a desired state so lets update it
$fileContent = $fileContent -Replace $Search, $Text
}

Expand Down Expand Up @@ -189,6 +203,9 @@ function Set-TargetResource
.PARAMETER Secret
The secret text to replace the text identified by the RegEx.
Only used when Type is set to 'Secret'.

.PARAMETER AllowAppend
Specifies to append text to the file being modified. Adds the ability to add a configuration entry.
#>
function Test-TargetResource
{
Expand Down Expand Up @@ -218,7 +235,11 @@ function Test-TargetResource
[Parameter()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Secret
$Secret,

[Parameter()]
[System.Boolean]
$AllowAppend = $false
)

Assert-ParametersValid @PSBoundParameters
Expand All @@ -239,6 +260,15 @@ function Test-TargetResource

if ($results.Count -eq 0)
{
if ($AllowAppend -eq $true)
{
# No matches found - but we want to append
Write-Verbose -Message ($localizedData.StringNotFoundMessageAppend -f `
$Path, $Search)

return $false
}

# No matches found - already in state
Write-Verbose -Message ($localizedData.StringNotFoundMessage -f `
$Path, $Search)
Expand Down Expand Up @@ -325,7 +355,11 @@ function Assert-ParametersValid
[Parameter()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Secret
$Secret,

[Parameter()]
[System.Boolean]
$AllowAppend = $false
)

# Does the file's parent path exist?
Expand All @@ -338,4 +372,49 @@ function Assert-ParametersValid
} # if
}

<#
.SYNOPSIS
Uses the stringBuilder class to append a configuration entry to the existing file content.

.PARAMETER FileContent
The existing file content of the configuration file.

.PARAMETER Text
The text to append to the end of the FileContent.
#>
function Add-ConfigurationEntry
{
[OutputType([String])]
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[String]
$FileContent,

[Parameter(Mandatory = $true)]
[String]
$Text
)

if ($FileContent -match '\n$' -and $FileContent -notmatch '\r\n$')
{
# default *nix line ending
$detectedNewLineFormat = "`n"
}
else
{
# default Windows line ending
$detectedNewLineFormat = "`r`n"
}

$stringBuilder = New-Object -TypeName System.Text.StringBuilder

$null = $stringBuilder.Append($FileContent)
$null = $stringBuilder.Append($Text)
$null = $stringBuilder.Append($detectedNewLineFormat)

return $stringBuilder.ToString()
}

Export-ModuleMember -Function *-TargetResource
3 changes: 2 additions & 1 deletion DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ class DSR_ReplaceText : OMI_BaseResource
[Key, Description("The RegEx string to use to search the text file.")] String Search;
[Write, Description("Specifies the value type to use as the replacement string. Defaults to 'Text'."),ValueMap{"Text", "Secret"},Values{"Text", "Secret"}] String Type;
[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("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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

ConvertFrom-StringData @'
SearchForTextMessage = Searching using RegEx '{1}' in file '{0}'.
StringNotFoundMessageAppend = String not found using RegEx '{1}' in file '{0}', change required.
StringNotFoundMessage = String not found using RegEx '{1}' in file '{0}', change not required.
StringMatchFoundMessage = String(s) '{2}' found using RegEx '{1}' in file '{0}'.
StringReplacementRequiredMessage = String found using RegEx '{1}' in file '{0}', replacement required.
Expand Down
25 changes: 25 additions & 0 deletions TestFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Setting1=Value1
Setting.Two='Value2'
Setting.Two='Value3'
Setting.Two='TestText'
Setting3.Test=Value4

Setting.NotExist='TestText'Setting1=Value1
Setting.Two='Value2'
Setting.Two='Value3'
Setting.Two='TestText'
Setting3.Test=Value4

Setting.NotExist='TestText'Setting1=Value1
Setting.Two='Value2'
Setting.Two='Value3'
Setting.Two='TestText'
Setting3.Test=Value4

Setting.NotExist='TestText'Setting1=Value1
Setting.Two='Value2'
Setting.Two='Value3'
Setting.Two='TestText'
Setting3.Test=Value4

Setting.NotExist='TestText'
Loading