Module: Serverspec::Backend::PowerShell::ScriptHelper

Included in:
Cmd, WinRM
Defined in:
lib/serverspec/backend/powershell/script_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_pre_command(cmd) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/serverspec/backend/powershell/script_helper.rb', line 20

def add_pre_command(cmd)
  path = Serverspec.configuration.path || RSpec.configuration.path
  if Serverspec.configuration.pre_command
    cmd.strip!
    cmd = 
<<-EOF
if (#{Serverspec.configuration.pre_command})
{
#{cmd}
}
EOF
    cmd = "$env:path = \"#{path};$env:path\"\n#{cmd}" if path
  end
  cmd
end

#build_command(cmd) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/serverspec/backend/powershell/script_helper.rb', line 7

def build_command(cmd)
  path = Serverspec.configuration.path || RSpec.configuration.path
  if path
    cmd.strip!
    cmd = 
<<-EOF
$env:path = "#{path};$env:path"
#{cmd}
EOF
  end
  cmd
end

#create_script(command) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/serverspec/backend/powershell/script_helper.rb', line 49

def create_script command
  script = build_command(command.script)
  script = add_pre_command(script)
  ps_functions = command.import_functions.map { |f| File.read(File.join(File.dirname(__FILE__), 'support', f)) }
<<-EOF
$exitCode = 1
try {
  #{ps_functions.join("\n")}
  $success = (#{script})
  if ($success -is [Boolean] -and $success) { $exitCode = 0 }
} catch {
  Write-Output $_.Exception.Message
}
Write-Output "Exiting with code: $exitCode"
exit $exitCode
  EOF
end

#encode_script(script) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/serverspec/backend/powershell/script_helper.rb', line 36

def encode_script script
  script_text = script.chars.to_a.join("\x00").chomp
  script_text << "\x00" unless script_text[-1].eql? "\x00"
  if script_text.respond_to?(:encode)
    script_text = script_text.encode('ASCII-8BIT')
  end
  if Base64.respond_to?(:strict_encode64)
    Base64.strict_encode64(script_text)
  else
    [ script_text ].pack("m").strip
  end
end