ImpersonateUser
System function.
The ImpersonateUser function allows WinTask to temporarily assume the rights of another user. This allows WinTask to execute administrator tasks, for example. Not available in WinTask Lite. Not available under Windows XP and 2003.
Syntax
a=ImpersonateUser()
or
a = ImpersonateUser(<user>, <password>,[<domain>])Parameters
When the parameters are omitted, the function will use the default account specified in WinTask Scheduler.
When parameters are included, the impersonation is done using the account specified by <user> and <password>. If <domain> is not specified, the current user domain will be used.
Return value
a, integer, return code which indicates if the impersonation has failed or succeeded.
Remarks
ImpersonateUser can be cancelled by the RevertToSelf function. ImpersonateUser cannot give admin rights to a .EXE, so you can't use it to run a file which needs admin rights for its execution. It can be used for changing file attributes just when the script is running.
Example code
This example explains how a script can acquire administrator privileges in order to change the default user name for the machine. Note that it's better to put another script in the Start Group which can complete the desired actions and delete the AutoAdminLogon from the Registry.
ImpersonateUser("administrator", "master1", "taskware")
auto$="1"
WriteReg("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoAdminLogon",1,auto$)
user$= "administrator"
password$="master1"
dom$= "taskware"
WriteReg("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName",1,user$)
WriteReg("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultPassword",1,password$)
WriteReg("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultDomainName",1,dom$)
flag=4 ' force new NT logon
lg = External("user32.dll", "ExitWindowsEx", flag, 0)