I need to be able to run some applications and commands as Administrator when running from a non-Admin command window.
I realize that this is probably one of the most requested use cases for AutoIt in general, and I have been able to get a few of my specific use cases working, but I have some specific needs that I haven't been able to solve yet. I'm starting to think I'm running into a limitation of Windows, but I don't know enough to really confirm that.
I'd really like to be able to start a non-Admin cmd window and run an application as Admin (using the RunAs function from an AutoIt app), and get both the console output and the exit code back. It seems like I can do one or the other, but I can't get both when running from a non-Admin console window. I've tried some UDFs, and made changes based on this process UDF (https://www.autoitscript.com/forum/files/file/356-process-udf/) to allow RunAs, but it doesn't seem to work in this case.
Other notes about my setup:
UAC is turned all the way down (off)
I don't have the ability to manually interact with the system at any point in the process - it's part of a manual reimage install loop for CI automated testing
The AutoItRunAs.exe will be called from a VBScript that's running under a non-Admin account
Windows 8+ only (mainly Windows 10)
Here's a simplified example of the AutoIt code that I have:
#AutoIt3Wrapper_Outfile_x64=AutoItRunAs.exe
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Change2CUI=y
#include <ProcessConstants.au3>
#include <AutoItConstants.au3>
#include <ProcessEx.au3>
Local $output = "", $err = "", $cmd=CmdLine[1], $user=CmdLine[2], $pass=CmdLine[3]
$PID = RunAs($user, "", $pass, 4, $cmd, "", @SW_HIDE, $STDERR_MERGED)
; Get the handle of the process
Local $hProcessHandle = _Process_GetHandle($PID)
While ProcessExists($PID)
; Capture the output
$output &= StdoutRead($PID)
; Don't kill the CPU
Sleep(100)
WEnd
; Capture any remaining output
$output &= StdoutRead($PID)
; Get the exit code from the process handle, so that this AutoIt executable can exit with the same exit code
$exitCode = _Process_GetExitCode($hProcessHandle)
; Write the output from running the command back out, so whoever is watching for the RunAs output can consume it
ConsoleWrite($output)
I'd like to be able to call my AutoIt application like this:
> AutoItRunAs.exe <cmd> <username> <password>
Examples:
(requires Admin permissions - I get the output, which meets my needs. I don't care about the exit code in this specific case)
> AutoItRunAs.exe "bcdedit" Administrator AdminPass
(requires Admin permissions - doesn't currently perform the copy)
> AutoItRunAs.exe "xcopy D:\myfile\test.txt C:\ /Y /A /H /E" Administrator AdminPass
(doesn't require Admin permissions - I still don't get the exit code)
> AutoItRunAs.exe "exit /b 7777" Administrator AdminPass
If I run the application from a non-Admin console window, the application either doesn't work (the xcopy example), or it doesn't provide the exit code (the exit /b example).
Any help is appreciated.
AutoItRunAs.au3