Skip to content

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

@RidaLeFou

Description

@RidaLeFou

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>

Metadata

Metadata

Assignees

Labels

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

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions