Skip to content

KeyValuePairFile

dscbot edited this page Jun 12, 2024 · 7 revisions

KeyValuePairFile

Parameters

Parameter Attribute DataType Description Allowed Values
Path Key String The path to the key value pair text file.
Name Key String The name of the key.
Ensure Write String Specifies the if the key value pair with the specified key should exist in the file. Present, Absent
Type Write String Specifies the value type to use as the replacement string. Defaults to 'Text'. Text, Secret
Text Write String The text to replace the value with in the identified key. Only used when Type is set to 'Text'.
Secret Write PSCredential The secret text to replace the value with in the identified key. Only used when Type is set to 'Secret'.
IgnoreNameCase Write Boolean Ignore the case of the name of the key. Defaults to $False.
IgnoreValueCase Write Boolean Ignore the case of any text or secret when determining if it they need to be updated. Defaults to $False.
Encoding Write String Specifies the file encoding. Defaults to ASCII ASCII, BigEndianUnicode, BigEndianUTF32, UTF8, UTF32

Description

The KeyValuePairFile resource is used to add, remove or set key/value pairs in a text file containing key/value pair entries.

This resource is intended to be used to set key/value pair values in configuration or data files where no partitions or headings are used to separate entries and each line contains only a single entry.

This resource should not be used to configure INI files. The IniSettingFile resource should be used instead.

Examples

Example 1

Remove all Core.Logging keys in the file c:\myapp\myapp.conf.

Configuration KeyValuePairFile_RemovePlainTextPair_Config
{
    Import-DSCResource -ModuleName FileContentDsc

    Node localhost
    {
        KeyValuePairFile RemoveCoreLogging
        {
            Path   = 'c:\myapp\myapp.conf'
            Name   = 'Core.Logging'
            Ensure = 'Absent'
        }
    }
}

Example 2

Set all Core.Logging keys to Information or add it if it is missing in the file c:\myapp\myapp.conf.

Configuration KeyValuePairFile_SetPlainTextPair_Config
{
    Import-DSCResource -ModuleName FileContentDsc

    Node localhost
    {
        KeyValuePairFile SetCoreLogging
        {
            Path   = 'c:\myapp\myapp.conf'
            Name   = 'Core.Logging'
            Ensure = 'Present'
            Text   = 'Information'
        }
    }
}

Example 3

Set all Core.Password keys to the password provided in the $Secret credential object or add it if it is missing in the file c:\myapp\myapp.conf.

Configuration KeyValuePairFile_SetSecretTextPair_Config
{
    param
    (
        [Parameter()]
        [ValidateNotNullorEmpty()]
        [PSCredential]
        $Secret
    )

    Import-DSCResource -ModuleName FileContentDsc

    Node localhost
    {
        KeyValuePairFile SetCorePassword
        {
            Path   = 'c:\myapp\myapp.conf'
            Name   = 'Core.Password'
            Ensure = 'Present'
            Type   = 'Secret'
            Secret = $Secret
        }
    }
}