从 Selenium 执行的 AutoIT 脚本修改主机文件

Modify hosts file from AutoIT script executed from Selenium

我正在尝试根据我 运行 我的脚本所在的环境更改我的主机文件。看起来我需要做的是 运行 作为管理员的命令(提升特权),我似乎无法在 AutoIT 中找到如何做到这一点。

以下是来自 Selenium 的调用示例:

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

这是我在 AutoIT 中的代码:

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


Run(@comspec & " /c DEL %WINDIR%\SYSTEM32\DRIVERS\ETC\HOSTS")

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

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

我如何告诉 AutoIT 运行 作为管理员?

首先请注意,您的帐户应该具有更高的权限。

据我所知,您在这里有以下选择:

  1. 最想要也最安全的

    运行 您的 Java 应用程序(IntelliJ、Eclipse)作为管理员。这样,它产生或执行的任何进程都将 运行 具有提升的特权(这不仅在 Windows 中起到连锁反应的作用)。

  2. 运行 提升权限

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