Method: PSWindows::Exec#add_env_var

Defined in:
lib/beaker/host/pswindows/exec.rb

#add_env_var(key, val) ⇒ Object

Add the provided key/val to the current ssh environment

Examples:

host.add_env_var('PATH', '/usr/bin:PATH')

Parameters:

  • key (String)

    The key to add the value to

  • val (String)

    The value for the key



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/beaker/host/pswindows/exec.rb', line 118

def add_env_var key, val
  key = key.to_s.upcase
  #see if the key/value pair already exists
  cur_val = subbed_val = get_env_var(key, true)
  subbed_val = cur_val.gsub(/#{Regexp.escape(val.gsub(/'|"/, ''))}/, '')
  if cur_val.empty?
    exec(powershell("[Environment]::SetEnvironmentVariable('#{key}', '#{val}', 'Machine')"))
    self.close #refresh the state
  elsif subbed_val == cur_val #not present, add it
    exec(powershell("[Environment]::SetEnvironmentVariable('#{key}', '#{val};#{cur_val}', 'Machine')"))
    self.close #refresh the state
  end
end