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

Unable to use Powershell code containing the $ sign as value for the Text key of the KeyValuePairFile resource #68

Open
RidaLeFou opened this issue Sep 19, 2022 · 1 comment
Assignees
Labels
bug The issue is a bug. help wanted The issue is up for grabs for anyone in the community.

Comments

@RidaLeFou
Copy link

RidaLeFou commented Sep 19, 2022

Problem description

Hello,

I want to modify a .conf file using the KeyValuePairFile resource.
The key UserParameter in the .conf file should be the following powershell command:

Get-ChildItem -Recurse CERT:LocalMachine/My | ? { $_.NotAfter -lt (Get-Date).AddMonths(2) } | % { $s=$_.Subject -replace "CN=",""; $e=$_.NotAfter.ToString(); $l=$s+"|"+$e; write-output $l }

However, the resource fails to handle the $ sign, and the result is not as desired.

Expected result:
UserParameter=Get-ChildItem -Recurse CERT:LocalMachine/My | ? { $_.NotAfter -lt (Get-Date).AddMonths(2) } | % { $s=$_.Subject -replace "CN=",""; $e=$_.NotAfter.ToString(); $l=$s+"|"+$e; write-output $l }

Current result with the resource:
UserParameter=Get-ChildItem -Recurse CERT:LocalMachine/My | ? { # This is a configuration file for Zabbix agent 2 (Windows)...

I have the impression that as soon as the resource meets the $ sign, it replaces it with the content of the file.

Thank you.

Verbose logs

VERBOSE: [SRV]: LCM:  [ Start  Test     ]  [[KeyValuePairFile]ZabbixConf]
VERBOSE: [SRV]:                            [[KeyValuePairFile]ZabbixConf] Searching for key 'UserParameter' in file 'C:\Temp\zab.conf'.
VERBOSE: [SRV]: LCM:  [ End    Test     ]  [[KeyValuePairFile]ZabbixConf]  in 0.0490 seconds.
VERBOSE: [SRV]: LCM:  [ Start  Set      ]  [[KeyValuePairFile]ZabbixConf]
VERBOSE: [SRV]:                            [[KeyValuePairFile]ZabbixConf] Searching for key 'UserParameter' in file 'C:\Temp\zab.conf'.
VERBOSE: [SRV]:                            [[KeyValuePairFile]ZabbixConf] Key 'UserParameter' found in file 'C:\Temp\zab.conf' and has been updated.
VERBOSE: [SRV]: LCM:  [ End    Set      ]  [[KeyValuePairFile]ZabbixConf]  in 0.0780 seconds.
VERBOSE: [SRV]: LCM:  [ End    Resource ]  [[KeyValuePairFile]ZabbixConf]

DSC configuration

Configuration ZabbixConf{

    Import-DscResource -ModuleName PSDesiredStateConfiguration
    Import-DscResource -ModuleName FileContentDsc -moduleversion 2.0.0
   
    Node SRV {
        KeyValuePairFile ZabbAgent {
            Path = 'C:\Temp\zab.conf'
            Name = 'UserParameter'
            Ensure = 'Present'            
            Text = 'Get-ChildItem -Recurse CERT:LocalMachine/My | ? { $_.NotAfter -lt (Get-Date).AddMonths(2) } | % { $s=$_.Subject -replace "CN=",""; $e=$_.NotAfter.ToString(); $l=$s+"|"+$e; write-output $l }'
        }
    }
}

Suggested solution

No :(

Operating system the target node is running

OsName               : Microsoft Windows 10 Enterprise        
OsOperatingSystemSKU : EnterpriseEdition                      
OsArchitecture       : 64-bit                                 
WindowsVersion       : 2009                                   
WindowsBuildLabEx    : 19041.1.amd64fre.vb_release.191206-1406
OsLanguage           : en-US                                  
OsMuiLanguages       : {en-US}

PowerShell version and build the target node is running

Name                           Value
----                           -----
PSVersion                      5.1.19041.1320
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.1320
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

FileContentDsc version

Name           Version   Path
----           -------   ----
FileContentDsc 2.0.0     <C:\Program Files\WindowsPowerShell\Modules\FileContentDsc\2.0.0\FileContentDsc.psd1>
@PlagueHO PlagueHO self-assigned this Sep 19, 2022
@PlagueHO PlagueHO added bug The issue is a bug. help wanted The issue is up for grabs for anyone in the community. labels Sep 19, 2022
@PlagueHO
Copy link
Member

Thanks for logging this @RidaLeFou. I haven't looked at this in detail, but it does look like the issue is in this line: https://github.com/dsccommunity/FileContentDsc/blob/main/source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.psm1#L242 - and it should be fixable - although I haven't done any detailed examination of this. I suspect it's a behavior of the [regex]::Replace() method, but will need to do some digging. I'm super short on time at the moment, so if anyone else gets to it first. PRs welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The issue is a bug. help wanted The issue is up for grabs for anyone in the community.
Projects
None yet
Development

No branches or pull requests

2 participants