Dustin N. Dustin N. - 17 days ago 4
Java Question

Modify hosts file from AutoIT script executed from Selenium

I am trying to change out my hosts file based on whichever environment I run my script in. It looks like what I'm needing to do is run the commands as admin (elevated privileges), I just cannot seem to find out how to do that in AutoIT.

Here is an example of the call from Selenium:

Runtime.getRuntime().exec("C:\HostSwitcher.exe " + C:\Scripts + " " + "QA2");

Here is my code in AutoIT:

$filePath = $CmdLine[1]
$env = $CmdLine[2]


if $env = "Dev" then
Run(@comspec & " /c COPY "&$filePath&"hostFiles\DevHost.txt %WINDIR%\SYSTEM32\DRIVERS\ETC\hosts")

if $env = "QA2" then
Run(@comspec & " /c COPY "&$filePath&"hostFiles\QA2Host.txt %WINDIR%\SYSTEM32\DRIVERS\ETC\hosts")

How would I tell AutoIT to run as admin?


As a starting note, your account should have elevated privileges.

As far as I know you have options here:

  1. the most desired and safe one

    Run your Java application (IntelliJ, Eclipse) as Administrator. This way, any processes spawn or executed by it would be ran with elevated privileges (this works as a chain reaction in Windows and not only).

  2. Run it with elevated privileges

    Runtime.getRuntime().exec("runas /profile /user:Administrator \"C:\HostSwitcher.exe " + C:\Scripts + " " + "QA2");