Defined Type: windows::environment

Defined in:
manifests/environment.pp

Overview

Define: windows::environment

Set Windows environment variable to the given value.

Parameters

value

The value that the environment variable should be, required by default (when ensure = ‘present’).

ensure

The ensure value for the resource, must be ‘present’ or ‘absent’. Defaults to ‘present’. When set to ‘absent’, the environment variable is removed.

variable

The environment variable to set, defaults to the name of the resource.

target

The location where an environment variable is stored, must be either ‘Machine’ (the default) or ‘User’.

Parameters:

  • value (Any) (defaults to: undef)
  • ensure (Any) (defaults to: 'present')
  • variable (Any) (defaults to: $name)
  • target (Any) (defaults to: 'Machine')


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'manifests/environment.pp', line 23

define windows::environment(
  $value    = undef,
  $ensure   = 'present',
  $variable = $name,
  $target   = 'Machine',
) {

  # Ensure only valid target parameter.
  validate_re($target, '^(Machine|User)$', 'Invalid target parameter')

  case $ensure {
    'present': {
      if (! $value) {
        fail("Must provide a value to set ${variable} with.\n")
      }
      $command = "[Environment]::SetEnvironmentVariable(\"${variable}\", \"${value}\", \"${target}\")"
      $unless = "if ([Environment]::GetEnvironmentVariable(\"${variable}\", \"${target}\") -ne \"${value}\"){ exit 1 }"
    }
    'absent': {
      $command = "[Environment]::SetEnvironmentVariable(\"${variable}\", \$null, \"${target}\")"
      $unless = "if ([Environment]::GetEnvironmentVariable(\"${variable}\", \"${target}\") -ne \$null){ exit 1 }"
    }
    default: {
      fail("Invalid windows::environment ensure value.\n")
    }
  }

  # Execute the command that sets or unsets the environment variable, and
  # refresh the environment.
  include windows::refresh_environment
  exec { "env-${name}-${target}":
    command  => $command,
    unless   => $unless,
    provider => 'powershell',
    notify   => Class['windows::refresh_environment'],
  }
}