tldr; I need to run a command as admin and retrieve the output
I've already created a bat file that does the job but I want to translate it to pure ruby as a learning exercise. Using backticks I can get the output from the command, unfortunately the output tells me I lack the user privileges. Through this site I've found how to poke UAC in the right way, but I can't seem to get it to recognise the command I want which is (bcdedit /v)
I've read through https://learn.microsoft.com/en-us/windows/desktop/shell/shell-shellexecute & Detect if running with administrator privileges under Windows XP and numerous stack overflow posts with similar questions but haven't found clear guidelines to do this.
def running_in_admin_mode?
(`reg query HKU\\S-1-5-19 2>&1` =~ /ERROR/).nil?
end
unless running_in_admin_mode?
shell = WIN32OLE.new('Shell.Application')
shell.ShellExecute("ruby", File.expand_path(__FILE__), nil, 'runas')
exit
end
update: so this sort of works. It flashes up the output I want in an IRB shell, but opens infinite new shells which locks up the computer.
require 'win32ole'
shell = WIN32OLE.new('Shell.Application')
shell.ShellExecute("ruby", File.expand_path(__FILE__), nil, 'runas')
response = `bcdedit /v`
p response
exit
Update: giving up doing it for now. Instead just checking the responses. Code is not completely functional yet, but here is where I am incase someone wants to pitch in.
require 'win32/registry'
require 'win32ole'
p 'Device Guard v0.1'
p 'Checking for requisite permissions'
def disable
p 'Would you like me to disable Docker and enable VMWare? [Yes] or [No]'
case (gets.downcase.chomp)
when 'yes'
puts "Disabling docker"
`bcdedit /set hypervisorlaunchtype off` # seems to work ok
Win32::Registry::HKEY_CURRENT_USER.open('SOFTWARE\Microsoft\Windows\CurrentVersion\Run') do |reg|
value = reg['Docker for Windows'] # need to save the location of the executable for next time
reg.delete_value('Docker for Windows') # Access denied even when run as admin :()
end
when 'no'
recheck
else
puts 'Instructions unclear'
disable
end
end
def enable
p 'Would you like me to disable VMWare and enable Docker? [Yes] or [No]'
case (gets.downcase.chomp)
when 'yes'
Win32::Registry::HKEY_CURRENT_USER.open('SOFTWARE\Microsoft\Windows\CurrentVersion\Run') do |reg|
docker_location = reg['Docker for Windows']
p docker_location
`bcdedit /set hypervisorlaunchtype Auto`
end
when 'no'
recheck
else
puts "Instructions unclear"
enable
end
end
def recheck
p 'Well what do you want me to do then? [Recheck] or [Exit]'
case (gets.downcase.chomp)
when 'recheck'
startup
when 'exit'
puts "bye then"
exit
else
puts "Instructions unclear"
recheck
end
end
def check_hypervisor(response)
edited_response = response.match('hypervisorlaunchtype(.*)')[1]
if edited_response.end_with?(" Off")
p 'Hypervisor Launch Type is disabled'
p 'This means VMWare should run and Docker is disabled'
enable
elsif edited_response.end_with?(" Auto")
p 'Hypervisor Launch Type is enabled'
p 'This means that Docker should run and VMWare is disabled'
disable
end
end
def startup
response = `bcdedit /v`
unless response.include?("Windows Boot Manager")
no_access
end
unless response.include?("Access is denied")
check_hypervisor(response)
end
end
def no_access
p 'I am not running as admin. Please run me from a shell with admin privilages'
exit
end
startup