Skip to content

ReplaceText will return TRUE when the string to replace doesn't exist but should #20

@jcwalker

Description

@jcwalker

Details of the scenario you tried and the problem that is occurring

When attempting to update a configuration file with an entry and that entry doesn't exist yet
Test-TargetResource returns TRUE. The resource is written in a way that it can only update an entry
in a file if it exists. I think it would be more useful if the resource could add entries to a configuration
file as well.

Verbose logs showing the problem

VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = SendConfigurationApply,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' = root/Microsoft/Windows/Desi
redStateConfiguration'.
VERBOSE: An LCM method call arrived from computer 2016-BUILD-1 with user sid S-1-5-21-828028386-903968607-3399198716-500.
VERBOSE: [2016-BUILD-1]: LCM: [ Start Set ]
VERBOSE: [2016-BUILD-1]: [DSCEngine] Importing the module C:\Program Files\WindowsPowerShell\Modules\FileContentDsc\1.0.0.38\DscResources\DSR_ReplaceText\DSR_ReplaceText.psm1 in
force mode.
VERBOSE: [2016-BUILD-1]: LCM: [ Start Resource ] [[ReplaceText]SetTextWithRegex]
VERBOSE: [2016-BUILD-1]: LCM: [ Start Test ] [[ReplaceText]SetTextWithRegex]
VERBOSE: [2016-BUILD-1]: [[ReplaceText]SetTextWithRegex] Importing the module DSR_ReplaceText in force mode.
VERBOSE: [2016-BUILD-1]: [[ReplaceText]SetTextWithRegex] Searching using RegEx 'lockPref("browser.download.dir", ".");' in file 'c:\scratch\firefox.cfg'.
VERBOSE: [2016-BUILD-1]: [[ReplaceText]SetTextWithRegex] String not found using RegEx 'lockPref("browser.download.dir", ".
");' in file 'c:\scratch\firefox.cfg', change not req
uired.
VERBOSE: [2016-BUILD-1]: LCM: [ End Test ] [[ReplaceText]SetTextWithRegex] in 0.0370 seconds.
VERBOSE: [2016-BUILD-1]: LCM: [ Skip Set ] [[ReplaceText]SetTextWithRegex]
VERBOSE: [2016-BUILD-1]: LCM: [ End Resource ] [[ReplaceText]SetTextWithRegex]
VERBOSE: [2016-BUILD-1]: LCM: [ End Set ]
VERBOSE: [2016-BUILD-1]: LCM: [ End Set ] in 0.6480 seconds.
VERBOSE: Operation 'Invoke CimMethod' complete.
VERBOSE: Time taken for configuration job to complete is 0.741 seconds

Suggested solution to the issue

If Test-TargetResource doesn't find any matches it should return FALSE so it is possible to add additional
entries to a configuration file.

The DSC configuration that is used to reproduce the issue (as detailed as possible)

Configuration Example
{
    Import-DSCResource -ModuleName FileContentDsc

    Node localhost
    {
        ReplaceText SetTextWithRegex
        {
            Path   = 'c:\scratch\firefox.cfg'
            Search = 'lockPref\("browser.download.dir", ".*"\);'
            Type   = 'Text'
            Text   = 'lockPref("browser.download.dir", "N:");'
        }
    }
}

The operating system the target node is running

Version and build of PowerShell the target node is running

PSVersion 5.1.14393.2155

Version of the DSC module that was used ('dev' if using current dev branch)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThe issue is a bug.help wantedThe issue is up for grabs for anyone in the community.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions