envvar

Synopsis

Returns the value of an environment variable.

Syntax

envvar(<variableName>)

Description

The envvar() function returns the value of an environment variable as a string. If the environment variable doesn't exist, DSC raises an error.

Examples

Example 1 - Reference DSC_CONFIG_ROOT in a configuration

When you use the --path option to specify a configuration document for any of the dsc config * commands, DSC automatically creates the DSC_CONFIG_ROOT environment variable and sets the value to the parent folder of the specified configuration document. For more information, see dsc config command reference.

This configuration echoes that folder with the Test/Echo resource.

# ./examples/envvar.example.1.dsc.config.yaml
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
resources:
  - name: Echo 'DSC_CONFIG_ROOT' in envvar
    type: Test/Echo
    properties:
      output: "[envvar('DSC_CONFIG_ROOT')]"
dsc config get --path ~/dsc/examples/envvar.example.1.dsc.config.yaml
results:
- name: Echo DSC_CONFIG_ROOT
  type: Test/Echo
  result:
    actualState:
      output: ~/dsc/examples
messages: []
hadErrors: false

Parameters

variableName

The envvar() function expects a single string representing the name of the environment variable to use. If the value isn't a string, DSC raises an error when validating the configuration document. If the environment variable named by the input doesn't exist, DSC raises an error.

Type:         string
Required:     true
MinimumCount: 1
MaximumCount: 1

Output

The envvar() function returns the value of the environment variable specified with the variableName parameter.

Type: string