Class: Ievms::WindowsGuest

Inherits:
Object
  • Object
show all
Defined in:
lib/ievms/windows_guest.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vbox_name) ⇒ WindowsGuest

Returns a new instance of WindowsGuest.



21
22
23
24
25
# File 'lib/ievms/windows_guest.rb', line 21

def initialize(vbox_name)
  @vbox_name = vbox_name

  is_vm vbox_name
end

Instance Attribute Details

#verboseObject

Returns the value of attribute verbose.



17
18
19
# File 'lib/ievms/windows_guest.rb', line 17

def verbose
  @verbose
end

Instance Method Details

#download_file_from_guest(guest_path, local_path, quiet = false) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ievms/windows_guest.rb', line 27

def download_file_from_guest(guest_path, local_path, quiet=false)

  log_stdout "Copying #{guest_path} to #{local_path}", quiet
  # 1 run cp command in machine
  guestcontrol_exec "cmd.exe", "cmd.exe /c copy \"#{guest_path}\" \"E:\\#{File.basename(local_path)}\""

  # 2 copy to tmp location in .ievms
  FileUtils.cp File.join(IEVMS_HOME,File.basename(local_path)), local_path

  # 3 remove tmp file in .ievms
  FileUtils.rm File.join(IEVMS_HOME,File.basename(local_path))
end

#download_string_from_file_to_guest(guest_path, quiet = false) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/ievms/windows_guest.rb', line 54

def download_string_from_file_to_guest( guest_path, quiet=false)
  log_stdout "Copying #{guest_path} to tempfile.txt", quiet
  guestcontrol_exec "cmd.exe", "cmd.exe /c copy \"#{guest_path}\" \"E:\\tmpfile.txt\""

  string = IO.read(File.join(IEVMS_HOME,'tmpfile.txt'))
  FileUtils.rm File.join(IEVMS_HOME,'tmpfile.txt')
  string
end

#run_command(command, quiet = false) ⇒ Object

execute existing batch file in Windows guest



107
108
109
110
111
# File 'lib/ievms/windows_guest.rb', line 107

def run_command(command, quiet=false)
  log_stdout "Executing command: #{command}", quiet
  out, _, _ = guestcontrol_exec "cmd.exe", "cmd.exe /c \"#{command}\""
  out
end

#run_command_as_admin(command, quiet = false) ⇒ Object

execute existibg batch file in Windows guest as Administrator



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ievms/windows_guest.rb', line 86

def run_command_as_admin(command,quiet=false)
  log_stdout "Executing command as administrator: #{command}", quiet

  run_command 'if exist C:\Users\IEUser\ievms.bat del C:\Users\IEUser\ievms.bat && Exit', true

  upload_string_as_file_to_guest(command, 'C:\Users\IEUser\ievms.bat', true)

  guestcontrol_exec "schtasks.exe", "schtasks.exe /run /tn ievms"

  unless quiet
    print "..."
    while schtasks_query_ievms.include? 'Running'
      print "."
      sleep 2
    end
    print "\n"
  end

end

#schtasks_query_ievmsObject



113
114
115
116
# File 'lib/ievms/windows_guest.rb', line 113

def schtasks_query_ievms
  out, _, _ = guestcontrol_exec "schtasks.exe", "schtasks.exe /query /tn ievms"
  out
end

#upload_file_to_guest(local_path, guest_path, quiet = false) ⇒ Object

Upload a local file to the windows guest



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ievms/windows_guest.rb', line 41

def upload_file_to_guest(local_path, guest_path, quiet=false)

  # 1 copy to tmp location in .ievms
  FileUtils.cp local_path, File.join(IEVMS_HOME,File.basename(local_path))

  # 2 run cp command in machine
  log_stdout "Copying #{local_path} to #{guest_path}", quiet
  guestcontrol_exec "cmd.exe", "cmd.exe /c copy \"E:\\#{File.basename(local_path)}\" \"#{guest_path}\""

  # 3 remove tmp file in .ievms
  FileUtils.rm File.join(IEVMS_HOME,File.basename(local_path))
end

#upload_file_to_guest_as_admin(local_path, guest_path, quiet = false) ⇒ Object

Upload a local file to the windows guest as Administator



76
77
78
79
80
81
82
83
# File 'lib/ievms/windows_guest.rb', line 76

def upload_file_to_guest_as_admin(local_path, guest_path, quiet=false)

  log_stdout "Copying #{local_path} to #{guest_path} as Administrator", quiet

  upload_file_to_guest(local_path, 'C:\Users\IEUser\.tempadminfile',true)
  run_command_as_admin('copy C:\Users\IEUser\.tempadminfile '+ guest_path,true)
  run_command 'del C:\Users\IEUser\.tempadminfile', true
end

#upload_string_as_file_to_guest(string, guest_path, quiet = false) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ievms/windows_guest.rb', line 63

def upload_string_as_file_to_guest(string, guest_path, quiet=false)

  tmp = Tempfile.new('txtfile')
  tmp.write "#{string}\n"
  path = tmp.path
  tmp.rewind
  tmp.close

  upload_file_to_guest(path, guest_path, true)
  FileUtils.rm path
end