WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

UAC

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

SendKey and UAC

 Keywords: SendKey SendKeysTo Do Not Send Keystroke Key Ignored Fail UAC Elevation Elevated Integrity Process Application EXE 

Question:

I am running on a system with UAC on and I am logged in as adminstrator. I am using WinBatch to try to send keystrokes to a command shell process that is running elevated. However when I compile my script using the manifest settings AsInvoker the SendKey/SendKeysTo functions seem to be ignored.

Answer:

Yes this is expected behavior for this specific scenario. This is because UAC does not allow processes to interact that are running with different intergrity levels.

The problem is you have is that you are logged in as an administrator. With User Account Control (UAC) fully enabled, interactive administrators normally run with least user privileges, but they can self-elevate to perform administrative tasks by giving explicit consent. In their least-privileged state, administrators are referred to as Protected administrators. In their elevated state, they are referred to as Elevated administrators.

Since you manifested your exe using AsInvoker the script is running in the least-privileged state and will not be able to intereact ( send keystrokes ) to the elevated process.

In order to get the script to work you will need to properly manifest your exe using either highest avialable ( if logged in as an admin ) or RequireAdministrator ( if logged in as a standard user ).

Since you are logged in as an admin and UAC is on...the following applies:

;WORKS: Logged in as Admin, Script with default .WBT extention (HighestAvilable)
;WORKS:  Logged in as Admin, Script with .WBT_AF extention (RequireAdministrator)
;EXPECTED FAIL:  Logged in as Admin, Script with .WBT_IF extention (AsInvoker)
uaclevel = "Error or unsupported platform.;Process is default (UAC is disabled or standard user).;Process is running elevated.;Process is not running elevated (user is in Administrators group)." 
Pause('UacElevationLevel of Script',ItemExtract(UacElevationLevel()+1,uaclevel,";"))

;Launch an Elevated Process
ShellExecute("cmd.exe", "", "", @NORMAL, "RunAs")

parentwindowname = "Admin"
ret = WinWaitExist( parentwindowname, 5 )
if ret == 0
  Pause("Notice","Unable to access specified window")
  Exit
endif

;Attempt to sendkeys
SendKeysTo( parentwindowname, "Hello World." )
Reference: http://msdn.microsoft.com/en-us/library/windows/desktop/aa511445.aspx

Note: On Windows Vista and 7, if you turn off UAC, the SendKeys will be sent regardless of the process elevation and /or manifest settings of the script. However on Windows 8 and newer, even with UAC prompting turned off all processes run with medium integrity level even for administrator accounts. This is unlike Windows 7 where almost all processes run with high integrity level for administrators with UAC prompting turned off.


Article ID:   W18321
Filename:   SendKey and UAC.txt
File Created: 2013:11:15:16:00:24
Last Updated: 2013:11:15:16:00:24