Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyValuePairFile: Key Value Pair not removed when 'Ensure' marked as 'Absent' #69

Open
aydeisen opened this issue Dec 12, 2022 · 0 comments

Comments

@aydeisen
Copy link

Problem description

When attempting to remove a Key/Value pair from a file, the Key/Value pair is not removed

Verbose logs

Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = 
PerformRequiredConfigurationChecks,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' = 
root/Microsoft/Windows/DesiredStateConfiguration'.
An LCM method call arrived from computer SERVER1 with user sid {SID}.
[SERVER1]:                            [] Executing Get-Action with configuration (null)'s checksum: 
5650263B30602B97769572C32951366FD4A3A2DA0AE1886205F3F1E73E727F3E.
[SERVER1]:                            [] Executing Get-Action with configuration 's checksum returned result 
status: GetConfiguration.
[SERVER1]:                            [] Checksum is different. LCM will execute GetConfiguration to pull 
configuration .
[SERVER1]:                            [] Executing GetConfiguration succeeded. Configuration  was pulled from 
server.
[SERVER1]:                            [] Applying the new configuration(s) pulled.
[SERVER1]: LCM:  [ Start  Resource ]  [[MsiPackage]InstallZabbix609]
[SERVER1]: LCM:  [ Start  Test     ]  [[MsiPackage]InstallZabbix609]
[SERVER1]:                            [[MsiPackage]InstallZabbix609] Parsing 
{020E0A73-C192-4D8C-AF6A-7986F7D26F69} as an identifyingNumber
[SERVER1]:                            [[MsiPackage]InstallZabbix609] Parsed 
{020E0A73-C192-4D8C-AF6A-7986F7D26F69} as {020E0A73-C192-4D8C-AF6A-7986F7D26F69}
[SERVER1]:                            [[MsiPackage]InstallZabbix609] The package Zabbix Agent 2 (64-bit) is 
installed
[SERVER1]: LCM:  [ End    Test     ]  [[MsiPackage]InstallZabbix609]  in 0.0470 seconds.
[SERVER1]: LCM:  [ Skip   Set      ]  [[MsiPackage]InstallZabbix609]
[SERVER1]: LCM:  [ End    Resource ]  [[MsiPackage]InstallZabbix609]
[SERVER1]: LCM:  [ Start  Resource ]  [[KeyValuePairFile]ZabbixHostnameRemoved]
[SERVER1]: LCM:  [ Start  Test     ]  [[KeyValuePairFile]ZabbixHostnameRemoved]
[SERVER1]:                            [[KeyValuePairFile]ZabbixHostnameRemoved] Searching for key 'Hostname' in 
file 'C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf'.
[SERVER1]:                            [[KeyValuePairFile]ZabbixHostnameRemoved] Key 'Hostname' not found in file 
'C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf' and should not exist. Change not required.
[SERVER1]:                            [[KeyValuePairFile]ZabbixHostnameRemoved] File encoding is set to 'UTF8' 
but should be set to 'ASCII', Change required.
[SERVER1]: LCM:  [ End    Test     ]  [[KeyValuePairFile]ZabbixHostnameRemoved]  in 0.0940 seconds.
[SERVER1]: LCM:  [ Start  Set      ]  [[KeyValuePairFile]ZabbixHostnameRemoved]
[SERVER1]:                            [[KeyValuePairFile]ZabbixHostnameRemoved] Searching for key 'Hostname' in 
file 'C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf'.
[SERVER1]:                            [[KeyValuePairFile]ZabbixHostnameRemoved] File encoding is set to 'UTF8' 
but should be set to 'ASCII', Change required.
[SERVER1]: LCM:  [ End    Set      ]  [[KeyValuePairFile]ZabbixHostnameRemoved]  in 0.3860 seconds.
[SERVER1]: LCM:  [ End    Resource ]  [[KeyValuePairFile]ZabbixHostnameRemoved]
[SERVER1]: LCM:  [ Start  Resource ]  [[KeyValuePairFile]ZabbixHostnameItem]
[SERVER1]: LCM:  [ Start  Test     ]  [[KeyValuePairFile]ZabbixHostnameItem]
[SERVER1]:                            [[KeyValuePairFile]ZabbixHostnameItem] Searching for key 'HostnameItem' in 
file 'C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf'.
[SERVER1]:                            [[KeyValuePairFile]ZabbixHostnameItem] Key 'HostnameItem' found in file 
'C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf' and should exist and value(s) are correct. Change not required.
[SERVER1]: LCM:  [ End    Test     ]  [[KeyValuePairFile]ZabbixHostnameItem]  in 0.0310 seconds.
[SERVER1]: LCM:  [ Skip   Set      ]  [[KeyValuePairFile]ZabbixHostnameItem]
[SERVER1]: LCM:  [ End    Resource ]  [[KeyValuePairFile]ZabbixHostnameItem]
[SERVER1]: LCM:  [ Start  Resource ]  [[Service]ZabbixAgentIsRunning]
[SERVER1]: LCM:  [ Start  Test     ]  [[Service]ZabbixAgentIsRunning]
[SERVER1]:                            [[Service]ZabbixAgentIsRunning] Service Zabbix Agent 2 exists.
[SERVER1]:                            [[Service]ZabbixAgentIsRunning] Perform operation 'Query CimInstances' 
with following parameters, ''queryExpression' = SELECT * FROM Win32_Service WHERE Name='Zabbix Agent 2','queryDialect' 
= WQL,'namespaceName' = root\cimv2'.
[SERVER1]:                            [[Service]ZabbixAgentIsRunning] Operation 'Query CimInstances' complete.
[SERVER1]:                            [[Service]ZabbixAgentIsRunning] Service Zabbix Agent 2 exists.
[SERVER1]: LCM:  [ End    Test     ]  [[Service]ZabbixAgentIsRunning]  in 0.4530 seconds.
[SERVER1]: LCM:  [ Skip   Set      ]  [[Service]ZabbixAgentIsRunning]
[SERVER1]: LCM:  [ End    Resource ]  [[Service]ZabbixAgentIsRunning]
Operation 'Invoke CimMethod' complete.
Time taken for configuration job to complete is 44.289 seconds

