RunWithLogon Return to Local User Account
Keywords: RunWithLogon run under switch back local user account
Question:
I am using RunWithLogon so that I can have my WinBatch script 'run as' an administrator. I need the script to make some high prvilieged changes to the printer. Using the script supplied in RunWithLogon documention, I am able to make these changes using an administrator account. However, after I make all the necessary 'high privleged changes', I then need to be able to set the logged in users default printer. In order to do this, I must switch back and have the script 'run as' the currently logged in user.Any ideas of how this can be done?
Answer:
Yes. The example your are refering to, is really just a script that re-launches itself. Therefore, in theory, your really dealing with two different script (instances). The first script/instance is running under the currently logged in user. It then launches a second instance of a script that runs under the administrator account.So if you need to execute code as the 'logged in' user account after running as 'administrator'. You will need to specify @WAIT in the RunWithLogon statement. This tells the first script/instance to wait for the second Admin instance to finish. Then you can execute any further code you want to run as the current user, after the RunWithLogon statement. See the lines commented with ****** .
;This script will give itself admin privileges moiparams=IntControl(1006,0,0,0,0) if StrIndexNC(moiparams,"URADMIN",0,@fwdscan) ; Already launched as admin param0 = param0 -1 ; Remove the URADMIN param from further consideration else ; need admin privileges if WinVersion(5) >= "2-5-0" ;Win2000 or newer ;NOTE: For security reasons *always* assign the password ; to a variable and then use the variable in the RunWithLogon ; statement. NEVER hardcode the password into the ; RunWithLogon statement or else the password my be ; exposed if the RunWithLogon fails on some kind of error ; This is also a good idea with the userid and domain ; information runas_user = "Administrator" runas_pswd = "password" runas_domain = "" moi=WinExename("") moiparams=strcat(moiparams, " URADMIN") ;******{ ADD CODE HERE YOU WANT TO RUN BEFORE YOUR ADMIN SCRIPT RUNS } ****** RunWithLogon(moi, moiparams, "", @NORMAL, @WAIT, runas_user, runas_domain, runas_pswd,0) ;****** { ADD CODE HERE YOU WANT TO RUN AFTER YOUR ADMIN SCRIPT EXITS } ****** exit else Message("RunWithLogon","Is not designed for this platform.") exit endif endif ;------------------------------- ; Running in Admin mode now. ;------------------------------- ;****** { ADD CODE HERE YOU WANT TO EXECUTE IN ADMIN MODE } ****** exit
Article ID: W15168