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

#copy_from_vm(src, dest, quiet = false) ⇒ Object

Copy a file to the virtual machine from the ievms home folder.



34
35
36
37
# File 'lib/ievms/windows_guest.rb', line 34

def copy_from_vm(src ,dest, quiet=false)
  print "Copying #{src} to #{dest}\n" unless quiet
  guestcontrol_exec "cmd.exe", "cmd.exe /c copy \"#{src}\" \"E:\\#{dest}\""
end

#copy_to_vm(src, dest, quiet = false) ⇒ Object

Copy a file to the virtual machine from the ievms home folder.



28
29
30
31
# File 'lib/ievms/windows_guest.rb', line 28

def copy_to_vm(src ,dest, quiet=false)
  print "Copying #{src} to #{dest}\n" unless quiet
  guestcontrol_exec "cmd.exe", "cmd.exe /c copy \"E:\\#{src}\" \"#{dest}\""
end

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



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

def download_file_from_guest(guest_path, local_path, quiet=false)

  # 1 run cp command in machine
  copy_from_vm guest_path, File.basename(local_path), quiet

  # 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



64
65
66
67
68
69
# File 'lib/ievms/windows_guest.rb', line 64

def download_string_from_file_to_guest( guest_path, quiet=false)
  copy_from_vm guest_path, 'tmpfile.txt', quiet
  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



101
102
103
104
105
# File 'lib/ievms/windows_guest.rb', line 101

def run_command(command, quiet=false)
  print "Executing command: #{command}\n" unless 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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ievms/windows_guest.rb', line 84

def run_command_as_admin(command,quiet=false)
  print "Executing command as administrator: #{command}\n" unless 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"

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

#schtasks_query_ievmsObject



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

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



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

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
  copy_to_vm File.basename(local_path), guest_path, quiet

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

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



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ievms/windows_guest.rb', line 71

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