DSC configuration

$MyConfig = @{
    AllNodes = @(
        @{
            NodeName = 'localhost'
        }
        @{
            NodeName    = '*'
            ZabbixProxy = '{IP1},{IP2}'
        }
    )
}

Configuration ZabbixSiteServers {
    Import-DscResource -ModuleName @{ModuleName = 'PSDscResources'; RequiredVersion = '2.12.0.0' } -Name MsiPackage, Service
    Import-DscResource -ModuleName @{ModuleName = 'FileContentDsc'; RequiredVersion = '1.3.0.151' }

    Node $AllNodes.NodeName {
        KeyValuePairFile ZabbixHostnameRemoved
        {
            Path      = 'C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf'
            Name      = 'Hostname'
            Ensure    = 'Absent'
            Text      = [System.String]::Empty
            Encoding  = 'ASCII'
            DependsOn = '[MsiPackage]InstallZabbix609'
        }

        KeyValuePairFile ZabbixHostnameItem
        {
            Path      = 'C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf'
            Name      = 'HostnameItem'
            Ensure    = 'Present'
            Text      = 'system.hostname[netbios,lower]'
            Encoding  = 'ASCII'
            DependsOn = '[MsiPackage]InstallZabbix609'
        }

        MsiPackage InstallZabbix609
        {
            ProductId     = '{020E0A73-C192-4D8C-AF6A-7986F7D26F69}'
            Path          = 'https://cdn.zabbix.com/zabbix/binaries/stable/6.0/6.0.9/zabbix_agent2-6.0.9-windows-amd64-openssl.msi'
            Ensure        = 'Present'
            FileHash      = '15C47E560F8D070F69A0BBFD390280D1B80EAD5775FB202F3A09DADC5A67C91F'
            HashAlgorithm = 'Sha256'
            Arguments     = "ENABLEPATH=1 HOSTMETADATAITEM=system.uname SERVER=""$($Node.ZabbixProxy)"" SERVERACTIVE=""$($Node.ZabbixProxy)"""
        }

        Service ZabbixAgentIsRunning
        {
            Name        = 'Zabbix Agent 2'
            State       = 'Running'
            StartupType = 'Automatic'
            Ensure      = 'Present'
            DependsOn   = '[KeyValuePairFile]ZabbixHostnameItem', '[KeyValuePairFile]ZabbixHostnameRemoved'
        }
    }
}

Suggested solution

The Regex is searching for an EOL Character that doesn't exist.

On line 264 of DSC_KeyValuePairFile.psm1, the regex is requiring $eolChars to exist. This is in contrast to line 242, which is looking for 0 or more matches to $eolChars.

On line 264, change:

$fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)$eolChars", '', $regExOptions)

to:

$fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)($eolChars*)", '', $regExOptions)

Operating system the target node is running

OsName               : Microsoft Windows Server 2016 Standard
OsOperatingSystemSKU : StandardServerEdition
OsArchitecture       : 64-bit
WindowsBuildLabEx    : 14393.5501.amd64fre.rs1_release.221103-1703
OsLanguage           : en-US
OsMuiLanguages       : {en-US}

PowerShell version and build the target node is running

5.1.14393.5127

FileContentDsc version

Name           Version   Path
----           -------   ----
FileContentDsc 1.3.0.151 C:\Program Files\WindowsPowerShell\Modules\FileContentDsc\1.3.0.151\FileContentDsc.psd1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant